How to Configure Log4J with a Properties File

How to set log4j property file?

From "Default Initialization Procedure" at http://logging.apache.org/log4j/1.2/manual.html:

  • Set the resource string variable to the value of the
    log4j.configuration system property. The preferred way to specify the
    default initialization file is through the log4j.configuration system
    property. In case the system property log4j.configuration is not
    defined, then set the string variable resource to its default value
    "log4j.properties".
  • Attempt to convert the resource variable to a URL.
  • If the resource variable cannot be converted to a URL, for example due
    to a MalformedURLException, then search for the resource from the
    classpath by calling
    org.apache.log4j.helpers.Loader.getResource(resource, Logger.class)
    which returns a URL. Note that the string "log4j.properties"
    constitutes a malformed URL. See Loader.getResource(java.lang.String)
    for the list of searched locations.

So you need to prepend file: to log4j.configuration property value in order that it can be treated as URL.

See https://stackoverflow.com/a/7927278/603516.

Even better code:

    System.setProperty("log4j.configuration", new File("resources", "log4j.xml").toURL());

log4j 1 to log4j 2 properties file

The properties file format is not the default configuration format in Log4j 2.x (it was only introduced in 2.4). If you were to use XML, there is an automatic configuration converter in log4j-1.2-api, which can convert your properties file to XML using:

java -cp log4j-1.2-api-2.16.0.jar:log4j-api-2.16.0.jar:log4j-core-2.16.0.jar org.apache.log4j.config.Log4j1ConfigurationConverter -i log4j.properties -o log4j2.xml

(I assume all three jars, log4j-1.2-api, log4j-api and log4j-core are in the current directory). In your case this gives you:

<?xml version="1.0" ?>
<Configuration name="Log4j1">
<Appenders>
<RollingFile
name="rollingFile"
fileName="C:/logs/vds.log"
filePattern="C:/logs/vds.log.%i">
<PatternLayout pattern="%d{yyyy-MM-dd HH:mm:ss} %-5p %m%n" />
<Policies>
<SizeBasedTriggeringPolicy size="10485760" />
</Policies>
<DefaultRolloverStrategy max="1" />
</RollingFile>
</Appenders>
<Loggers>
<Root level="INFO">
<AppenderRef ref="rollingFile" />
</Root>
<Logger
name="org.apache.camel.component.swagger"
level="INFO" />
<Logger
name="org.apache.camel.component.servlet"
level="INFO" />
<Logger
name="org.apache.camel"
level="INFO" />
<Logger
name="com.wordnik.swagger"
level="INFO" />
</Loggers>
</Configuration>

(the indentation is mine). Remark: the max="1" is probably a bug in the converter.

If you insist on using the properties format, your logger configuration should look like:

logger.1.name=com.wordnik.swagger
logger.1.level=INFO
logger.2.name=org.apache.camel.component.servlet
logger.2.level=INFO
logger.3.name=org.apache.camel.component.swagger
logger.3.level=INFO
logger.4.name=org.apache.camel
logger.4.level=INFO

Loading log4j properties from a package in java

If conf is the source folder you can use:

PropertyConfigurator.configure("classpath:conf/log4j.properties");

else you can try this:

PropertyConfigurator.configure(this.getClass().getClassLoader().getResource("conf/log4j.properties"));

How to add path of log4j.properties file in my project classpath in eclipse?

Right click on the folder, select Build Path then Use as a Source Folder.

Just remember that when you will deploy you will need to create a directory for resources and add it to class path.

You should also consider to use maven for your java project, it may seem overkill initially but it will pay off in the long run.

Loading log4j2.xml or properties configuration file during runtime by specifying path

If no LoggerContext is associated with the caller, all these methods have the same effect: they create a LoggerContext and configure it using the configuration source provided.

The 4 methods start to differ if there is a LoggerContext associated with the caller. This can happen if any LogManager's get* method was called before (e.g. in a static initializer). If this happens, the first two methods will replace that context's configuration, while the last two are no-ops.

PropertyConfigurator and DOMConfigurator in Log4j 1.x worked differently: unless you used log4j.reset=true key they modified the previous configuration. Although the two classes have been ported to the newest log4j-1.2-api, the "reset" semantic is not implemented and they behave like Configurator.reconfigure restricted to a single configuration format.

Remark: Configurator.reconfigure tries to guess the configuration format to choose the appropriate ConfigurationFactory. Since both Log4j 2.x and Log4j 1.x have the properties and XML formats, all properties and XML files will interpreted as native Log4j2 configuration files. Check this question for a workaround.



Related Topics



Leave a reply



Submit