java Log4j 2 不写入文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25118993/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Log4j 2 doesn't write to file
提问by Bart Pelle
Having the following config file:
具有以下配置文件:
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="warn">
<Appenders>
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout pattern="[%d{HH:mm:ss.SSS} %-5level] %logger{36} - %msg%n"/>
</Console>
<File name="File" fileName="error.log">
<PatternLayout pattern="[%d{ISO8601} %-5level] %logger{36} - %msg%n"/>
</File>
</Appenders>
<Loggers>
<Logger name="errors" level="error">
<AppenderRef ref="File"/>
</Logger>
<Root level="all">
<AppenderRef ref="Console"/>
</Root>
</Loggers>
</Configuration>
only writes (all) logging output to the console. I however had the intention to write anything above error
to a file named error.log
with a slightly different format. However, running my application results in everything being written to the console, leaving an empty file behind (which gets created, just not filled).
只将(所有)日志输出写入控制台。然而,我打算将上面的任何内容写入error
以error.log
稍微不同的格式命名的文件中。但是,运行我的应用程序会导致所有内容都写入控制台,并留下一个空文件(已创建,只是未填充)。
Somehow it seems like the Root logger catches everything because I had also tried this:
不知何故,根记录器似乎捕获了所有内容,因为我也尝试过:
<Logger name="errors" level="error">
<AppenderRef ref="Console"/>
</Logger>
which does not log twice. I'm out of ideas really, I even copied an example from the docs (sample #2 from here) and that aswell leaves an empty file.
这不会记录两次。我真的没有想法,我什至从文档中复制了一个示例(来自此处的示例 #2),并且还留下了一个空文件。
采纳答案by Bart Pelle
Ah, I was being stupid. The attribute name
seems to be a filter for which classes will use that logger. Changing name
to my top level package resolved the issue.
啊,我傻了。该属性name
似乎是类将使用该记录器的过滤器。更改name
为我的顶级包解决了这个问题。
回答by Remko Popma
Yes, the root logger level is ALL so it will receive all events. One option is to do this:
是的,根记录器级别是 ALL,因此它将接收所有事件。一种选择是这样做:
<Loggers>
<Root level="all">
<AppenderRef ref="Console" level="trace" />
<AppenderRef ref="File" level="error" />
</Root>
</Loggers>
This way you only have one logger, so you don't need to worry about additivity and in your code you can just write LogManager.getLogger(MyClass.class)
to get a Logger
instance. (If you use a named Logger, you would need to use the logger name in your code: LogManager.getLogger("error")
.)
这样您就只有一个记录器,因此您无需担心可加性,并且在您的代码中您只需编写LogManager.getLogger(MyClass.class)
一个Logger
实例即可。(如果您使用命名记录器,则需要在代码中使用记录器名称:LogManager.getLogger("error")
。)
回答by Mike Ashby
Log4J2 doesn't write to file until the process has completed. By default the buffer isn't flushed with each call to the logger. For example,
Log4J2 在进程完成之前不会写入文件。默认情况下,每次调用记录器时都不会刷新缓冲区。例如,
<File name="File" fileName="LogfileName.log" immediateFlush="false" append="false">
<PatternLayout pattern="%d{yyy-MM-dd HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
</File>
will not log to the file immediately. Change the values of immediateFlush to true to force the buffer to flush on each call to logger.
不会立即登录到该文件。将immediateFlush 的值更改为true 以强制缓冲区在每次调用logger 时刷新。
回答by Sireesh Yarlagadda
Try this something like this.
试试这个。
Here Target is important attribute to be noticed.
这里的 Target 是需要注意的重要属性。
The console appender has one param element defined. Looking at the javadoc for ConsoleAppender , the setTarget method is used to choose which console stream to print messages to, System.out or System.err. The example configures the appender to use System.out.
控制台 appender 定义了一个 param 元素。查看 ConsoleAppender 的 javadoc,setTarget 方法用于选择将消息打印到哪个控制台流,System.out 或 System.err。该示例将 appender 配置为使用 System.out。
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
<appender name="console" class="org.apache.log4j.ConsoleAppender">
<param name="Target" value="System.out"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%-5p %c{1} - %m%n"/>
</layout>
</appender>
<root>
<priority value ="debug" />
<appender-ref ref="console" />
</root>
</log4j:configuration>