环境准备
开发工具:IntelliJ IDEA
项目构建工具:maven 3.6.0
JDK版本:1.8
代码地址
log4jdemo
log4j2demo
logbackdemo
logdemo
log4j
pom.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>org.example</groupId>
<artifactId>log4jdemo</artifactId>
<version>1.0-SNAPSHOT</version>
<name>log4jdemo</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>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
</dependencies>
</project>
Main.java
package com.mac2099.log4j;
import org.apache.log4j.Logger;
/**
* Hello world!
*/
public class Main {
static final Logger LOGGER = Logger.getLogger(Main.class);
public static void main(String[] args) {
LOGGER.info("log4j demo");
}
}
log4j.properties
log4j.rootLogger=INFO,console
log4j.appender.console=org.apache.log4j.ConsoleAppender
log4j.appender.console.Target=System.out
log4j.appender.console.layout=org.apache.log4j.PatternLayout
log4j.appender.console.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} [%p] %c: %m%n
运行结果
2021-10-11 22:21:20 [INFO] com.mac2099.log4j.Main: log4j demo
log4j.properties打印日志到控制台和磁盘文件的配置参考
# 日志级别,appendName1,appendName2,......
log4j.rootLogger=INFO,console,logFile
#打印日志到控制台
log4j.appender.console=org.apache.log4j.ConsoleAppender
log4j.appender.console.layout=org.apache.log4j.PatternLayout
log4j.appender.console.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} [%p] %c: %m%n
#打印日志到/Users/mac2099/temp/mylog.log
log4j.appender.logFile=org.apache.log4j.FileAppender
log4j.appender.logFile.Threshold=DEBUG
log4j.appender.logFile.ImmediateFlush=true
log4j.appender.logFile.Append=true
log4j.appender.logFile.File=/Users/mac2099/temp/mylog.log
log4j.appender.logFile.layout=org.apache.log4j.PatternLayout
log4j.appender.logFile.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} [%p] %c: %m%n
log4j参考:
Log4j.properties配置详解-cnblogs
Log4j.properties配置详解
log4j2
pom.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>org.example</groupId>
<artifactId>log4j2demo</artifactId>
<version>1.0-SNAPSHOT</version>
<name>log4j2demo</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>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.12.1</version>
</dependency>
</dependencies>
</project>
Main.java
package com.mac2099.log4j2;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
/**
* Hello world!
*/
public class Main {
static final Logger LOGGER = LogManager.getLogger(Main.class);
public static void main(String[] args) {
LOGGER.info("log4j2 demo");
}
}
log4j2.properties
rootLogger.level=info
rootLogger.appenderRef.stdout.ref=STDOUT
appender.console.type=Console
appender.console.name=STDOUT
appender.console.layout.type=PatternLayout
appender.console.layout.pattern=%d{yyyy-MM-dd HH:mm:ss} [%p] %c: %m%n
运行结果
2021-10-11 22:35:09 [INFO] com.mac2099.log4j2.Main: log4j2 demo
log4j2参考:
log4j2详细配置参考
name=PropertiesConfig
# 定义变量。指定日志文件的位置和文件名称,以便记录多份日志时,直接引用
property.fileName=ctms
property.fileDir=../logs
property.filePath=${fileDir}/${fileName}.log
appenders=console, rolling
# rootLogger, 根记录器,所有记录器的父辈
# 指定根日志的级别
rootLogger.level=info
# 指定输出的appender引用
rootLogger.appenderRef.stdout.ref=Stdout
rootLogger.appenderRef.rolling.ref=InfoRollingFile
# console
# 指定输出源的类型与名称
appender.console.type=Console
appender.console.name=Stdout
appender.console.layout.type=PatternLayout
# 输出模板
appender.console.layout.pattern=%highlight{%d{yyyy-MM-dd HH:mm:ss.SSS} [%5p] [%t] [%l] - %m%n}{FATAL=white, ERROR=red, WARN=blue, INFO=Green, DEBUG=Yellow, TRACE=blue}
# rolling file
appender.rolling.type=RollingFile
appender.rolling.name=InfoRollingFile
appender.rolling.fileName=${filePath}
# 指定当发生Rolling时,文件的转移和重命名规则
appender.rolling.filePattern=${fileDir}/${fileName}_%d{yyyy-MM-dd}_%i.log
appender.rolling.layout.type=PatternLayout
appender.rolling.layout.pattern=%highlight{%d{yyyy-MM-dd HH:mm:ss.SSS} [%5p] [%t] [%l] - %m%n}{FATAL=white, ERROR=red, WARN=blue, INFO=Green, DEBUG=Yellow, TRACE=blue}
# 指定记录文件的封存策略,该策略主要是完成周期性的日志文件封存工作
appender.rolling.policies.type=Policies
# 基于时间的触发策略
appender.rolling.policies.time.type=TimeBasedTriggeringPolicy
# 当前记录周期为每1h生成一个文件
appender.rolling.policies.time.interval=1
appender.rolling.policies.time.modulate=true
# 基于日志文件体积的触发策略
appender.rolling.policies.size.type=SizeBasedTriggeringPolicy
# 当日志文件体积大于size指定的值时,触发Rolling
appender.rolling.policies.size.size=20M
# 文件封存的覆盖策略
appender.rolling.strategy.type=DefaultRolloverStrategy
# 生成分割(封存)文件的个数
appender.rolling.strategy.max=100
logback
pom.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>org.example</groupId>
<artifactId>logbackdemo</artifactId>
<version>1.0-SNAPSHOT</version>
<name>logbackdemo</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>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.2.3</version>
</dependency>
</dependencies>
</project>
Main.java
package com.mac2099.logback;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* Hello world!
*/
public class Main {
static final Logger LOGGER = LoggerFactory.getLogger(Main.class);
public static void main(String[] args) {
LOGGER.info("logback demo");
}
}
logback.xml
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<!--控制台日志, 控制台输出 -->
<appender name="console" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%d{yyyy-MM-dd HH:mm:ss} [%p] %c: %m%n</pattern>
</encoder>
</appender>
<!-- 日志输出级别 -->
<root level="debug">
<appender-ref ref="console"/>
</root>
</configuration>
运行结果
2021-10-11 22:40:35 [INFO] com.mac2099.logback.Main: logback demo