log4j2输出未写入日志文件

时间:2016-05-19 14:50:29

标签: java xml windows log4j2

我第一次尝试使用log4j2。我从各个站点复制了一些配置示例,似乎都没有生成日志文件的输出。输出到STDOUT /控制台工作正常。

我可以从控制台输出中看到已找到log4j2.xml文件。

有关如何将其写入日志文件的任何想法。

我在Windows 7上使用Java 8和log4j 2.5。

Java源代码

package log4j2Demo;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

public class LogDemo {
private static final Logger logger = LogManager.getLogger(LogDemo.class.getName());

    public static void main(String[] args) {
        logger.debug("Hello world - debug log");
        logger.info("Hello world - info log");
        logger.warn("Hello world - warn log");
        logger.error("Hello world - error log");
    }
}

log4j2.xml

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="debug">
    <Properties>
        <Property name="log-path">logs</Property>
    </Properties>
    <Appenders>
        <RollingFile name="file-log" fileName="${log-path}/log4j2demo-info.log"
filePattern="${log-path}/log4j2demo-info-%d{yyyy-MM-dd}.log">
            <PatternLayout>
                <pattern>[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n</pattern>
            </PatternLayout>
            <Policies>
                <TimeBasedTriggeringPolicy interval="1" modulate="true"/>
            </Policies>
        </RollingFile>
        <Console name="console" target="SYSTEM_OUT">
            <PatternLayout pattern="[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n"/>
        </Console>
    </Appenders>
    <Loggers>
        <Logger name="a.demo.of.log4j2" level="info" additivity="false">
            <appender-ref ref="file-log" level="info"/>
        </Logger>
        <Root level="info" additivity="false">
            <appender-ref ref="console"/>
        </Root>
    </Loggers>
</Configuration>

控制台输出

2016-05-20 00:31:46,345 main DEBUG Initializing configuration XmlConfiguration[location=C:\demo\log4j2Demo\bin\log4j2.xml]
2016-05-20 00:31:46,350 main DEBUG Installed script engines
2016-05-20 00:31:46,705 main DEBUG Oracle Nashorn Version: 1.8.0_91, Language: ECMAScript, Threading: Not Thread Safe, Compile: true, Names: {nashorn, Nashorn, js, JS, JavaScript, javascript, ECMAScript, ecmascript}
2016-05-20 00:31:46,706 main DEBUG PluginManager 'Core' found 97 plugins
2016-05-20 00:31:46,706 main DEBUG PluginManager 'Level' found 0 plugins
2016-05-20 00:31:46,710 main DEBUG No scheduled items
2016-05-20 00:31:46,711 main DEBUG Building Plugin[name=property, class=org.apache.logging.log4j.core.config.Property].
2016-05-20 00:31:46,723 main DEBUG PluginManager 'TypeConverter' found 23 plugins
2016-05-20 00:31:46,731 main DEBUG createProperty(name="log-path", value="logs")
2016-05-20 00:31:46,731 main DEBUG Building Plugin[name=properties, class=org.apache.logging.log4j.core.config.PropertiesPlugin].
2016-05-20 00:31:46,737 main DEBUG configureSubstitutor(={log-path=logs}, Configuration(C:\demo\log4j2Demo\bin\log4j2.xml))
2016-05-20 00:31:46,737 main DEBUG PluginManager 'Lookup' found 13 plugins
2016-05-20 00:31:46,738 main DEBUG Building Plugin[name=layout, class=org.apache.logging.log4j.core.layout.PatternLayout].
2016-05-20 00:31:46,745 main DEBUG PatternLayout$Builder(pattern="[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n", PatternSelector=null, Configuration(C:\demo\log4j2Demo\bin\log4j2.xml), Replace=null, charset="null", alwaysWriteExceptions="null", noConsoleNoAnsi="null", header="null", footer="null")
2016-05-20 00:31:46,745 main DEBUG PluginManager 'Converter' found 38 plugins
2016-05-20 00:31:46,759 main DEBUG Building Plugin[name=TimeBasedTriggeringPolicy, class=org.apache.logging.log4j.core.appender.rolling.TimeBasedTriggeringPolicy].
2016-05-20 00:31:46,761 main DEBUG createPolicy(interval="1", modulate="true")
2016-05-20 00:31:46,762 main DEBUG Building Plugin[name=Policies, class=org.apache.logging.log4j.core.appender.rolling.CompositeTriggeringPolicy].
2016-05-20 00:31:46,762 main DEBUG createPolicy(={TimeBasedTriggeringPolicy(nextRolloverMillis=0, interval=1, modulate=true)})
2016-05-20 00:31:46,763 main DEBUG Building Plugin[name=appender, class=org.apache.logging.log4j.core.appender.RollingFileAppender].
2016-05-20 00:31:46,764 main DEBUG createAppender(fileName="logs/log4j2demo-info.log", filePattern="logs/log4j2demo-info-%d{yyyy-MM-dd}.log", append="null", name="file-log", bufferedIO="null", bufferSize="null", immediateFlush="null", Policies(CompositeTriggeringPolicy(policies=[TimeBasedTriggeringPolicy(nextRolloverMillis=0, interval=1, modulate=true)])), Strategy=null, PatternLayout([%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n), Filter=null, ignoreExceptions="null", advertise="null", advertiseURI="null", Configuration(C:\demo\log4j2Demo\bin\log4j2.xml))
2016-05-20 00:31:46,774 main DEBUG Starting RollingFileManager logs/log4j2demo-info.log
2016-05-20 00:31:46,776 main DEBUG PluginManager 'FileConverter' found 2 plugins
2016-05-20 00:31:46,778 main DEBUG Building Plugin[name=layout, class=org.apache.logging.log4j.core.layout.PatternLayout].
2016-05-20 00:31:46,780 main DEBUG PatternLayout$Builder(pattern="[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n", PatternSelector=null, Configuration(C:\demo\log4j2Demo\bin\log4j2.xml), Replace=null, charset="null", alwaysWriteExceptions="null", noConsoleNoAnsi="null", header="null", footer="null")
2016-05-20 00:31:46,784 main DEBUG Building Plugin[name=appender, class=org.apache.logging.log4j.core.appender.ConsoleAppender].
2016-05-20 00:31:46,785 main DEBUG Not in a ServletContext environment, thus not loading WebLookup plugin.
2016-05-20 00:31:46,786 main DEBUG PluginManager 'Converter' found 38 plugins
2016-05-20 00:31:46,788 main DEBUG Jansi is not installed, cannot find org.fusesource.jansi.WindowsAnsiOutputStream
2016-05-20 00:31:46,789 main DEBUG Starting OutputStreamManager SYSTEM_OUT.false-2
2016-05-20 00:31:46,799 main DEBUG ConsoleAppender$Builder(PatternLayout([%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n), Filter=null, target="SYSTEM_OUT", name="console", follow="null", ignoreExceptions="null")
2016-05-20 00:31:46,800 main DEBUG Jansi is not installed, cannot find org.fusesource.jansi.WindowsAnsiOutputStream
2016-05-20 00:31:46,800 main DEBUG Starting OutputStreamManager SYSTEM_OUT.false
2016-05-20 00:31:46,800 main DEBUG Building Plugin[name=appenders, class=org.apache.logging.log4j.core.config.AppendersPlugin].
2016-05-20 00:31:46,801 main DEBUG createAppenders(={file-log, console})
2016-05-20 00:31:46,801 main DEBUG Building Plugin[name=appender-ref, class=org.apache.logging.log4j.core.config.AppenderRef].
2016-05-20 00:31:46,802 main DEBUG createAppenderRef(ref="file-log", level="INFO", Filter=null)
2016-05-20 00:31:46,802 main DEBUG Building Plugin[name=logger, class=org.apache.logging.log4j.core.config.LoggerConfig].
2016-05-20 00:31:46,806 main DEBUG createLogger(additivity="false", level="INFO", name="a.demo.of.log4j2", includeLocation="null", ={file-log}, ={}, Configuration(C:\demo\log4j2Demo\bin\log4j2.xml), Filter=null)
2016-05-20 00:31:46,808 main DEBUG Building Plugin[name=appender-ref, class=org.apache.logging.log4j.core.config.AppenderRef].
2016-05-20 00:31:46,809 main DEBUG createAppenderRef(ref="console", level="null", Filter=null)
2016-05-20 00:31:46,809 main DEBUG Building Plugin[name=root, class=org.apache.logging.log4j.core.config.LoggerConfig$RootLogger].
2016-05-20 00:31:46,810 main DEBUG createLogger(additivity="false", level="INFO", includeLocation="null", ={console}, ={}, Configuration(C:\demo\log4j2Demo\bin\log4j2.xml), Filter=null)
2016-05-20 00:31:46,810 main DEBUG Building Plugin[name=loggers, class=org.apache.logging.log4j.core.config.LoggersPlugin].
2016-05-20 00:31:46,811 main DEBUG createLoggers(={a.demo.of.log4j2, root})
2016-05-20 00:31:46,812 main DEBUG Configuration XmlConfiguration[location=C:\demo\log4j2Demo\bin\log4j2.xml] initialized
2016-05-20 00:31:46,812 main DEBUG Starting configuration XmlConfiguration[location=C:\demo\log4j2Demo\bin\log4j2.xml]
2016-05-20 00:31:46,813 main DEBUG Started configuration XmlConfiguration[location=C:\demo\log4j2Demo\bin\log4j2.xml] OK.
2016-05-20 00:31:46,813 main DEBUG Shutting down OutputStreamManager SYSTEM_OUT.false-1
2016-05-20 00:31:46,813 main DEBUG Stopped org.apache.logging.log4j.core.config.DefaultConfiguration@6f539caf OK
2016-05-20 00:31:46,860 main DEBUG Registering MBean org.apache.logging.log4j2:type=73d16e93
2016-05-20 00:31:46,862 main DEBUG Registering MBean org.apache.logging.log4j2:type=73d16e93,component=StatusLogger
2016-05-20 00:31:46,864 main DEBUG Registering MBean org.apache.logging.log4j2:type=73d16e93,component=ContextSelector
2016-05-20 00:31:46,865 main DEBUG Registering MBean org.apache.logging.log4j2:type=73d16e93,component=Loggers,name=
2016-05-20 00:31:46,866 main DEBUG Registering MBean org.apache.logging.log4j2:type=73d16e93,component=Loggers,name=a.demo.of.log4j2
2016-05-20 00:31:46,867 main DEBUG Registering MBean org.apache.logging.log4j2:type=73d16e93,component=Appenders,name=console
2016-05-20 00:31:46,868 main DEBUG Registering MBean org.apache.logging.log4j2:type=73d16e93,component=Appenders,name=file-log
2016-05-20 00:31:46,870 main DEBUG Reconfiguration complete for context[name=73d16e93] at URI C:\demo\log4j2Demo\bin\log4j2.xml (org.apache.logging.log4j.core.LoggerContext@7722c3c3) with optional ClassLoader: null
2016-05-20 00:31:46,871 main DEBUG Shutdown hook enabled. Registering a new one.
2016-05-20 00:31:46,872 main DEBUG LoggerContext[name=73d16e93, org.apache.logging.log4j.core.LoggerContext@7722c3c3] started OK.
[INFO ] 2016-05-20 00:31:46.878 [main] LogDemo - Hello world - info log
[WARN ] 2016-05-20 00:31:46.879 [main] LogDemo - Hello world - warn log
[ERROR] 2016-05-20 00:31:46.879 [main] LogDemo - Hello world - error log
2016-05-20 00:31:46,880 pool-1-thread-1 DEBUG Stopping LoggerContext[name=73d16e93, org.apache.logging.log4j.core.LoggerContext@7722c3c3]
2016-05-20 00:31:46,880 pool-1-thread-1 DEBUG Stopping LoggerContext[name=73d16e93, org.apache.logging.log4j.core.LoggerContext@7722c3c3]...
2016-05-20 00:31:46,882 pool-1-thread-1 DEBUG Shutting down RollingFileManager logs/log4j2demo-info.log
2016-05-20 00:31:46,882 pool-1-thread-1 DEBUG Shutting down OutputStreamManager SYSTEM_OUT.false
2016-05-20 00:31:46,882 pool-1-thread-1 DEBUG Stopped XmlConfiguration[location=C:\demo\log4j2Demo\bin\log4j2.xml] OK
2016-05-20 00:31:46,882 pool-1-thread-1 DEBUG Stopped LoggerContext[name=73d16e93, org.apache.logging.log4j.core.LoggerContext@7722c3c3]...

显示已创建文件但空

的Windows目录列表
C:\demo\log4j2Demo\logs>dir
 Volume in drive C is dfsdf0694
 Volume Serial Number is asdffd

 Directory of C:\demo\log4j2Demo\logs

20/05/2016  12:45 AM    <DIR>          .
20/05/2016  12:45 AM    <DIR>          ..
20/05/2016  12:31 AM                 0 log4j2demo-info.log
               1 File(s)              0 bytes
               2 Dir(s)  263,832,457,216 bytes free

1 个答案:

答案 0 :(得分:1)

它没有写入您的文件,因为您用于记录的Logger名为log4j2Demo.LogDemo,但您配置的记录器名为“a.demo.of.log4j2”。由于名称不匹配,因此将使用您已配置为登录到控制台的根记录器。