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");
	}


免费的个人常用网址收藏管理导航——666导航网

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值