Error:'No name attribute provided'- for log4j2.properties customisation
Asked Answered
N

3

6

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

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

The error:

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
Niue answered 12/6, 2019 at 16:7 Comment(0)
B
3

You have no where defined the appender that you have used in your properties file i.e. com.example.controller make sure you have followed below steps for getting your custom logs to your custom file

1.Declare the appender to use them such as

appenders=abhishek

2.Than Define your appender file properties

#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.Than define your logger

logger.abhishek.name = abhishek.CustomLog
logger.abhishek.level = debug
logger.abhishek.additivity = false
logger.abhishek.appenderRef.abhishek.ref = abhishekRollingFile

If you don't want to go with your custom appender follow step 2 & 3 but instead of abhishek use rolling.

This is how you can append as many different log files as you want!

Beezer answered 8/11, 2020 at 12:42 Comment(2)
No luck for me :(Cetacean
Did you restart the tomcat (application server) or reloaded the log4j2 properties file?Beezer
T
2

Change your logger from:

logger.com.example.controller.name = com.example.controller

to

logger.controller.name = com.example.controller or logger.comExampleController.name = com.example.controller

The Logic is

Between logger and name must exist only one word.

I debugged it an see it in Class PropertiesConfigurationBuilder method createLogger

Translative answered 28/10, 2022 at 14:38 Comment(1)
Superb, this worked for meMorganatic
L
0
  • USE the below configuration which will create a 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)

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
Logography answered 15/11, 2019 at 14:19 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.