我使用SLF4J +的logback 這裏是logback.xml 3個追加程序(1 - 控制檯,2個不同的文件)self4g的logback 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);
}
}
}
記錄& CONNECT_FILE_lOGGER將信息寫入同一個文件,所以它們是相同的記錄器。 爲什麼當我們調用LoggerFactory.getLogger("connect");
時,即使名稱爲「connect」的記錄器沒有被註釋,我們也會得到名爲「com」的記錄器。 我嘗試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
首先你的配置是錯誤的:它在連接和通訊記錄器上都有'loger'標記而不是'logger'。其次,「同樣的伐木工」是什麼意思? 'connect'和'com'將是不同的記錄器 - 如果配置是正確的。 – Grasshopper
1.謝謝你,我把tag'loger'改成了'logger'。但情況沒有改變。 2.他們在同一個文件中寫入信息,所以他們使用相同的appender,所以他們是同一個記錄器,因爲只有在一個記錄器中我設置了'FILE'appender – MIF
並且它工作嗎? – Grasshopper