<?xml version="1.0" encoding="UTF-8"?>
<Configuration>
<Appenders>
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
</Console>
<Appender type="File" name="File" fileName="/path/to/logfiles/AandB.log">
<Layout type="PatternLayout">
<Pattern>%d %p %C{1.} [%t] %m%n</Pattern>
</Layout>
</Appender>
</Appenders>
<Loggers>
<Logger level="info" name="A"><!-- speficially route these messages to the file appender only -->
<AppenderRef ref="File"/>
</Logger>
<Logger level="info" name="B"><!-- speficially route these messages to the file and console appenders -->
<AppenderRef ref="Console"/>
<AppenderRef ref="File"/>
</Logger>
<Root level="trace"> <!-- per default route all log statements to the console, which is where logging for C will end up -->
<AppenderRef ref="Console"/>
</Root>
</Loggers>
</Configuration>
1条答案
按热度按时间kkbh8khc1#
在java代码中,你不仅有log语句,还有一些
log
示例的初始化器,如下所示:现在,在你的日志配置(log4j2.xml)中,记录器匹配类别,你可以定义哪个类别对应哪个appender。最后,你可以设置appender写入文件或控制台或其他目的地,如下所示:
虽然这是可能的,但我不认为为每个类创建一个单独的日志文件(=〉file appender)是非常有效的,但在库级别上这是有意义的。
详情请参阅https://logging.apache.org/log4j/2.x/manual/configuration.html