首先,Java日志框架可以分为两类:门面型日志框架和记录型日志框架。
下面主要介绍一下常用的Slf4j门面框架搭配各种记录型日志框架的基础配置方法。
如果我们项目中单纯只引入Slf4j,如下所示。
<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">
<parent>
<artifactId>logartifactId>
<groupId>org.luxinfenggroupId>
<version>1.0-SNAPSHOTversion>
parent>
<modelVersion>4.0.0modelVersion>
<artifactId>slf4jartifactId>
<properties>
<maven.compiler.source>8maven.compiler.source>
<maven.compiler.target>8maven.compiler.target>
properties>
<dependencies>
<dependency>
<groupId>org.slf4jgroupId>
<artifactId>slf4j-apiartifactId>
<version>2.0.6version>
dependency>
dependencies>
project>
在执行日志记录时,会提示如下:即如果没有选定具体的记录型日志框架时,会使用默认的NOP实现,即什么都不输出。
SLF4J: No SLF4J providers were found.
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See https://www.slf4j.org/codes.html#noProviders for further details.
<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">
<parent>
<artifactId>logartifactId>
<groupId>org.luxinfenggroupId>
<version>1.0-SNAPSHOTversion>
parent>
<modelVersion>4.0.0modelVersion>
<artifactId>slf4jartifactId>
<properties>
<maven.compiler.source>8maven.compiler.source>
<maven.compiler.target>8maven.compiler.target>
properties>
<dependencies>
<dependency>
<groupId>org.slf4jgroupId>
<artifactId>slf4j-apiartifactId>
<version>2.0.6version>
dependency>
<dependency>
<groupId>org.slf4jgroupId>
<artifactId>slf4j-simpleartifactId>
<version>2.0.6version>
dependency>
dependencies>
project>
slf4j-simple是一个用于在Java程序中实现简单日志记录的组件,它使用简单的文本格式记录日志消息,并且不支持配置文件或其他高级功能,因此非常适合调试和开发环境。但是,如果需要对生产环境中的日志进行更严格的管理和控制,则可能需要使用其他更加复杂的日志实现。(slf4j-simple只支持INFO及以上级别的日志展示),示例如下。
public static void main(String[] args) {
Logger logger = LoggerFactory.getLogger(Slf4jWithoutImpl.class);
logger.info("Info message");
logger.error("Error message");
logger.warn("Warn message");
logger.debug("Debug message");
}
输出为
[main] INFO slf4j.Slf4jWithoutImpl - Info message
[main] ERROR slf4j.Slf4jWithoutImpl - Error message
[main] WARN slf4j.Slf4jWithoutImpl - Warn message
<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">
<parent>
<artifactId>logartifactId>
<groupId>org.luxinfenggroupId>
<version>1.0-SNAPSHOTversion>
parent>
<modelVersion>4.0.0modelVersion>
<artifactId>slf4jartifactId>
<properties>
<maven.compiler.source>8maven.compiler.source>
<maven.compiler.target>8maven.compiler.target>
properties>
<dependencies>
<dependency>
<groupId>org.slf4jgroupId>
<artifactId>slf4j-apiartifactId>
<version>2.0.6version>
dependency>
<dependency>
<groupId>org.slf4jgroupId>
<artifactId>slf4j-jdk14artifactId>
<version>2.0.6version>
dependency>
dependencies>
project>
输出
二月 11, 2023 11:31:03 上午 slf4j.Slf4jWithoutImpl main
信息: Info message
二月 11, 2023 11:31:03 上午 slf4j.Slf4jWithoutImpl main
严重: Error message
二月 11, 2023 11:31:03 上午 slf4j.Slf4jWithoutImpl main
警告: Warn message
通过配置文件可以修改日志的展示形式。
创建配置文件logging.properties,然后在VM Options加上参数:-Djava.util.logging.config.file=C:\Users\123456\IdeaProjects\log\slf4j\src\main\resources\logging.properties(配置文件的绝对日志)
注意:java.util.logging中的日志等级与我们常用的有所区别,比如SEVERE对应的是ERROR,详情可见:https://ost.51cto.com/posts/235
handlers = java.util.logging.ConsoleHandler
java.util.logging.ConsoleHandler.level = SEVERE
java.util.logging.ConsoleHandler.formatter = java.util.logging.SimpleFormatter
java.util.logging.SimpleFormatter.format= [%1$tF %1$tT] [%4$-7s] %3$s %5$s %n
修改配置文件后输出为:
[2023-02-11 21:49:46] [严重 ] slf4j.Slf4jWithoutImpl Error message
<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">
<parent>
<artifactId>logartifactId>
<groupId>org.luxinfenggroupId>
<version>1.0-SNAPSHOTversion>
parent>
<modelVersion>4.0.0modelVersion>
<artifactId>slf4jartifactId>
<properties>
<maven.compiler.source>8maven.compiler.source>
<maven.compiler.target>8maven.compiler.target>
properties>
<dependencies>
<dependency>
<groupId>org.slf4jgroupId>
<artifactId>slf4j-apiartifactId>
<version>2.0.6version>
dependency>
<dependency>
<groupId>ch.qos.logbackgroupId>
<artifactId>logback-classicartifactId>
<version>1.3.5version>
dependency>
dependencies>
project>
输出:
22:09:50.477 [main] INFO slf4j.Slf4jWithoutImpl - Info message
22:09:50.477 [main] ERROR slf4j.Slf4jWithoutImpl - Error message
22:09:50.477 [main] WARN slf4j.Slf4jWithoutImpl - Warn message
配置文件
生成logback.xml文件,并放置到resources目录下。
日志级别:OFF、FATAL、ERROR、WARN、INFO、DEBUG、ALL
<configuration debug="false">
<property name="LOG_HOME" value="/home" />
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%npattern>
encoder>
appender>
<appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<FileNamePattern>${LOG_HOME}/TestWeb.log.%d{yyyy-MM-dd}.logFileNamePattern>
<MaxHistory>30MaxHistory>
rollingPolicy>
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%npattern>
encoder>
<triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
<MaxFileSize>10MBMaxFileSize>
triggeringPolicy>
appender>
<root level="WARN">
<appender-ref ref="STDOUT" />
<appender-ref ref="FILE"/>
root>
configuration>
输出:
2023-02-11 22:22:35.547 [main] ERROR slf4j.Slf4jWithoutImpl - Error message
2023-02-11 22:22:35.547 [main] WARN slf4j.Slf4jWithoutImpl - Warn message
<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">
<parent>
<artifactId>logartifactId>
<groupId>org.luxinfenggroupId>
<version>1.0-SNAPSHOTversion>
parent>
<modelVersion>4.0.0modelVersion>
<artifactId>slf4jartifactId>
<properties>
<maven.compiler.source>8maven.compiler.source>
<maven.compiler.target>8maven.compiler.target>
properties>
<dependencies>
<dependency>
<groupId>org.slf4jgroupId>
<artifactId>slf4j-apiartifactId>
<version>2.0.6version>
dependency>
<dependency>
<groupId>org.apache.logging.log4jgroupId>
<artifactId>log4j-apiartifactId>
<version>2.9.1version>
dependency>
<dependency>
<groupId>org.apache.logging.log4jgroupId>
<artifactId>log4j-coreartifactId>
<version>2.9.1version>
dependency>
<dependency>
<groupId>org.slf4jgroupId>
<artifactId>slf4j-log4j12artifactId>
<version>2.0.6version>
dependency>
dependencies>
project>
配置文件:
在resources文件夹下创建文件:log4j.properties文件,文件内容为
log4j.rootLogger=ALL, stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d %p [%c] - %m%n
输出:
2023-02-11 23:00:49,407 INFO [slf4j.Slf4jWithoutImpl] - Info message
2023-02-11 23:00:49,407 ERROR [slf4j.Slf4jWithoutImpl] - Error message
2023-02-11 23:00:49,407 WARN [slf4j.Slf4jWithoutImpl] - Warn message