<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN">
<Properties>
<Property name="log-path">logs</Property>
</Properties>
<Appenders>
<Console name="console-log" target="SYSTEM_OUT">
<PatternLayout pattern="[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n"/>
</Console>
<File name="trace-log" fileName="${log-path}/trace.log" immediateFlush="true" append="true">
<PatternLayout pattern="%d{yyy-MM-dd HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
</File>
<File name="error-log" fileName="${log-path}/error.log" immediateFlush="true" append="true">
<PatternLayout pattern="%d{yyy-MM-dd HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
</File>
<File name="debug-log" fileName="${log-path}/debug.log" immediateFlush="true" append="true">
<PatternLayout pattern="%d{yyy-MM-dd HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
</File>
<File name="info-log" fileName="${log-path}/info.log" immediateFlush="true" append="true">
<PatternLayout pattern="%d{yyy-MM-dd HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
</File>
</Appenders>
<Loggers>
<Root level="error">
<AppenderRef ref="error-log"/>
<AppenderRef ref="console-log"/>
</Root>
<Root level="info">
<appender-ref ref="info-log"/>
<AppenderRef ref="console-log"/>
</Root>
<Root level="debug">
<AppenderRef ref="debug-log"/>
<AppenderRef ref="console-log"/>
</Root>
<Root level="trace">
<AppenderRef ref="trace-log"/>
<AppenderRef ref="console-log"/>
</Root>
</Loggers>
</Configuration>
我上面编写的代码仅将所有内容附加到trace-log。事实上,最后调用的root只是存储所有日志信息。我该如何纠正?我希望所有日志文件都能直观地工作。
答案 0 :(得分:1)
这里有一些问题:
你可以试试这个:
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN">
<Properties>
<Property name="log-path">logs</Property>
</Properties>
<Appenders>
<Console name="console-log" target="SYSTEM_OUT">
<PatternLayout pattern="[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n"/>
</Console>
<File name="trace-log" fileName="${log-path}/trace.log" immediateFlush="true" append="true">
<ThresholdFilter level="debug" onMatch="DENY" onMismatch="ACCEPT"/>
<PatternLayout pattern="%d{yyy-MM-dd HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
</File>
<File name="error-log" fileName="${log-path}/error.log" immediateFlush="true" append="true">
<Filters>
<ThresholdFilter level="error" onMatch="NEUTRAL" onMismatch="DENY"/>
<ThresholdFilter level="fatal" onMatch="DENY" onMismatch="ACCEPT"/>
</Filters>
<PatternLayout pattern="%d{yyy-MM-dd HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
</File>
<File name="debug-log" fileName="${log-path}/debug.log" immediateFlush="true" append="true">
<Filters>
<ThresholdFilter level="debug" onMatch="NEUTRAL" onMismatch="DENY"/>
<ThresholdFilter level="info" onMatch="DENY" onMismatch="ACCEPT"/>
</Filters> <PatternLayout pattern="%d{yyy-MM-dd HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
</File>
<File name="info-log" fileName="${log-path}/info.log" immediateFlush="true" append="true">
<Filters>
<ThresholdFilter level="info" onMatch="NEUTRAL" onMismatch="DENY"/>
<ThresholdFilter level="warn" onMatch="DENY" onMismatch="ACCEPT"/>
</Filters>
<PatternLayout pattern="%d{yyy-MM-dd HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
</File>
</Appenders>
<Loggers>
<Root level="trace">
<AppenderRef ref="error-log"/>
<AppenderRef ref="info-log"/>
<AppenderRef ref="debug-log"/>
<AppenderRef ref="trace-log/>
<AppenderRef ref="console-log"/>
</Root>
</Loggers>
</Configuration>