Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/355.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在log4j2 SMTPAppender的运行时属性中设置到、从、主题值_Java_Log4j2_Smtpappender - Fatal编程技术网

Java 在log4j2 SMTPAppender的运行时属性中设置到、从、主题值

Java 在log4j2 SMTPAppender的运行时属性中设置到、从、主题值,java,log4j2,smtpappender,Java,Log4j2,Smtpappender,我使用的是log4j2.0-beta9。我有一个关于SMTP附加器的问题。我需要配置主题、属性中的from和to值。我正在记录一条MapMessage,我的配置如下- <?xml version="1.0" encoding="UTF-8"?> <configuration status="DEBUG"> <appenders> <Console name="Console" target="SYSTEM_OUT">

我使用的是log4j2.0-beta9。我有一个关于SMTP附加器的问题。我需要配置主题、属性中的from和to值。我正在记录一条MapMessage,我的配置如下-

<?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", 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

%d[%t]-5p%c-%m%n
//记录消息的Java代码
可丢弃的;可丢弃的这是应用程序引发的异常。
MapMessage=新建MapMessage();
message.put(“throwable.message”,throwable.getMessage());
message.put(“throwable.class”,throwable.getClass().getName());
message.put(“throwable.stacktrace”,ExceptionUtils.getStackTrace(throwable));//apache commons中的例外部分
LOGGER.error(消息,可丢弃);//org.apache.logging.log4j.Logger
问题是这些值都不会被动态替换。有没有办法做到这一点


提前感谢。

您需要将
mail.to
mail.from
系统属性设置为。您遇到的问题可能是您正在Servlet 3.0环境中运行,在这种情况下,在执行设置属性的代码之前,正在处理Log4j2.xml文件

如果是这种情况,您可以创建一个servlet容器初始值设定项,在加载Log4j2的servlet容器初始值设定项之前,在
web.xml
文件中配置该初始值设定项以加载