简体   繁体   English

如何使用 Log4J 从 SLF4J 获取除根记录器以外的其他内容

[英]How to get other than root logger from SLF4J using Log4J

I am learning Slf4j and log4j in Spring.I have seen that eyerywhere we are using one line我正在 Spring 中学习 Slf4j 和 log4j。我已经看到了我们正在使用一行的地方

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?我如何获得在 log4j.xml 文件中定义的其他记录器?

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>

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);假设Name FQN 是foo.bar.Name ),当你调用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. ,您将获得一个名为foo.bar.Name的记录器,其继承自foo.bar ,其继承自foo ,其继承自 root 。

You can get the root logger in SLF4J by LoggerFactory.getLogger(Logger.ROOT_LOGGER_NAME)您可以通过LoggerFactory.getLogger(Logger.ROOT_LOGGER_NAME)在 SLF4J 中获取根记录器

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"我相信您对“获取从 ROOT 记录器继承配置的子记录器”和“获取 ROOT 记录器”感到困惑

2.How can i get other loggers which are defined in log4j.xml file? 2.如何获取log4j.xml文件中定义的其他记录器?

For example, if you want to get logger com.example.foo , it is simply by private final Logger logger = LoggerFactory.getLogger("com.example.foo");例如,如果要获取 logger com.example.foo ,只需通过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).如果您获得记录器“com.example.foo.Bar”,鉴于“com.example.foo.Bar”没有特定设置,则其行为应与使用“com.example.foo”相同(除了记录器名称当然会显示在日志中)。

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);由于将类名本身用作记录器名称是一种常见做法,因此 SLF4J 还提供了一种通过提供类(如您在问题中所做的那样)来获取记录器的方法,通过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")在这种情况下,获得的记录器名称将与提供的类的 FQN 相同(“com.example.foo.Bar”)

1.How this is getting root logger 1.这是如何获得根记录器的

Loggers build a hierarchy, as you can see from the tables here: http://logging.apache.org/log4j/2.x/manual/architecture.html记录器构建了一个层次结构,正如您从这里的表中看到的: http : //logging.apache.org/log4j/2.x/manual/architecture.html

2.How can i get other loggers which are defined in log4j.xml file? 2.如何获取log4j.xml文件中定义的其他记录器?

If you want strictly what's defined there, than you have to parse and read that config file.如果您想要严格定义那里的内容,则必须解析并阅读该配置文件。 If you need all the active loggers and their configuration at runtime, than there's API for it.如果您在运行时需要所有活动记录器及其配置,则可以使用 API。 Eg an article how to do it: http://nelz.net/2008/04/08/log4j-runtime-configuration/例如一篇如何做的文章: http : //nelz.net/2008/04/08/log4j-runtime-configuration/

Also,I havent found any good tutorial on slf4J.If you have some good links另外,我还没有找到关于 slf4J 的任何好的教程。如果你有一些好的链接

slf4j has quite a few docs: http://www.slf4j.org/docs.html but since it's just a wrapper, log4j does the "work" so here's a book about it: http://www.qos.ch/shop/products/log4jManual slf4j 有很多文档: http ://www.slf4j.org/docs.html 但由于它只是一个包装器,log4j 完成了“工作”,所以这里有一本关于它的书: http : //www.qos.ch/商店/产品/log4j手册

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM