在logback中,我将logback.xml文件配置为在单独的文件(debug.log和error.log)中分别调试和错误日志行。
参数的想法是创建类似的东西:
logger.error("A new error occured with a param", 1);
其中1是传递给记录器的整数,应该写入error.log,如:
11:22:17.678 [main] ERROR c.j.example.logbackmavenexample.App - A new error occured with a param - 1
我的logback.xml如下所示:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<!-- System.out Appender -->
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{5} - %msg%n</pattern>
</encoder>
</appender>
<appender name="debugFile" class="ch.qos.logback.core.FileAppender">
<file>/Users/henriqueferreira/Desktop/separate-log/debug.log</file>
<filter class="ch.qos.logback.classic.filter.LevelFilter">
<level>DEBUG</level>
<onMatch>ACCEPT</onMatch>
<onMismatch>DENY</onMismatch>
</filter>
<encoder>
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n%ex{full}</pattern>
</encoder>
</appender>
<appender name="errorFile" class="ch.qos.logback.core.FileAppender">
<File>/Users/henriqueferreira/Desktop/separate-log/error.log</File>
<filter class="ch.qos.logback.classic.filter.LevelFilter">
<level>ERROR</level>
<onMatch>ACCEPT</onMatch>
<onMismatch>DENY</onMismatch>
</filter>
<encoder>
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n%ex{full}</pattern>
</encoder>
</appender>
<root level="DEBUG"><!-- Using the lowest level here -->
<appender-ref ref="debugFile" />
<appender-ref ref="errorFile" />
</root>
</configuration>
是否可以在创建的日志文件中写入传递的参数?
答案 0 :(得分:0)
感谢@Bentaye,我找到了答案。基本上不是将整数作为参数传递,而是可以传递标记,例如:
Marker interesting = MarkerFactory.getMarker("INTERESTING");
logger.debug(interesting, "something happened");
它会按我的意愿记录:
12:37:59.060 [main] DEBUG c.j.example.logbackmavenexample.App - INTERESTING - something happened
我只需在logback.xml文件中的此文件模式中添加%marker:
<encoder>
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %marker - %msg%n%ex{full}</pattern>
</encoder>