错误:'未提供名称属性'-用于log4j2.properties定制

问题描述 投票:1回答:1

我正在自定义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
spring-boot log4j2
1个回答
0
投票
 - USE the below configuration which will create log file in the provided path and it will delete it after 14d policy.
 - For the package name declaration use the selected appender here is (rolling)

---log4j2 configuration----

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.policies.size.type = SizeBasedTriggeringPolicy<br/>
appender.rolling.policies.size.size=10KB <br/>
appender.rolling.strategy.type = DefaultRolloverStrategy <br/>
appender.rolling.strategy.max = 2 <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.maxDepth = 1 <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.example.controller
logger.rolling.level = debug
logger.rolling.additivity = false
logger.rolling.appenderRef.rolling.ref = RollingFile
© www.soinside.com 2019 - 2024. All rights reserved.