0

I'm using log4j to log into LogOutput.log. I'm getting below error. Can anyone explain the logic. I'm new to logging

'Exception in thread "main" java.lang.ExceptionInInitializerError
Caused by: org.apache.logging.log4j.core.config.ConfigurationException: No name attribute provided for Appender layout
at org.apache.logging.log4j.core.config.properties.PropertiesConfigurationBuilder.createAppender(PropertiesConfigurationBuilder.java:212)
at org.apache.logging.log4j.core.config.properties.PropertiesConfigurationBuilder.build(PropertiesConfigurationBuilder.java:158)
at org.apache.logging.log4j.core.config.properties.PropertiesConfigurationFactory.getConfiguration(PropertiesConfigurationFactory.java:56)
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:532)
at org.apache.logging.log4j.core.config.ConfigurationFactory$Factory.getConfiguration(ConfigurationFactory.java:456)
at org.apache.logging.log4j.core.config.ConfigurationFactory.getConfiguration(ConfigurationFactory.java:318)
at org.apache.logging.log4j.core.LoggerContext.reconfigure(LoggerContext.java:687)
at org.apache.logging.log4j.core.LoggerContext.reconfigure(LoggerContext.java:708)
at org.apache.logging.log4j.core.LoggerContext.start(LoggerContext.java:263)
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.logging.log4j.spi.AbstractLoggerAdapter.getContext(AbstractLoggerAdapter.java:138)
at org.apache.logging.log4j.jcl.LogAdapter.getContext(LogAdapter.java:39)
at org.apache.logging.log4j.spi.AbstractLoggerAdapter.getLogger(AbstractLoggerAdapter.java:48)
at org.apache.logging.log4j.jcl.LogFactoryImpl.getInstance(LogFactoryImpl.java:40)
at org.apache.logging.log4j.jcl.LogFactoryImpl.getInstance(LogFactoryImpl.java:55)
at org.apache.commons.logging.LogFactory.getLog(LogFactory.java:655)
at userinterface.PizzaShoppie.<clinit>(PizzaShoppie.java:17)'

here is my properties file

name=LoggerConfigFile

rootlogger.level=DEBUG
rootlogger.appenderRef.file.ref=LoggerAppender

appender.file.name=LoggerAppender
appender.file.type=FILE
appender.file.fileName=log/LogOutput.log

appender.layout.type=PatternLayout
appender.layout.pattern=%d{dd-mmm-yyyy} %level -%m%n

          

1 Answers1

1

TL;DR: replace appender.layout with appender.file.layout.

The purpose of a Log4j 2 configuration file is to build a Configuration object with its tree of subcomponents. Whenever you have a simple JavaBean property like "name" you can use:

<component prefix>.name=My configuration name

For properties that are components you use the syntax:

<component prefix>.<unique identifier>.type=<plugin's name>

to set the property on the component. FileAppender's plugin name is "File", therefore you can write (you use "file" as identifier, but it can be anything, e.g. "1"):

appender.1.type=File

to instantiate it and then you need to configure all JavaBean properties of the FileAppender by prepending the prefix appender.1. to the property name:

appender.1.name=LoggerAppender
appender.1.fileName=log/LogOutput.log
...

The same rules apply to nested components of the appender:

appender.1.layout.type=PatternLayout

and so on recursively.

Piotr P. Karwasz
  • 12,857
  • 3
  • 20
  • 43