Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to configure log4j2.xml log level for specific class only?

Tags:

java

log4j

log4j2

In log4j it's possible to define log levels by package as follows:

    <logger name="org.springframework.web.servlet.mvc.method.annotation" level="info">
        <AppenderRef ref="CONSOLE" />
    </logger>

Questin: how can I define the logging for a specific class only? (eg org.springframework.web.servlet.mvc.method.annotation.EndpointHandlerMapping)? If I just put this into the <logger name property, nothing is logged anymore.

like image 547
membersound Avatar asked Sep 11 '15 08:09

membersound


People also ask

What should be configured to create a custom log file with a different log level?

To define a custom log level in code, use the Level. forName() method. This method creates a new level for the specified name. After a log level is defined you can log messages at this level by calling the Logger.

What is configuration status in Log4j2 xml?

Configuration: the root element of a log4j2 configuration file; the status attribute represents the level at which internal log4j events should be logged. Appenders: this element contains a list of appenders; in our example, an appender corresponding to the System console is defined.


1 Answers

It should work with fully qualified name as well. Logger.name doesnt have to be package/class only but it could be any name which you want to. When you call LoggerFactory.getLogger("MyLogger"), then you have to use

<Logger name="MyLogger" level="info">
  <AppenderRef ref="CONSOLE"/>
</Logger>

Check what is logged when you use logger for package and use logger name from log. What logger name is logged for this class?

like image 107
Mario Doskoc Avatar answered Oct 03 '22 08:10

Mario Doskoc