错误:“未提供名称属性”- 用于 log4j2.properties 自定义

Dav*_*vid 5 java log4j2 spring-boot

我正在自定义 log4j2.properties 以在所需位置生成日志文件。\n在执行此操作时,我收到以下错误。

\n\n

我的 log4j2.properties 文件

\n\n
status = debug\nname= properties_configuration\n\n# Give directory path where log files should get stored\nproperty.basePath = ./log/\n\n# ConsoleAppender will print logs on console\nappender.console.type = Console\nappender.console.name = consoleLogger\nappender.console.target = SYSTEM_OUT\nappender.console.layout.type = PatternLayout\n\n# Specify the pattern of the logs\nappender.console.layout.pattern = %d{yyyy-MM-dd HH:mm:ss.SSS} %level [%t] [%c] [%M] [%l] - %msg%n\n\n\n# RollingFileAppender will print logs in file which can be rotated based on time or size\nappender.rolling.type = RollingFile\nappender.rolling.name = fileLogger\nappender.rolling.fileName= ${basePath}app.log\nappender.rolling.filePattern= ${basePath}app_%d{yyyyMMdd}.log.gz\nappender.rolling.layout.type = PatternLayout\nappender.rolling.layout.pattern = %d{yyyy-MM-dd HH:mm:ss.SSS} %level [%t] [%c] [%M] [%l] - %msg%n\nappender.rolling.policies.type = Policies\n\n# Rotate log file each day and keep 30 days worth\nappender.rolling.policies.time.type = TimeBasedTriggeringPolicy\nappender.rolling.policies.time.interval = 1\nappender.rolling.policies.time.modulate = true\nappender.rolling.strategy.type = DefaultRolloverStrategy\nappender.rolling.strategy.delete.type = Delete\nappender.rolling.strategy.delete.basePath = ${basePath}\nappender.rolling.strategy.delete.maxDepth = 1\nappender.rolling.strategy.delete.ifLastModified.type = IfLastModified\n# Delete files older than 30 days\nappender.rolling.strategy.delete.ifLastModified.age = 30d\n\n# Mention package name here in place of example. Classes in this package or subpackages will use ConsoleAppender and RollingFileAppender for logging\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0 \nlogger.com.example.controller.name = com.example.controller\nlogger.com.example.controller.level = debug\nlogger.com.example.controller.additivity = false\nlogger.com.example.controller.appenderRef.rolling.ref = fileLogger\nlogger.com.example.controller.appenderRef.console.ref = consoleLogger\n\n# Configure root logger for logging error logs in classes which are in package other than above specified package\nrootLogger.level = debug\nrootLogger.additivity = false\nrootLogger.appenderRef.rolling.ref = fileLogger\nrootLogger.appenderRef.console.ref = consoleLogger\n\n
Run Code Online (Sandbox Code Playgroud)\n\n

错误:

\n\n
Exception in thread "main" java.lang.ExceptionInInitializerError\n    at org.apache.commons.logging.LogAdapter$Log4jAdapter.createLog(LogAdapter.java:122)\n    at org.apache.commons.logging.LogAdapter.createLog(LogAdapter.java:89)\n    at org.apache.commons.logging.LogFactory.getLog(LogFactory.java:67)\n    at org.apache.commons.logging.LogFactory.getLog(LogFactory.java:59)\n    at org.springframework.boot.SpringApplication.<clinit>(SpringApplication.java:194)\n    at com.example.redisTemplateDemo.RedisTemplateDemoApplication.main(RedisTemplateDemoApplication.java:38)\nCaused by: org.apache.logging.log4j.core.config.ConfigurationException: **No name attribute provided for Logger com**\n    at org.apache.logging.log4j.core.config.properties.PropertiesConfigurationBuilder.createLogger(PropertiesConfigurationBuilder.java:256)\n    at org.apache.logging.log4j.core.config.properties.PropertiesConfigurationBuilder.build(PropertiesConfigurationBuilder.java:178)\n    at org.apache.logging.log4j.core.config.properties.PropertiesConfigurationFactory.getConfiguration(PropertiesConfigurationFactory.java:52)\n    at org.apache.logging.log4j.core.config.properties.PropertiesConfigurationFactory.getConfiguration(PropertiesConfigurationFactory.java:35)\n    at org.apache.logging.log4j.core.config.ConfigurationFactory$Factory.getConfiguration(ConfigurationFactory.java:454)\n    at org.apache.logging.log4j.core.config.ConfigurationFactory$Factory.getConfiguration(ConfigurationFactory.java:386)\n    at org.apache.logging.log4j.core.config.ConfigurationFactory.getConfiguration(ConfigurationFactory.java:261)\n    at org.apache.logging.log4j.core.LoggerContext.reconfigure(LoggerContext.java:616)\n    at org.apache.logging.log4j.core.LoggerContext.reconfigure(LoggerContext.java:637)\n    at org.apache.logging.log4j.core.LoggerContext.start(LoggerContext.java:231)\n    at org.apache.logging.log4j.core.impl.Log4jContextFactory.getContext(Log4jContextFactory.java:153)\n    at org.apache.logging.log4j.core.impl.Log4jContextFactory.getContext(Log4jContextFactory.java:45)\n    at org.apache.logging.log4j.LogManager.getContext(LogManager.java:194)\n    at org.apache.commons.logging.LogAdapter$Log4jLog.<clinit>(LogAdapter.java:155)\n    ... 6 more\n
Run Code Online (Sandbox Code Playgroud)\n

Tap*_*pan 0

  • 使用以下配置将在提供的路径中创建一个日志文件,并在 14 天策略后将其删除。
  • 对于包名称声明,请使用此处选定的附加程序(滚动)

创建 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
Run Code Online (Sandbox Code Playgroud)