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
025 * certain pages are accessed.
026 * 
027 * For more information about this appender, please refer to the online manual at
028 * http://logback.qos.ch/manual/appenders.html#AccessSMTPAppender
029 * <p>
030 * @author Ceki G&uuml;lc&uuml;
031 * @author S&eacute;bastien Pennec
032 * 
033 */
034public class SMTPAppender extends SMTPAppenderBase<IAccessEvent> {
035
036    static final String DEFAULT_SUBJECT_PATTERN = "%m";
037
038    /**
039     * The default constructor will instantiate the appender with a
040     * {@link EventEvaluator} that will trigger on events with level
041     * ERROR or higher.
042     */
043    public SMTPAppender() {
044    }
045
046    /**
047     * Use <code>evaluator</code> passed as parameter as the {@link
048     * EventEvaluator} for this SMTPAppender.
049     */
050    public SMTPAppender(EventEvaluator<IAccessEvent> evaluator) {
051        this.eventEvaluator = evaluator;
052    }
053
054    /**
055     * Perform SMTPAppender specific appending actions, mainly adding the event to
056     * the appropriate cyclic buffer.
057     */
058    @Override
059    protected void subAppend(CyclicBuffer<IAccessEvent> cb, IAccessEvent event) {
060        cb.add(event);
061    }
062
063    @Override
064    protected void fillBuffer(CyclicBuffer<IAccessEvent> cb, StringBuffer sbuf) {
065        int len = cb.length();
066        for (int i = 0; i < len; i++) {
067            // sbuf.append(MimeUtility.encodeText(layout.format(cb.getOrCreate())));
068            IAccessEvent event = cb.get();
069            sbuf.append(layout.doLayout(event));
070        }
071    }
072
073    @Override
074    protected Layout<IAccessEvent> makeSubjectLayout(String subjectStr) {
075        if (subjectStr == null) {
076            subjectStr = DEFAULT_SUBJECT_PATTERN;
077        }
078        PatternLayout pl = new PatternLayout();
079        pl.setPattern(subjectStr);
080        pl.start();
081        return pl;
082    }
083
084    @Override
085    protected PatternLayout makeNewToPatternLayout(String toPattern) {
086        PatternLayout pl = new PatternLayout();
087        pl.setPattern(toPattern);
088        return pl;
089    }
090
091    @Override
092    protected boolean eventMarksEndOfLife(IAccessEvent eventObject) {
093        return false;
094    }
095
096}