我正在自定义log4j2.properties以在所需位置生成日志文件。 这样做时,我收到以下错误。
我的log4j2.priperties文件
status = debug
name= properties_configuration
# Give directory path where log files should get stored
property.basePath = ./log/
# ConsoleAppender will print logs on console
appender.console.type = Console
appender.console.name = consoleLogger
appender.console.target = SYSTEM_OUT
appender.console.layout.type = PatternLayout
# Specify the pattern of the logs
appender.console.layout.pattern = %d{yyyy-MM-dd HH:mm:ss.SSS} %level [%t] [%c] [%M] [%l] - %msg%n
# RollingFileAppender will print logs in file which can be rotated based on time or size
appender.rolling.type = RollingFile
appender.rolling.name = fileLogger
appender.rolling.fileName= ${basePath}app.log
appender.rolling.filePattern= ${basePath}app_%d{yyyyMMdd}.log.gz
appender.rolling.layout.type = PatternLayout
appender.rolling.layout.pattern = %d{yyyy-MM-dd HH:mm:ss.SSS} %level [%t] [%c] [%M] [%l] - %msg%n
appender.rolling.policies.type = Policies
# Rotate log file each day and keep 30 days worth
appender.rolling.policies.time.type = TimeBasedTriggeringPolicy
appender.rolling.policies.time.interval = 1
appender.rolling.policies.time.modulate = true
appender.rolling.strategy.type = DefaultRolloverStrategy
appender.rolling.strategy.delete.type = Delete
appender.rolling.strategy.delete.basePath = ${basePath}
appender.rolling.strategy.delete.maxDepth = 1
appender.rolling.strategy.delete.ifLastModified.type = IfLastModified
# Delete files older than 30 days
appender.rolling.strategy.delete.ifLastModified.age = 30d
# Mention package name here in place of example. Classes in this package or subpackages will use ConsoleAppender and RollingFileAppender for logging
logger.com.example.controller.name = com.example.controller
logger.com.example.controller.level = debug
logger.com.example.controller.additivity = false
logger.com.example.controller.appenderRef.rolling.ref = fileLogger
logger.com.example.controller.appenderRef.console.ref = consoleLogger
# Configure root logger for logging error logs in classes which are in package other than above specified package
rootLogger.level = debug
rootLogger.additivity = false
rootLogger.appenderRef.rolling.ref = fileLogger
rootLogger.appenderRef.console.ref = consoleLogger
错误:
Exception in thread "main" java.lang.ExceptionInInitializerError
at org.apache.commons.logging.LogAdapter$Log4jAdapter.createLog(LogAdapter.java:122)
at org.apache.commons.logging.LogAdapter.createLog(LogAdapter.java:89)
at org.apache.commons.logging.LogFactory.getLog(LogFactory.java:67)
at org.apache.commons.logging.LogFactory.getLog(LogFactory.java:59)
at org.springframework.boot.SpringApplication.<clinit>(SpringApplication.java:194)
at com.example.redisTemplateDemo.RedisTemplateDemoApplication.main(RedisTemplateDemoApplication.java:38)
Caused by: org.apache.logging.log4j.core.config.ConfigurationException: **No name attribute provided for Logger com**
at org.apache.logging.log4j.core.config.properties.PropertiesConfigurationBuilder.createLogger(PropertiesConfigurationBuilder.java:256)
at org.apache.logging.log4j.core.config.properties.PropertiesConfigurationBuilder.build(PropertiesConfigurationBuilder.java:178)
at org.apache.logging.log4j.core.config.properties.PropertiesConfigurationFactory.getConfiguration(PropertiesConfigurationFactory.java:52)
at org.apache.logging.log4j.core.config.properties.PropertiesConfigurationFactory.getConfiguration(PropertiesConfigurationFactory.java:35)
at org.apache.logging.log4j.core.config.ConfigurationFactory$Factory.getConfiguration(ConfigurationFactory.java:454)
at org.apache.logging.log4j.core.config.ConfigurationFactory$Factory.getConfiguration(ConfigurationFactory.java:386)
at org.apache.logging.log4j.core.config.ConfigurationFactory.getConfiguration(ConfigurationFactory.java:261)
at org.apache.logging.log4j.core.LoggerContext.reconfigure(LoggerContext.java:616)
at org.apache.logging.log4j.core.LoggerContext.reconfigure(LoggerContext.java:637)
at org.apache.logging.log4j.core.LoggerContext.start(LoggerContext.java:231)
at org.apache.logging.log4j.core.impl.Log4jContextFactory.getContext(Log4jContextFactory.java:153)
at org.apache.logging.log4j.core.impl.Log4jContextFactory.getContext(Log4jContextFactory.java:45)
at org.apache.logging.log4j.LogManager.getContext(LogManager.java:194)
at org.apache.commons.logging.LogAdapter$Log4jLog.<clinit>(LogAdapter.java:155)
... 6 more
答案 0 :(得分:0)
创建log4j2.properties文件并声明所需的配置,例如:
name = ABC <br/>
appender.rolling.type = RollingFile<br/>
appender.rolling.name = RollingFile<br/>
appender.rolling.fileName = ABClog/ABCapplication.log<br/>
appender.rolling.filePattern = ABClog/ABCapplication.%d{dd-MMM}.log<br/>
appender.rolling.layout.type = PatternLayout<br/>
appender.rolling.layout.pattern=%d{yyyy-MM-dd HH:mm:ss.SSS} %level [%t] [%c] [%M] [%l]- %msg%n<br/>
appender.rolling.policies.type = Policies<br/>
appender.rolling.strategy.type = DefaultRolloverStrategy <br/>
appender.rolling.strategy.action.type = Delete <br/>
appender.rolling.strategy.action.basepath =log <br/>
appender.rolling.strategy.action.condition.type = IfFileName<br/>
appender.rolling.strategy.action.condition.glob = ABC*.log<br/>
appender.rolling.strategy.action.ifAny.type = IfAny<br/>
appender.rolling.strategy.action.ifAny.ifLastModified.type = IfLastModified<br/>
appender.rolling.strategy.action.ifAny.ifLastModified.age = 14d<br/>
appender.rolling.strategy.action.ifAny.ifAccumulatedFileSize.type = IfAccumulatedFileSize<br/>
appender.rolling.strategy.action.ifAny.ifAccumulatedFileSize.exceeds = 200MB<br/>
appender.rolling.policies.time.type = TimeBasedTriggeringPolicy
appender.rolling.policies.time.interval = 1
appender.rolling.policies.time.modulate = true
logger.rolling.name = com.currentobject.controller
logger.rolling.level = debug
logger.rolling.additivity = false
logger.rolling.appenderRef.rolling.ref = RollingFile
答案 1 :(得分:0)
您没有在哪里定义您在属性文件中使用过的附加程序,即com.example.controller
确保已按照以下步骤将自定义日志保存到自定义文件中
1。声明附加程序以使用它们,例如
appenders=abhishek
2。比定义您的附加文件属性
#Define rolling file appender for abhishek
appender.abhishek.type = RollingFile
appender.abhishek.name = abhishekRollingFile
appender.abhishek.fileName = ${filename}/abhishek.log
appender.abhishek.filePattern = ${filename}/abhishek/abhishek.%d{dd-MMM}.log.gz
appender.abhishek.layout.type = PatternLayout
appender.abhishek.layout.pattern = %t [%-5p] %d - %m%n
appender.abhishek.policies.type = Policies
appender.abhishek.policies.time.type = TimeBasedTriggeringPolicy
appender.abhishek.policies.time.interval = 1
appender.abhishek.policies.time.modulate = true
appender.abhishek.policies.size.type = SizeBasedTriggeringPolicy
appender.abhishek.policies.size.size = 128MB
appender.abhishek.strategy.type = DefaultRolloverStrategy
appender.abhishek.strategy.max = 5
3。比定义您的记录器
logger.abhishek.name = abhishek.CustomLog
logger.abhishek.level = debug
logger.abhishek.additivity = false
logger.abhishek.appenderRef.abhishek.ref = abhishekRollingFile
如果您不想使用自定义附加程序,请执行步骤2和3 ,但不要使用abhishek
来使用rolling
。
这是您可以根据需要附加尽可能多的不同日志文件的方法!