弄了三天,收获其实不小,终于到最后收尾了。
一,SLF4j与Log4j2的集成
1,先把需要的东搞到手
2,把这些包加载到项目中
3,写配置文件
<?xml version="1.0" encoding="UTF-8" ?>
<Configuration status="WARN">
<Appenders>
<Console name="console" target="SYSTEM_OUT">
<PatternLayout pattern="[%-5level] %d{yyyy-MM-dd HH:mm:ss} [%t] - %msg%n" />
</Console>
</Appenders>
<Loggers>
<Root level="info" additivity="false">
<AppenderRef ref="console" />
</Root>
</Loggers>
</Configuration>
4,测试
package demo.log;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class DemoExample {
public static void main(String[] args) {
// System.setProperty(SimpleLogger.DEFAULT_LOG_LEVEL_KEY, "DEBUG");
Logger logger = LoggerFactory.getLogger(DemoExample.class);
logger.info("Info Message");
logger.error("Error Message");
logger.warn("Warn Message");
logger.debug("Debug Message");
logger.trace("Trace Message");
}
}
二,SLF4j与Logback的集成
1,加载包
2,配置文件
<?xml version="1.0" encoding="UTF-8" ?>
<configuration>
<appender name="consoleAppender" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>
%d{yyyy-MM-dd HH:mm:ss} [%thread] %-5level %logger{55} - %msg%n
</pattern>
</encoder>
</appender>
<root level="debug">
<appender-ref ref="consoleAppender" />
</root>
</configuration>
3,测试
package demo.log;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class DemoExample {
public static void main(String[] args) {
// System.setProperty(SimpleLogger.DEFAULT_LOG_LEVEL_KEY, "DEBUG");
Logger logger = LoggerFactory.getLogger(DemoExample.class);
logger.info("Info Message");
logger.error("Error Message");
logger.warn("Warn Message");
logger.debug("Debug Message");
logger.trace("Trace Message");
}
}
4,效果
D:\Java\jdk17\bin\java.exe "-javaagent:D:\JetBrains\IntelliJ IDEA Community Edition 2024.3.1.1\lib\idea_rt.jar=1190:D:\JetBrains\IntelliJ IDEA Community Edition 2024.3.1.1\bin" -Dfile.encoding=UTF-8 -classpath D:\test\LogLearn\out\production\LogLearn;D:\test\log-lib\slf4j-api-2.0.9.jar;D:\test\log-lib\logback-classic-1.5.9.jar;D:\test\log-lib\logback-core-1.5.9.jar demo.log.DemoExample
2025-01-04 09:07:41 [main] INFO demo.log.DemoExample - Info Message
2025-01-04 09:07:41 [main] ERROR demo.log.DemoExample - Error Message
2025-01-04 09:07:41 [main] WARN demo.log.DemoExample - Warn Message
2025-01-04 09:07:41 [main] DEBUG demo.log.DemoExample - Debug Message
Process finished with exit code 0