0

i have this configuration in my log4j2.xml file:

<?xml version="1.0" encoding="UTF-8"?>
<Configuration>
  <Appenders>
    <File name="FileLogger" fileName="HelloLog4j.log" append="false">
      <PatternLayout pattern="%d %t %-5p %c{2} - %m%n"/>
    </File>
    <Console name="STDOUT" target="SYSTEM_OUT">
      <PatternLayout pattern="%d %-5p [%t] %C{2} (%F:%L) - %m%n"/>
    </Console>
  </Appenders>
  <Loggers>
    <Logger name="hello" level="trace">
      <AppenderRef ref="FileLogger"/>
    </Logger>
    <Root level="trace">
      <AppenderRef ref="STDOUT"/>
    </Root>
  </Loggers>
</Configuration>

i put this file in src/resources, and i follow this guide: Guide but when i run my project i have this error:

ERROR StatusLogger No log4j2 configuration file found. Using default configuration: logging only errors to the console.
Remko Popma
  • 35,130
  • 11
  • 92
  • 114
DevOps85
  • 6,473
  • 6
  • 23
  • 42

2 Answers2

1

I would avoid the use of DOMConfigurator, stick to the standard: put log4j2.xml in your classpath. Where that is depends on your app and your runtime environment.

Remko Popma
  • 35,130
  • 11
  • 92
  • 114
0

The XML file has to be inside the src folder itself and not inside src/resources or anything inside it, it has to be at root level of the folder.However if that does not work out here is a slightly different way of doing it.

You can use DOM Configurator to configure the logger manually.

Below is a sample snippet which describers how you can use it.

private static org.apache.log4j.Logger logger;

public static Logger getLogger(){
    if(logger == null){
        logger= LogManager.getLogger(ClassName.class);
        DOMConfigurator.configure("path/to/log4jxml");
    }
    return logger;

}

Class name would be the class in which you are instantiating the class.It is good practice to use Singleton design pattern for this in a custom class and then instantiating it via the getLogger method.Don't forget to make your constructor private.

Edit:

A sample config for console appender.The conversion pattern is date layout.

<appender name="console" class="org.apache.log4j.ConsoleAppender">
    <layout class="org.apache.log4j.PatternLayout">
        <param name="ConversionPattern"
               value="%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n" />
    </layout>
</appender>
<root>
    <level value="INFO" />
    <appender-ref ref="console" />
</root>
Madusudanan
  • 1,017
  • 1
  • 15
  • 36
  • but in web.xml i must add something? – DevOps85 Dec 22 '14 at 16:51
  • This is not related to web.xml.First try to place the log4j2.xml in the src root itself.If that does not work out, then try to use the code that I gave to instantiate the Logger instance.But, this would differ if you are using a build tool such as Maven or something, but either way the code should work. – Madusudanan Dec 22 '14 at 16:52
  • the configuration now work, but does not print anything...i try logger.debug("Hello world - debug log"); logger.info("Hello world - info log"); logger.warn("Hello world - warn log"); – DevOps85 Dec 22 '14 at 16:55
  • Try to call logger.info from the same class from which you created, just to test it out.Now since the classpath issue is resolved, try different configuration files, I have added a sample configuration,check it out. – Madusudanan Dec 22 '14 at 16:58
  • but this DOMConfiguration isn't for log4j 2? – DevOps85 Dec 22 '14 at 18:37
  • The config is Log4j style (older version) but a working example from which I am using, so that you can verify your config.You can refer [here](http://logging.apache.org/log4j/2.0/manual/appenders.html) for more examples. – Madusudanan Dec 23 '14 at 04:05