30

I am running an example using log4j 2.0-rc1 and log4j.properties file, but log4j lib always runs it with the default configuration (log level, appender, etc). I also tried changing the name to log4j2.properties and nothing happened.

xav
  • 5,452
  • 7
  • 48
  • 57
emanuell
  • 303
  • 1
  • 3
  • 4

3 Answers3

31

Log4j 2 doesn't support the Log4j v1 ".properties" format anymore (yet, since v2.4, Log4j supports a Property format, but its syntax is totally different from v1 format). New formats are XML, JSON, and YAML, see the documentation (note: if you used one of these formats in a file called ".properties", it may be confusing).

To specify the location of your configuration file, do you use the system property log4j.configurationFile, the Log4j class ConfigurationFactory, or something else? Did you read this manual page? It explains that: Although the Log4j 2 configuration syntax is different than that of Log4j 1.x, most, if not all, of the same functionality is available.

So it seems that a legacy Log4j1.x log4j.propertiesfile is not supported as is, it must be migrated to v2.x format. The migration seems quite easy though, looking at the example in the link I gave above. Here is an extract:

Example of Log4j v1.x config file:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE log4j:configuration PUBLIC "-//APACHE//DTD LOG4J 1.2//EN" "log4j.dtd">
<log4j:configuration xmlns:log4j='http://jakarta.apache.org/log4j/'>
    <appender name="STDOUT" class="org.apache.log4j.ConsoleAppender">
        <layout class="org.apache.log4j.PatternLayout">
            <param name="ConversionPattern" value="%d %-5p [%t] %C{2} (%F:%L) - %m%n"/>
        </layout>
    </appender>
    <category name="org.apache.log4j.xml">
        <priority value="info" />
    </category>
    <Root>
        <priority value ="debug" />
        <appender-ref ref="STDOUT" />
    </Root>
</log4j:configuration>

Same config file migrated to Log4j v2:

<?xml version="1.0" encoding="UTF-8"?>
<Configuration>
    <Appenders>
        <Console name="STDOUT" target="SYSTEM_OUT">
            <PatternLayout pattern="%d %-5p [%t] %C{2} (%F:%L) - %m%n"/>
        </Console>
    </Appenders>
    <Loggers>
    <Logger name="org.apache.log4j.xml" level="info"/>
        <Root level="debug">
            <AppenderRef ref="STDOUT"/>
        </Root>
    </Loggers>
</Configuration>
xav
  • 5,452
  • 7
  • 48
  • 57
  • 1
    where should this go / what should the file be named for log4j2? – Don Cheadle Jan 15 '15 at 18:03
  • As explained [here](http://logging.apache.org/log4j/2.x/manual/configuration.html), it may be named "log4j2.yaml", "log4j2.json", or "log4j2.xml", depending on used format. You can use any file name and any file location you want (file extension seems to have to be compliant though), as long as you give this name/location in your Log4j configuration – xav Jan 15 '15 at 20:13
  • 3
    Log4j 2 now supports .properties format: https://logging.apache.org/log4j/2.0/manual/configuration.html#Properties – Drew Oct 15 '15 at 21:45
  • 1
    @Drew Thanks, I updated my answer to reflect this (Property format is back but with a different syntax compared to Log4j v1) – xav Oct 15 '15 at 22:04
  • @xav Am I the only one who thought this change confusing (using a configuration file property, to tell where a configuration file is -- instead of a .property file)? I spent over an hour trying to find the answer to this question for a project, when the answer was right in front of me- but I didn't understand it. – Timothy Steele Jan 21 '16 at 20:56
  • @TimothySteele I'm not sure I understand what you mean when you say _"using a configuration file property, to tell where a configuration file is"_: are you talking about the `log4j.configurationFile` system property? This system property is not the only way to specify to Log4j the configuration file to use: https://logging.apache.org/log4j/2.0/manual/configuration.html#AutomaticConfiguration – xav Jan 21 '16 at 21:53
  • Please check answer below, As of version 2.4, Log4J2 does now, again, support .property files, so currently accepted answer is a bit obsolete. – tarunkumar Jun 16 '16 at 19:15
  • @tarunkumar : Yes, it does support .property files, but it's not the same syntax as Log4j v1, as explained in Log4j v2 documentation (that's what I indicated at the beginning of my answer). I will add a comment on the other Answer just in case – xav Jun 16 '16 at 20:43
  • @TimothySteele this whole change is so unnecessary and bloated! They should have at least maintained some level of backward compatibility. This change is not easy as xav mentions in his answer – Navin Israni Jun 26 '17 at 16:55
16

As of version 2.4, Log4J2 does now, again, support .property files. See here in the documentation for property configuration.

Configuration with Properties

As of version 2.4, Log4j now supports configuration via properties files. Note that the property syntax is NOT the same as the syntax used in Log4j 1. Like the XML and JSON configurations, properties configurations define the configuration in terms of plugins and attributes to the plugins.

Jack Hughes
  • 181
  • 1
  • 5
  • 4
    Be careful readers, as explained in the documentation excerpt in this answer, the format to use in this file is not the same as in Log4j v1 (I often receive comments that my answer is wrong and this one is right) – xav Jun 16 '16 at 20:49
4

Log4j 2 uses a new configuration file format. You need to use XML (default), JSON (with additional libraries), or even YAML (again, libraries). Check out the documentation.

Matt
  • 637
  • 3
  • 10