一,我们采用硬编码体验一下几个使用比较多的日志
分别导入几种日志的 jar 包
XML
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.fll</groupId>
<artifactId>log-system-study</artifactId>
<version>1.0-SNAPSHOT</version>
<name>log-system-study</name>
<!-- FIXME change it to the project's website -->
<url>http://www.example.com</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<!-- === log4j1 相关jar包 ==== -->
<!-- 单独导入 log4j1 -->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
<!-- === log4j2 相关jar包 === -->
<!-- 单独导入 log4j2 -->
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>2.13.3</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.13.3</version>
</dependency>
<!-- === logback 相关jar包 ==== -->
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.2.4</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-core</artifactId>
<version>1.2.4</version>
</dependency>
</dependencies>
<build>
</build>
</project>
java
package com.fll;
import ch.qos.logback.classic.LoggerContext;
import ch.qos.logback.classic.util.ContextInitializer;
import ch.qos.logback.core.joran.spi.JoranException;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.spi.ExtendedLogger;
import org.junit.Test;
import java.net.URL;
import java.util.logging.Logger;
public class LoggerTest {
/**
* java自带的日志工具
* java.util.logging.Logger
*/
@Test
public void julLogger() {
Logger logger = Logger.getLogger("julLogger");
//System.setProperty("-Djava.util.logging.config.file", "classpath:/logging.properties");
//logger.setLevel(Level.FINE);
logger.info("julLogger Hello World!");
//运行结果 显示红色
//七月 30, 2024 10:51:38 上午 com.fll.LoggerTest julLogger
//信息: julLogger Hello World!
}
/**
* log4j1
*/
@Test
public void log4JLogger(){
org.apache.log4j.Logger log4JLogger = org.apache.log4j.Logger.getLogger("log4JLogger");
log4JLogger.info("log4J_1_Logger Hello World!");
//运行结果
//[log4j_1] 2024-07-30 10:55:29,017 INFO [log4J_1_Logger] - log4J_1_Logger Hello World!
}
/**
* log4j2
*/
@Test
public void log4J_2_Logger(){
ExtendedLogger logger= LogManager.getContext().getLogger("log4J_2_Logger");
logger.info("log4J_2_Logger Hello World!");
//运行结果
//[log4j_2] line=41 10:55:50.050 [main]INFO - log4J_2_Logger Hello World!
}
/**
* logback
*/
@Test
public void logBackLogger(){
// Here we create context
LoggerContext loggerContext = new LoggerContext();
//创建一个LoggerContext的初始化器,该初始化器可以通过configureByResource()方法,
//使用指定的配置(xml,或者 properties)对LoggerContext实例进行初始化
// Initializer is used to enrich context with details
ContextInitializer contextInitializer = new ContextInitializer(loggerContext);
try {
//获取我们的配置文件
// Get a configuration file from classpath
URL configurationUrl = Thread.currentThread()
.getContextClassLoader().getResource("logback.xml");
if (configurationUrl == null) {
throw new IllegalStateException
("Unable to find custom logback configuration file");
}
// 解析配置文件,将解析到的配置设置给 LoggerContext
// Ask context initializer to load configuration into context
contextInitializer.configureByResource(configurationUrl);
// Here we get logger from context
ch.qos.logback.classic.Logger logger =
loggerContext.getLogger("logBackLogger");
logger.info("logBackLogger Hello World!");
//运行结果
//[logback] 2024-07-30 10:57:03 [main] INFO logBackLogger - logBackLogger Hello World!
} catch (JoranException e) {
throw new RuntimeException("Unable to configure logger", e);
}
}
@Test
public void logBackLogger1(){
// Here we create context
LoggerContext loggerContext = new LoggerContext();
// Initializer is used to enrich context with details
ContextInitializer contextInitializer = new ContextInitializer(loggerContext);
try {
//autoConfig 方法中 会先调用 findURLOfDefaultConfigurationFile()
//获取一个默认的配置文件,
// 然后调用configureByResource 对 loggerContext进行初始化
// findURLOfDefaultConfigurationFile 逻辑是先获取环境变量
//logback.configurationFile
// 配置的文件,找不到再找 logback-test.xml ,logback.groovy , logback.xml
contextInitializer.autoConfig();
// Here we get logger from context
ch.qos.logback.classic.Logger logger =
loggerContext.getLogger("logBackLogger");
logger.info("logBackLogger Hello World!");
//运行结果
//[logback] 2024-07-30 10:57:51 [main] INFO logBackLogger - logBackLogger Hello World!
} catch (JoranException e) {
throw new RuntimeException("Unable to configure logger", e);
}
}
}
log4j的配置文件 log4j.properties
java
log4j.rootLogger=info, stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=[log4j_1] %d %p [%c] - %m%n
log4j2的配置文件 log4j2.xml
XML
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<appenders>
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout pattern="[log4j_2] line=%L %d{HH:mm:ss.sss} [%t]%highlight{%-5level} - %msg%n"/>
</Console>
<!-- <Console name="Console" target="SYSTEM_0UT"> -->
<!-- <PatternLayout pattern="file=%c line=%L %dHH:mm:ss.sss}[%t]%highlight{%-5level} %logger{36}-%msg%n"/> -->
<!-- </Console> -->
</appenders>
<loggers>
<root level="debug">
<appender-ref ref="Console"/>
</root>
</loggers>
</configuration>
java.util.logging.Logger 配置文件 logging.properties
java
.level=WARNING
handlers=java.util.logging.ConsoleHandler
java.util.logging.ConsoleHandler.level=WARNING
java.util.logging.ConsoleHandler.formatter=java.util.logging.SimpleFormatter
logback的配置文件 logback.xml
XML
<configuration>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>[logback] %d{yyyy-MM-dd HH:mm:ss} [%thread] %-5level %logger{36} - %msg%n</pattern>
</encoder>
</appender>
<root level="debug">
<appender-ref ref="STDOUT" />
</root>
</configuration>
二、采用 commons-logging 动态选用日志
<!-- === commons-logging 相关jar包 == -->
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.2</version>
</dependency>
先只导入commons-logging.jar不导入其他的日志包
java
package com.fll;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.junit.Test;
public class CommonLoggingTest {
@Test
public void commonLogging(){
Log logger = LogFactory.getLog("commonLogging");
logger.info("commonLogging Hello World!");
}
}
可以看到运行结果:日志打印采用的是
class org.apache.commons.logging.impl.Jdk14Logger
内部封装的是 java.util.logging.Logger
除了commons-logging之外再导入 log4j1的 jar 包
XML
<!-- === commons-logging 相关jar包 == -->
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.2</version>
</dependency>
<!-- 单独导入 log4j1 -->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
可以看到运行结果:日志打印采用的是
class org.apache.commons.logging.impl.Log4JLogger