001/**
002 * Logback: the reliable, generic, fast and flexible logging framework.
003 * Copyright (C) 1999-2015, QOS.ch. All rights reserved.
004 *
005 * This program and the accompanying materials are dual-licensed under
006 * either the terms of the Eclipse Public License v1.0 as published by
007 * the Eclipse Foundation
008 *
009 *   or (per the licensee's choosing)
010 *
011 * under the terms of the GNU Lesser General Public License version 2.1
012 * as published by the Free Software Foundation.
013 */
014package ch.qos.logback.access.net;
015
016import ch.qos.logback.access.PatternLayout;
017import ch.qos.logback.access.spi.IAccessEvent;
018import ch.qos.logback.core.Layout;
019import ch.qos.logback.core.boolex.EventEvaluator;
020import ch.qos.logback.core.helpers.CyclicBuffer;
021import ch.qos.logback.core.net.SMTPAppenderBase;
022
023/**
024 * Send an e-mail when a specific access event occurs, typically when certain
025 * pages are accessed.
026 * 
027 * For more information about this appender, please refer to the online manual
028 * at http://logback.qos.ch/manual/appenders.html#AccessSMTPAppender
029 * <p>
030 * 
031 * @author Ceki G&uuml;lc&uuml;
032 * @author S&eacute;bastien Pennec
033 * 
034 */
035public class SMTPAppender extends SMTPAppenderBase<IAccessEvent> {
036
037    static final String DEFAULT_SUBJECT_PATTERN = "%m";
038
039    /**
040     * The default constructor will instantiate the appender with a
041     * {@link EventEvaluator} that will trigger on events with level ERROR or
042     * higher.
043     */
044    public SMTPAppender() {
045    }
046
047    /**
048     * Use <code>evaluator</code> passed as parameter as the {@link EventEvaluator}
049     * for this SMTPAppender.
050     */
051    public SMTPAppender(EventEvaluator<IAccessEvent> evaluator) {
052        this.eventEvaluator = evaluator;
053    }
054
055    /**
056     * Perform SMTPAppender specific appending actions, mainly adding the event to
057     * the appropriate cyclic buffer.
058     */
059    @Override
060    protected void subAppend(CyclicBuffer<IAccessEvent> cb, IAccessEvent event) {
061        cb.add(event);
062    }
063
064    @Override
065    protected void fillBuffer(CyclicBuffer<IAccessEvent> cb, StringBuffer sbuf) {
066        int len = cb.length();
067        for (int i = 0; i < len; i++) {
068            // sbuf.append(MimeUtility.encodeText(layout.format(cb.getOrCreate())));
069            IAccessEvent event = cb.get();
070            sbuf.append(layout.doLayout(event));
071        }
072    }
073
074    @Override
075    protected Layout<IAccessEvent> makeSubjectLayout(String subjectStr) {
076        if (subjectStr == null) {
077            subjectStr = DEFAULT_SUBJECT_PATTERN;
078        }
079        PatternLayout pl = new PatternLayout();
080        pl.setPattern(subjectStr);
081        pl.start();
082        return pl;
083    }
084
085    @Override
086    protected PatternLayout makeNewToPatternLayout(String toPattern) {
087        PatternLayout pl = new PatternLayout();
088        pl.setPattern(toPattern);
089        return pl;
090    }
091
092    @Override
093    protected boolean eventMarksEndOfLife(IAccessEvent eventObject) {
094        return false;
095    }
096
097}