Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get other than root logger from SLF4J using Log4J

Tags:

java

slf4j

log4j

I am learning Slf4j and log4j in Spring.I have seen that eyerywhere we are using one line

private final Logger logger = LoggerFactory.getLogger(name.class);

I have seen that this is getting root logger by default.

  1. How this is getting root logger?Am I wrong?

  2. How can i get other loggers which are defined in log4j.xml file?

This is my configuration:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE log4j:configuration PUBLIC
  "-//APACHE//DTD LOG4J 1.2//EN" "http://logging.apache.org/log4j/1.2/apidocs/org/apache/log4j/xml/doc-files/log4j.dtd">
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/"
    debug="false">

    <appender name="CONSOLE" class="org.apache.log4j.ConsoleAppender">
        <layout class="org.apache.log4j.PatternLayout">
            <param name="ConversionPattern" value="[%d{dd/MM/yy hh:mm:ss:sss z}] %5p %c{2}: %m%n" />
        </layout>
    </appender>

    <appender name="ASYNC" class="org.apache.log4j.AsyncAppender">
        <appender-ref ref="CONSOLE" />
        <appender-ref ref="FILE" />
    </appender>

    <appender name="FILE" class="org.apache.log4j.RollingFileAppender">

        <param name="File" value="C:/log/spring-hib.log" />
        <param name="MaxBackupIndex" value="100" />

        <layout class="org.apache.log4j.PatternLayout">
            <param name="ConversionPattern" value="[%d{dd/MM/yy hh:mm:ss:sss z}] %5p %c{2}: %m%n" />
        </layout>


    </appender>

  <logger name="com.example.foo">
  <level value="DEBUG"/>
  <appender-ref ref="FooLogFile"/>
 </logger>



    <category name="org.hibernate">
        <priority value="DEBUG" />
    </category>

    <category name="java.sql">
        <priority value="debug" />
    </category>

    <root>
        <priority value="INFO" />
        <appender-ref ref="ASYNC" />
    </root>

</log4j:configuration>
like image 955
beinghuman Avatar asked Sep 01 '13 19:09

beinghuman


People also ask

Can we use log4j and SLF4J together?

So essentially, SLF4J does not replace log4j; they both work together. It removes the dependency on log4j from your application and makes it easy to replace it in the future with the more capable library.

Which Logger is used by SLF4J?

SLF4J supports popular logging frameworks, namely log4j, java. util. logging, Simple logging and NOP. The logback project supports SLF4J natively.

Is SLF4J different from log4j?

Comparison SLF4J and Log4jUnlike log4j, SLF4J (Simple Logging Facade for Java) is not an implementation of logging framework, it is an abstraction for all those logging frameworks in Java similar to log4J. Therefore, you cannot compare both. However, it is always difficult to prefer one between the two.

Can SLF4J work without log4j?

Conclusion. So essentially, SLF4J does not replace Log4j, Both work together. SLF4j removes the tight coupling between the application and logging frameworks. It makes it easy to replace with any other logging framework in the future with a more capable library.


1 Answers

I have seen that this is getting root logger by default

No it is not.

Assume FQN of Name is foo.bar.Name), when you call private final Logger logger = LoggerFactory.getLogger(Name.class); , you are getting a logger with name foo.bar.Name, for which inherits from foo.bar, for which inherits from foo, for which inherits from root.

You can get the root logger in SLF4J by LoggerFactory.getLogger(Logger.ROOT_LOGGER_NAME)

Loggers are hierarchical. Child level logger inherits configurations from its parent (including appenders, level etc). I believe you were confused about "getting a child logger which inherited config from ROOT logger" and "getting the ROOT logger"

2.How can i get other loggers which are defined in log4j.xml file?

For example, if you want to get logger com.example.foo, it is simply by private final Logger logger = LoggerFactory.getLogger("com.example.foo");

As mentioned above, loggers are hierarchical. If you get the logger "com.example.foo.Bar", given there are no specific setting for "com.example.foo.Bar", its behaviour should be the same as using "com.example.foo" (except the logger name being show in log of course).

As it is a common practice to use the class name itself as the logger name, SLF4J also provide a method to get logger by providing a class (as what you do in your question), by Logger logger = LoggerFactory.getLogger(Bar.class);. This make it more refactoring-friendly. In this case, the logger name got will be the same as the FQN of the class provided ("com.example.foo.Bar")

like image 100
Adrian Shum Avatar answered Sep 18 '22 12:09

Adrian Shum