错误:“未提供名称属性"-用于log4j2.properties定制 [英] Error:'No name attribute provided'- for log4j2.properties customisation

查看:176
本文介绍了错误:“未提供名称属性"-用于log4j2.properties定制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在自定义log4j2.properties以在所需位置生成日志文件. 这样做时,我收到以下错误.

I am customising log4j2.properties to generate log file in desired location. While doing that I am getting the following error.

我的log4j2.priperties文件

My log4j2.priperties file

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

推荐答案

  • 使用以下配置将在提供的路径中创建一个日志文件,并将在14d策略后将其删除.
  • 对于软件包名称声明,请使用此处选择的附加程序(滚动)
  • 创建log4j2.properties文件并声明所需的配置,例如:

    Create log4j2.properties file and declare the required configurtaion,like:

    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
    

    这篇关于错误:“未提供名称属性"-用于log4j2.properties定制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆