[J2EE:中间件]LOG4J配置文件(log4.properties)详解
前置条件
Maven:依赖
<!-- 日志 --> <!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-api --> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> <version>1.7.25</version> </dependency> <!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-log4j12 --> <!-- slf4j与log4j的整合jar包 : 其将自动引入其log4j-1.2.17.jar --> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> <version>1.7.25</version> </dependency>
log4j.properties
########################################################################################### # 注意事项: # 1 出现此类异常的可能原因 # [异常信息]↓ # log4j:WARN No appenders could be found for logger (org.example.XXXXTest). # log4j:WARN Please initialize the log4j system properly. # log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info. # [原因/方案] # 1)【根前缀】必须是 log4j,而非log。 # 2)构建/编译后的target/classes目录下是否存在 【log4j[2].properties/xml】文件 ########################################################################################### ### Global logging configuration ### #################################### ### 根日志器(rootLogger) : 新的使用名称,对应Logger类 ### # 配置: (继承的)子日志器的默认日志级别 + 自定义的日志输出源(Appender) # 日志级别: OFF, FATAL, ERROR, WARN, INFO, DEBUG, ALL # 自定义的输出源: CONSOLE, stdout, logfile, 其他自定义名字 # 示例: # log4j.rootLogger=ERROR,file,stdout 表示,日志级别为ERROR的日志输出到控制台(stdout)和file中 log4j.rootLogger=ALL, stdout, ERROR_FILE ### 根目录(rootCategory) : 旧的使用名称,对应原来的Category类 ### #log4j.rootCategory=ERROR, LOGFILE ### log4j.category.* : 对自定义类的设置,可对类、包和工程单独设置 #log4j.category.org.springframework=error #log4j.category.org.apache=error ### 日志器继承状态 additivity # 意义: 子Logger是否继承父Logger的输出源[appender]的标志位 # 默认: [true]子Logger会继承父Logger的appender (即 子Logger会在父Logger的appender里输出) log4j.additivity=false #log4j.additivity.com.example.user.service=false ### 配置: 具体Java包的日志级别 ### ################################ log4j.logger.org.apache=INFO log4j.logger.org.springframework=warn log4j.logger.org.hibernate=ERROR # mybatis log4j.logger.com.ibatis=DEBUG log4j.logger.com.ibatis.common.jdbc.SimpleDataSource=DEBUG log4j.logger.com.ibatis.common.jdbc.ScriptRunner=DEBUG log4j.logger.com.ibatis.sqlmap.engine.impl.SqlMapClientDelegate=DEBUG # sql log4j.logger.java.sql.ResultSet=INFO log4j.logger.java.sql.Connection=DEBUG log4j.logger.java.sql.Statement=DEBUG log4j.logger.java.sql.PreparedStatement=DEBUG ### 配置: 输出源 ### ################### ### 控制台(ConsoleAppender) ### log4j.appender.stdout=org.apache.log4j.ConsoleAppender # ↓Threshold=WARN:指定日志信息的最低输出级别,默认DEBUG log4j.appender.stdout.Threshold=DEBUG # ↓Target=System.err:默认值是System.out,可选值:System.err / System.out / ... log4j.appender.stdout.Target=System.out log4j.appender.stdout.layout=org.apache.log4j.PatternLayout # Pattern to output the caller‘s file name and line number. log4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} [%p] [%l] - %m%n ### 文件(org.apache.log4j.FileAppender) ### # org.apache.log4j.DailyRollingFileAppender(特有属性:datePattern / 可实现每天单独产生1个日志文件) # [extends FileAppender] # org.apache.log4j.RollingFileAppender(特有属性:MaxBackupIndex / MaxFileSize) # [extends FileAppender] log4j.appender.ERROR_FILE=org.apache.log4j.DailyRollingFileAppender log4j.appender.ERROR_FILE.name=LogDemo_File # ↓Threshold=WARN:指定日志信息的最低输出级别,默认DEBUG log4j.appender.ERROR_FILE.Threshold=ERROR # ↓ D:/project.log | ../logs/logs.log | /example.log[项目根目录] | example.log[classes目录下] log4j.appender.ERROR_FILE.File=logs/error.log log4j.appender.ERROR_FILE.Encoding=UTF-8 log4j.appender.ERROR_FILE.Append=true log4j.appender.ERROR_FILE.layout=org.apache.log4j.PatternLayout log4j.appender.ERROR_FILE.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} [%p] [%l] - %m%n # ↓Keep one backup file #log4j.appender.FILE.MaxBackupIndex=5 [org.apache.log4j.RollingFileAppender 特有属性] #log4j.appender.FILE.MaxFileSize=100KB [org.apache.log4j.RollingFileAppender 特有属性] log4j.appender.ERROR_FILE.DatePattern=‘_‘yyyy-MM-dd-HH‘.log‘
相关推荐
chw0 2020-11-04
sdaq 2020-07-26
MrLiar 2020-07-07
sdaq 2020-06-16
丨Fanny丨Cri 2020-06-13
CXC0 2020-06-08
丨Fanny丨Cri 2020-06-03
丨Fanny丨Cri 2020-05-17
JavaWinner 2020-05-10
JavaWinner 2020-05-04
CXC0 2020-04-20
CXC0 2020-04-10
丨Fanny丨Cri 2020-02-18
MrLiar 2020-02-16
CXC0 2020-02-09