下面提供的配置实现了按照不同级别存储不同文件,日志回滚只保留最近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");
}