You are viewing a plain text version of this content. The canonical link for it is here.
Posted to log4j-user@logging.apache.org by Rajiv Nair <ra...@gmail.com> on 2013/12/16 12:17:22 UTC

Setting to, from, subject values from properties in log4j2 SMTPAppender

Hi

I'm using log4j 2.0-beta9. I have a question about the SMTP appender. I
need to configure the subject, from and to values from properties. I'm
logging a MapMessage and my config is as below -

<?xml version="1.0" encoding="UTF-8"?>
<configuration status="DEBUG">

    <appenders>
        <Console name="Console" target="SYSTEM_OUT">
            <PatternLayout pattern="%d [%t] %-5p %c  - %m%n"/>
        </Console>

        <SMTP name="Mail" subject="Error Log for ${env:HOSTNAME}" to="${sys:
mail.to}" from="${sys:mail.from}"
              smtpHost="${sys:mail.host}" smtpPort="${sys:mail.port}"
smtpDebug="true" bufferSize="1">
            <PatternLayout>
                <pattern>%d [%t] %-5p %c - %m%n</pattern>
            </PatternLayout>
        </SMTP>

        <Async name="AsyncMail">
            <appender-ref ref="Mail" />
        </Async>
    </appenders>

    <loggers>
        <root level="info">
            <appender-ref ref="Console"/>
            <appender-ref ref="AsyncMail">
                <MapFilter onMatch="ACCEPT" onMismatch="DENY">
                    <KeyValuePair key="throwable.class"
value="java.lang.RuntimeException" />
                </MapFilter>
            </appender-ref>
        </root>
    </loggers>
</configuration>


// Java Code to log the msg
Throwable throwable; // this is the exception that is thrown by the app.
MapMessage message = new MapMessage();
message.put("throwable.message", "Entered FM exception mapper for throwable
: " + throwable.getMessage());
message.put("throwable.class", throwable.getClass().getName());
message.put("throwable.stacktrace",
ExceptionUtils.getStackTrace(throwable)); // ExceptionUtils from
apache-commons
LOGGER.error(message, throwable); // org.apache.logging.log4j.Logger

The problem is that none of these values are replaced dynamically. Is there
anyway to do this?

Thanks in advance,
Rajiv