我正在尝试将日志显示到控制台以及文件中,但是在文件中我只想要从某个特定类中显示的某些日志,我不知道如何执行此操作。下面是我的logback.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration>
<configuration>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>{ "date_time":"%date", "thread":"[%thread]", "log_level":"%-5level", "class_name":"%logger{0}", "log_message":"%msg" }%n</pattern>
</encoder>
</appender>
<appender name="FILE" class="ch.qos.logback.core.FileAppender">
<file> test.txt </file>
<append>false</append>
<encoder>
<pattern>{ "date_time":"%date", "thread":"[%thread]", "log_level":"%-5level", "class_name":"%logger{0}", "log_message":"%msg" }%n</pattern>
</encoder>
</appender>
<root level="info">
<appender-ref ref="FILE" />
<appender-ref ref="STDOUT" />
</root>
</configuration>
答案 0 :(得分:10)
只需为您的&#34;一个特定类&#34;声明一个logger
然后将该记录器与您的FILE
appender相关联。
例如:
<appender name="FILE" class="ch.qos.logback.core.FileAppender">
<file>test.log</file>
<encoder>
<pattern>{ "date_time":"%date", "thread":"[%thread]", "log_level":"%-5level", "class_name":"%logger{0}", "log_message":"%msg" }%n</pattern>
</encoder>
</appender>
<logger name="your.particular.Class">
<appender-ref ref="FILE" />
</logger>
<root level="info">
<appender-ref ref="STDOUT" />
</root>
这将确保your.particular.Class
发出的任何日志事件仅定向到FILE
appender,所有其他日志仅定向 到STDOUT
appender。