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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18561936/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 09:03:39  来源:igfitidea点击:

How to get other than root logger from SLF4J using Log4J

javaslf4jlog4j

提问by beinghuman

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?

  1. 这是如何获得根记录器的?我错了吗?

  2. 我如何获得在 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>

采纳答案by Adrian Shum

I have seen that this is getting root logger by default

我已经看到默认情况下这是获取根记录器

No it is not.

不它不是。

When you called private final Logger logger = LoggerFactory.getLogger(Name.class);and assume FQN of Nameis foo.bar.Name, then 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.

当您调用private final Logger logger = LoggerFactory.getLogger(Name.class);并假设 FQN of Nameis 时foo.bar.Name,您将获得一个名称foo.bar.Name为的记录器,其继承自foo.bar、继承自foo、继承自根。

The way to get the root logger in SLF4J is by LoggerFactory.getLogger(Logger.ROOT_LOGGER_NAME)

在 SLF4J 中获取根记录器的方法是 LoggerFactory.getLogger(Logger.ROOT_LOGGER_NAME)

What you need to know is, loggers are in hierarchy. Child level logger is inheriting configurations from the parent (including appenders, level etc). I believe you have mixed up the idea of "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, you are trying to get com.example.foo, it is simply by private final Logger logger = LoggerFactory.getLogger("com.example.foo");

例如,您正在尝试获取com.example.foo,它只是通过private final Logger logger = LoggerFactory.getLogger("com.example.foo");

As mentioned above, loggers are hierarchical. If you get logger "com.example.foo.Bar", given that you haven't give specific setting to "com.example.foo.Bar", its behavior should be the same as using "com.example.foo" (except the logger name shown in log of course).

如上所述,记录器是分层的。如果你得到记录器“com.example.foo.Bar”,鉴于你没有给“com.example.foo.Bar”提供特定的设置,它的行为应该与使用“com.example.foo”相同(当然,日志中显示的记录器名称除外)。

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

由于使用类名本身作为记录器名称是一种正常的做法,因此 SLF4J 还提供了一种通过提供类来获取记录器的方法(如您在问题中所做的那样),以便您可以这样做Logger logger = LoggerFactory.getLogger(Bar.class);,这使它更重构友好。在这种情况下,获得的记录器名称将与提供的类的 FQN 相同(在这种情况下为“com.example.foo.Bar”)

回答by Hans

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. E.g. an article how to do it: http://nelz.net/2008/04/08/log4j-runtime-configuration/

如果您想要严格定义那里的内容,则必须解析并阅读该配置文件。如果您在运行时需要所有活动记录器及其配置,则可以使用 API。例如一篇文章如何做: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.htmlbut 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手册