log4j配置文件
1.log4j.(mybatis)
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-I1e7mW2N-1627697901370)(E:\java文档\暑假笔记\imge\log4j.properties.png)]
log4j.properties
# Set root category priority to INFO and its only appender to CONSOLE.
#log4j.rootCategory=INFO, CONSOLE debug info warn error fatal
log4j.rootCategory=debug, CONSOLE, LOGFILE
# Set the enterprise logger category to FATAL and its only appender to CONSOLE.
log4j.logger.org.apache.axis.enterprise=FATAL, CONSOLE
# CONSOLE is set to be a ConsoleAppender using a PatternLayout.
log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender
log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout
log4j.appender.CONSOLE.layout.ConversionPattern=%d{ISO8601} %-6r [%15.15t] %-5p %30.30c %x - %m\n
# LOGFILE is set to be a File appender using a PatternLayout.
log4j.appender.LOGFILE=org.apache.log4j.FileAppender
log4j.appender.LOGFILE.File=d:\axis.log
log4j.appender.LOGFILE.Append=true
log4j.appender.LOGFILE.layout=org.apache.log4j.PatternLayout
log4j.appender.LOGFILE.layout.ConversionPattern=%d{ISO8601} %-6r [%15.15t] %-5p %30.30c %x - %m\n
2.(spring)
日志:
pom:
<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>
在Spring项目中使用Log4j2.xml日志
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-VvakDXyT-1627697901373)(E:\java文档\暑假笔记\imge\日志添加的log4j位置.png)]
log4j2.xml
<?xml version="1.0" encoding="UTF-8"?>
<!-- 配置LoggerConfig,即Appenders的日志级别为WARN -->
<Configuration status="info">
<!-- Appenders支持配置多个Appender,支持向不同的目标输送日志,本例为配置向控制台输出 -->
<Appenders>
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout pattern="%-5level[%d{yyyy/MM/dd HH:mm:ss.SSS}] [%t]: \n\t%logger{36} \n\t%msg %n" />
</Console>
</Appenders>
<!-- Loggers支持配置多个Logger,可引用不同的目标Appender,也可根据业务需求定制特定要求的Appender -->
<Loggers>
<!-- 4个级别低->高:debug,info,warning,error -->
<Root level="info">
<AppenderRef ref="Console" />
</Root>
</Loggers>
</Configuration>
slf4j+log4j2的配置
昨天自己测试了一下slf4j+log4j2的配置,可以正常使用,虽然配置十分简单,但好记性不如烂笔头,想想还是记录下来吧。
**运行的环境:**jdk1.7、tomcat7、Maven的web项目
1.在新建的maven项目的pom文件中添加jar包依赖(依赖代码在步骤末尾):
需要添加依赖的jar包有:
slf4j的api接口包:slf4j-api
log4j2的核心包:log4j-core
log4j2的api接口包:log4j-api
slf4j对应log4j2日志框架的驱动包:log4j-slf4j-impl
log4j2的异步日志功能包:com.lmax.disruptor
解决web项目log4j可能出现警告的jar包:log4j-web
2.配置log4j2.xml日志配置文件(日志配置文件内容放在步骤末尾):
·日志配置文件默认读取路径为src/main/resource
·若文件放置在默认读取路径下则无需配置读取路径
·若文件放置到其他路径下则需要在web.xml中设置日志配置文件的读取路径,代码如下:
<context-param>
<param-name>log4jConfiguration</param-name>
<param-value>classpath:log4j2.xml</param-value>
</context-param>
3.在代码中使用日志功能:
[](javascript:void(0)