1. 程式人生 > >logBack日誌整合檔案分級別儲存日誌回滾

logBack日誌整合檔案分級別儲存日誌回滾

下面提供的配置實現了按照不同級別儲存不同檔案,日誌回滾只保留最近7天日誌檔案

需要匯入一下幾個包


logback,xml配置

<?xml version="1.0" encoding="UTF-8"?>  
<configuration scan="true" scanPeriod="3 seconds">  
      <property name="ADDR" value="./xc_cool_app_" /> 
    <!-- <property name="ADDR" value="/opt/logs/app/xc_cool_app_" />   --><!-- 伺服器日誌路徑 -->
    <!-- 控制檯輸出 -->  
    <appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">   
        <encoder charset="UTF-8">  
            <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>  
        </encoder>  
    </appender>  
  
    <!-- 檔案輸出=每天產生一個檔案-->  
     <!-- 出錯日誌 appender  -->  
    <appender name="ERROR" class="com.xc.utils.LogUtils"> 
    <file>${ADDR}error.log</file>   
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">  
            <!-- 按天回滾 daily -->  
            <fileNamePattern>${ADDR}error-%d{yyyy-MM-dd}.log</fileNamePattern>  
            <!-- 日誌最大的歷史 7天 -->  
            <maxHistory>7</maxHistory>  
        </rollingPolicy>  
        <encoder>  
            <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger - %msg%n</pattern>  
        </encoder>  
        <filter class="ch.qos.logback.classic.filter.LevelFilter"><!-- 只打印錯誤日誌 -->  
            <level>ERROR</level>  
            <onMatch>ACCEPT</onMatch>  
            <onMismatch>DENY</onMismatch>  
        </filter>  
    </appender>  
  
    <appender name="INFO1" class="com.xc.utils.LogUtils">  
    	
        <file>${ADDR}info.log</file>  
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">  
            <fileNamePattern>${ADDR}info.%d{yyyyMMdd}.log</fileNamePattern>  
            <maxHistory>7</maxHistory>  
        </rollingPolicy>  
        <encoder charset="UTF-8">  
            <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>  
        </encoder>
        <filter class="ch.qos.logback.classic.filter.LevelFilter"><!-- 只打印INFO日誌 -->  
            <level>INFO</level>  
            <onMatch>ACCEPT</onMatch>  
            <onMismatch>DENY</onMismatch>  
        </filter>    
    </appender>  
      
     <appender name="WARN1" class="com.xc.utils.LogUtils">  
    	
        <file>${ADDR}warn.log</file>  
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">  
            <fileNamePattern>${ADDR}warn.%d{yyyyMMdd}.log</fileNamePattern>  
            <maxHistory>7</maxHistory>  
        </rollingPolicy>  
        <encoder charset="UTF-8">  
            <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>  
        </encoder>
        <filter class="ch.qos.logback.classic.filter.LevelFilter"><!-- 只打印INFO日誌 -->  
            <level>WARN</level>  
            <onMatch>ACCEPT</onMatch>  
            <onMismatch>DENY</onMismatch>  
        </filter>    
    </appender> 
    
    <root level="INFO"> 
     <appender-ref ref="ERROR" />  
     <appender-ref ref="INFO1" />  
     <appender-ref ref="WARN1" />  
    </root>  

</configuration> 
日誌工具類:裡面的方法可以根據個人情況進行刪除
package com.xc.utils;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Locale;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import ch.qos.logback.core.rolling.RollingFileAppender;

public class LogUtils extends RollingFileAppender<Object> {
	public static final int LOG_TYPE_ALARM = 1;
	public static final int LOG_TYPE_STATUS = 2;
	public static final int LOG_TYPE_DATA = 3;

	public static boolean DEBUG = true;
	public static boolean DEBUG_LESSER = true;
	public static Logger  logger = LoggerFactory.getLogger(Logger.class); 
	private static final String tag = "xc_cool_app  ";
	
	private static SimpleDateFormat sdf = new SimpleDateFormat("MM月dd日 HH:mm:ss", Locale.getDefault());
	private static LogUtils logUtils;
	public static String className1="";
	public static String className2="";
	static List<String> classname=new ArrayList<String>();
	

	public static LogUtils getInstance(Class clzz) {
		if (logUtils == null){
			logUtils = new LogUtils();
			logUtils.logger=LoggerFactory.getLogger(clzz);
		}
		return logUtils;
	}
	
		
	public static void logDebug(String str) {
		if (DEBUG)
			logger.debug(tag + "#T#" + sdf.format(new Date()) + "$T$ #M#" +str + "$M$");
	}

	public  static void logInfo(String str) {
		if (DEBUG)
			logger.info(tag + "#T#" + sdf.format(new Date()) + "$T$ #M#" +str + "$M$");
	}
	
	public static void logWarn(String str) {
		if (DEBUG)
			logger.warn(tag + "#T#" + sdf.format(new Date()) + "$T$ #M#" +str + "$M$");
	}
	
	public static void logError(String str) {
		if (DEBUG)
			logger.error(tag + "#T#" + sdf.format(new Date()) + "$T$ #M#" +str + "$M$");
		for(String s:classname){
		System.err.println("1------------"+s+"------");
		}
		
	}

	//================================================================================================
	public static void log(String str) {
		if (DEBUG)
			logger.info(tag + "#T#" + sdf.format(new Date()) + "$T$ #M#" + str + "$M$");
	}

	public static void logInfo(int type, String str) {
		if (DEBUG)
			logger.info(tag + "#T#" + sdf.format(new Date()) + "$T$ #P#" + type + "$P$ #M#" + str + "$M$");
	}

	public static void logInfo(String deviceId, int type, String str) {
		if (DEBUG)
			logger.info(tag + "#T#" + sdf.format(new Date()) + "$T$ #D#" + deviceId + "$D$ #P#" + type + "$P$ #M#" + str + "$M$");
	}

	public static void logLesser(String str) {
		if (DEBUG_LESSER)
			logger.info(tag + sdf.format(new Date()) + " : " + str);
	}

	public static void print(String str) {
		if (DEBUG)
			System.out.print(str);
	}

	public static void e(String str) {
		if (DEBUG)
			System.err.println(tag + sdf.format(new Date()) + " : " + str);
	}

	
}

呼叫方式:
public void add() {
		 LogUtils.getInstance(this.getClass()).logError("======error");  
		 
		 
		render("assetsAdd.html");
	}