self4g logback两个记录器在同一个类中不起作用

时间:2015-05-28 12:40:34

标签: java logging slf4j logback

我使用slf4j + logback 这是带有3个appender的logback.xml(1个 - 控制台,2个不同的文件)

`

<configuration>
    <property name="LOG_HOME" value="D:/logs" />
    <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
        <layout class="ch.qos.logback.classic.PatternLayout">
            <Pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</Pattern>
    </layout>
</appender>
<appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
   <!-- <file>D:/logs/all/all_log.txt</file>-->
    <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
        <!-- daily rollover -->
        <fileNamePattern>${LOG_HOME}/all/all_log.%d{yyyy-MM-dd}.log</fileNamePattern>
        <!-- keep 30 days' worth of history -->
        <maxHistory>30</maxHistory>
    </rollingPolicy>
    <append>true</append>
    <encoder>
        <pattern>%-4relative [%thread] %-5level %logger{35} - %msg%n</pattern>
    </encoder>
</appender>
<appender name="CONNECT_FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
    <!--<file>D:/logs/connect/connect_log.txt</file>-->
    <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
        <!-- daily rollover -->
        <fileNamePattern>${LOG_HOME}/connect/connect_log.%d{yyyy-MM-dd}.log</fileNamePattern>

        <!-- keep 30 days' worth of history -->
        <maxHistory>30</maxHistory>
    </rollingPolicy>
    <append>true</append>
    <encoder>
        <pattern>%-4relative [%thread] %-5level %logger{35} - %msg%n</pattern>
    </encoder>
</appender>
<!--<logger name="connect" level="info" additivity="false">
    <appender-ref ref="CONNECT_FILE"/>
</logger>-->
<logger name = "com" level="info">
    <appender-ref ref="FILE"/>
</logger>
<root level="info">
    <appender-ref ref="STDOUT"/>
</root>

`

和简单的课程:

package com.gitHub.xMIFx.Servlets;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * Created by bukatinvv on 25.05.2015.
 */
@WebServlet("/main.do")
public class MainController  extends HttpServlet{
    private static final String PAGE_OK = "pages/main.jsp";
    private static final Logger lOGGER = LoggerFactory.getLogger(MainController.class.getName());
    private static final Logger CONNECT_FILE_lOGGER = LoggerFactory.getLogger("connect");
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp){
        req.setAttribute("User","Vlad");
        try {
            lOGGER.info("logger: forwarding");
            CONNECT_FILE_lOGGER.info("Connect-logger: forwarding");
           // if(true){throw new IOException("blad");}
            req.getRequestDispatcher(PAGE_OK).forward(req,resp);
        } catch (ServletException e) {
            lOGGER.error("Exception", e);
        } catch (IOException e) {
            lOGGER.error("Exception", e);
        }
    }
}

lOGGER&amp; CONNECT_FILE_lOGGER在同一文件中写入信息,因此它们是相同的记录器。 为什么当我们调用LoggerFactory.getLogger("connect");时,我们得到名为“com”的记录器,即使没有注释名为“connect”的记录器。 我尝试this,但情况相同。

来自文件all_log.2015-05-28.log的日志:

4968 [http-nio-8081-exec-5] INFO  c.g.xMIFx.Servlets.MainController - logger: forwarding 
4968 [http-nio-8081-exec-5] INFO  connect - Connect-logger: forwarding

2 个答案:

答案 0 :(得分:0)

有趣的是,即使您没有使用传递给LoggerFactory的名称声明记录器,logback也会创建一个记录器。因此:

  • 当'connect'被注释掉时,CONNECT_FILE_lOGGER是一个ROOT子记录器,它也登录到控制台。
  • 通过取消注释'connect',您创建了一个附加到所需文件的记录器。由于additivity标志为false,因此记录到它的任何消息都不会传播到其父级。

答案 1 :(得分:-1)

抱歉,我不知道那是什么。但当我将additivity="false"添加到名为“com”的记录器时,它可以工作。然后我将其退回并取消评论“连接” - 现在一切正常。