springboot自带日志框架logging需要屏蔽掉
在spring-boot-starter依赖 没有这个依赖的在spring-boot-starter-web依赖中屏蔽默认日志
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</exclusion>
</exclusions>
添加log4j.properties日志配置文件
#定义LOG输出级别
log4j.rootLogger=INFO,Console
#定义日志输出目的地为控制台
log4j.appender.Console=org.apache.log4j.ConsoleAppender
#可以灵活地指定日志输出格式,下面一行是指定具体的格式
log4j.appender.Console.layout=org.apache.log4j.PatternLayout
log4j.appender.Console.layout.ConversionPattern=[%p] [%d{yyyy-MM-dd HH\:mm\:ss}][%c]%m%n
#每天一个文件
log4j.appender.baseapd=org.apache.log4j.DailyRollingFileAppender
#指定输出目录
log4j.appender.baseapd.File=../logs/root.log
#定义文件最大大小
log4j.appender.baseapd.append=true
log4j.appender.baseapd.DatePattern=yyyy-MM-dd
# 输出所以日志,如果换成DEBUG表示输出DEBUG以上级别日志
log4j.appender.baseapd.Threshold=INFO
log4j.appender.baseapd.layout=org.apache.log4j.PatternLayout
log4j.appender.baseapd.layout.ConversionPattern=[%p] [%d{yyyy-MM-dd HH\:mm\:ss}][%c]%m%n
log4j.logger.com=info,baseapd
#每天一个文件
log4j.appender.commInterceptor=org.apache.log4j.DailyRollingFileAppender
#指定输出目录
log4j.appender.commInterceptor.File=../logs/common_interceptor.log
#定义文件最大大小
log4j.appender.commInterceptor.append=true
log4j.appender.commInterceptor.DatePattern=yyyy-MM-dd
# 输出所以日志,如果换成DEBUG表示输出DEBUG以上级别日志
log4j.appender.commInterceptor.Threshold=DEBUG
log4j.appender.commInterceptor.layout=org.apache.log4j.PatternLayout
log4j.appender.commInterceptor.layout.ConversionPattern=[%p] [%d{yyyy-MM-dd HH\:mm\:ss}][%c]%m%n
log4j.logger.com.duocai.platform.api.interceptor=debug,commInterceptor
使用
private Logger logger = Logger.getLogger(MailController.class);
// 直接使用下面工具类
LogUtil.info(logger, "工具类可以灵活使用~~");
我这里写了日志工具类可以直接拿去用
package com.zlp.cps.util;
import com.alibaba.fastjson.JSONObject;
import org.apache.log4j.Logger;
/**
* JSONObject.toJSON(o)
*
*/
public class LogUtil {
public static void timeStatInfo(Logger logger, long start, Object... desc) {
try {
logger.info("timestat " + appendInfo(desc) + " " + (System.currentTimeMillis() - start));
} catch (Throwable e) {
e.printStackTrace();
}
}
public static void timeStatDebug(Logger logger, long start, Object... desc) {
try {
logger.debug("timestat " + appendInfo(desc) + " " + (System.currentTimeMillis() - start));
} catch (Throwable e) {
e.printStackTrace();
}
}
public static void debug(Logger logger, Object... objs) {
try {
logger.debug(appendInfo(objs));
} catch (Throwable e) {
e.printStackTrace();
}
}
public static void info(Logger logger, Object... objs) {
try {
logger.info(appendInfo(objs));
} catch (Throwable e) {
e.printStackTrace();
}
}
public static void warn(Logger logger, Object... objs) {
try {
logger.warn(appendInfo(objs));
} catch (Throwable e) {
e.printStackTrace();
}
}
public static void error(Logger logger, Object... objs) {
try {
logger.error(appendInfo(objs));
} catch (Throwable e) {
e.printStackTrace();
}
}
private static String appendInfo(Object... objs) {
StringBuilder sb = new StringBuilder();
for (Object o : objs) {
if (o == null) {
sb.append("NULL ");
} else {
try {
sb.append(JSONObject.toJSON(o)).append(" ");
} catch (Exception e) {
// TODO Criteria toJSON exception
sb.append(o.toString()).append(" ");
}
}
}
if (sb.length() > 1) {
sb.deleteCharAt(sb.length() - 1);
}
return sb.toString();
}
}