log4j 2 - configuration issue

有些话、适合烂在心里 提交于 2019-11-29 09:28:05

Actually This is a straight forward process. Two main classes of Log4j 2 are as follows that you need to import like this:

    import org.apache.logging.log4j.LogManager
    import org.apache.logging.log4j.Logger

Now get a Logger instance by using this code.

    private static final Logger logger = LogManager.getLogger();

Note I didn't specified the class name to getLogger() method as a parameter. Log4j 2 automatically figures it out.

Now you can use any of the info(), trace(), debug(), warn(), error(), fatal() method from the Logger class. But to get the output from all of these methods you'll need a XML configuration file. By default Log4j 2 only produces output from the error() and fatal() methods.

Configuration file is as follows:

    <?xml version="1.0" encoding="UTF-8"?>
    <Configuration status="WARN">
        <Appenders>
            <File name="MyCustomLogFile" fileName="/home/me/mylogfile.log">
                <PatternLayout>
                    <Pattern>%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n</Pattern>
                </PatternLayout>
            </File>
            <Console name="Console" target="SYSTEM_OUT">
                <PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
            </Console>
        </Appenders>
        <Loggers>
            <Root level="all">
                <AppenderRef ref="MyCustomLogFile"/>
                <!--<AppenderRef ref="Console"/>-->
            </Root>
        </Loggers>
    </Configuration>

Save this file with any name any where. I use Log4j2.xml as name. Now you'll need this file to be put in classpath, which you can do by passing a system property when running the program as follows:

    java -Dlog4j.configurationFile=/path/to/xml/configuration/file/Log4j2.xml MyMainClass

And you've done it. Logging will be right away on your console.

Special Notes:

  • In XML file I've provided 2 appenders: A file and a console. You can see that you just need to uncomment the commented AppenderRef tag to get output in a file instead of console.

  • You can also provide an environment variable as a system property. Log4j 2 will read the configuration file from the environment variable first and then in -D argument if not found an environment variable.

Have fun with logging. :-)

place log4j2.xml file under src/main/resources. It works

you should put your log4j2.xml into the classpath.

or set "log4j.configurationFile" system property to force to use your log4j2.xml

Please refer to: http://logging.apache.org/log4j/2.x/manual/configuration.html#AutomaticConfiguration

It exists in the classpath of the project and I tried putting it in many other directories.

Where exactly? There is often confusion about where "in the classpath" means. It can't just be anywhere. It has to be at the 'top' or the 'default package'.

src
├── main
│   └── java
│       ├── com
│       │   └── example
│       └── log4j2.xml

A tip for eclipse users:

Right click on the project and click "refresh". Make sure you could see the log4j2.xml file in eclipse. (This solved my problem.)

To be verbose:

  • You shouldn't add the file to build path.(If you do, eclipse will warn you about this)

  • The name of this file doesn't appear in '.classpath' file.

  • I put my log4j2.xml under src/ directory. It works.

I'm quite sure that you have to write down the full qualified name of the class whose messages you want to be logged - something like com.application.log4jtest.YourClass. If that doesn't work, try fiddling with the log level.

Also - just as a notice - you can also write

Logger logger = LogManager.getLogger(MyClass.class); // omit .getClass()

I had similar problem. I put the file under src folder and it worked. I did not mention any package name in the log4j2.xml file.

In the documentation of log4j 2: http://logging.apache.org/log4j/2.x/manual/configuration.html#AutomaticConfiguration

"If a JSON file cannot be located the XML ConfigurationFactory will try to locate log4j2.xml on the classpath."

but it is not working with classpath. Instead if we keep it in src folder, then it is working.

<context-param>
    <param-name>log4jConfiguration</param-name>
    <param-value>log4j2.xml</param-value>
</context-param>

similar problem is mentioned here : https://issues.apache.org/jira/browse/LOG4J2-357

I had the problem, I tried some solutions, but only this worked to me:

Go to web.xml, and add this parameter:

<context-param>
    <param-name>log4jConfiguration</param-name>
    <param-value>log4j2.xml</param-value>
</context-param>

Ok, I solved the problem. I had to specify in the xml the package="myPackage"

I also faced the same problem.I kept my log4j2.xml file in System environment variable.

Variable name : sys_logroot Variable value : D:\user\gouse

and no logs are created for me.

use the system variable-Dlog4j.configurationFile=path/to/log4j2.xmlSee here

This solve my problem

I had the same 'ERROR StatusLogger No log4j2 configuration file found. Using default configuration: logging only errors to the console.' message over and over. It made me crazy. The log4j2.xml file was placed correctly at src/main/resources, like i did at thousands of projects before.

The solution in my case was to remove <packaging>pom</packaging> from the root pom.xml. packaging pom causes the content of src/main/resources not to be copied to target/classes.

Happy logging for anyone with the same root cause.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!