最近对公司的现有日志进行了处理,使用的是logback
需求是按照级别输出,同时能按照业务将日志写入不同文件中。
以下是我的配置文件,直接放入resource目录下即可运行
logback.xml文件
其中日志格式这块因为我加入了zipkin生产traceid的逻辑,实现链路追踪,方便知道分布式中服务间调用关系,实际使用中删除掉即可
OPENAPI-LOGGING
[%boldRed(%d{HH:mm:ss.SSS})] %contextName [%X{X-B3-TraceId:-},%X{X-B3-SpanId:-}] %highlight(%level) [%boldRed(%thread)] %boldMagenta([%file:%line]) - %cyan(%msg%n)
UTF-8
${log_dir_sys}/info.log
true
INFO
ACCEPT
DENY
${log_dir_sys}/%d{yyyy-MM-dd}/info.%d{MM-dd}.log
30
true
[%d{HH:mm:ss.SSS}] %contextName [%X{X-B3-TraceId:-},%X{X-B3-SpanId:-}] %level [%thread] [%file:%line] - %msg%n
UTF-8
${log_dir_sys}/error.log
true
ERROR
ACCEPT
DENY
${log_dir_sys}/%d{yyyy-MM-dd}/error.%d{MM-dd}.log
30
true
[%d{HH:mm:ss.SSS}] %contextName [%X{X-B3-TraceId:-},%X{X-B3-SpanId:-}] %level [%thread] [%file:%line] - %msg%n
UTF-8
${log_dir_business}/user/business_user.log
true
${log_dir_business}/user/%d{yyyy-MM-dd}/business_user.%d{MM-dd}.log
30
true
[%d{HH:mm:ss.SSS}] %contextName [%X{X-B3-TraceId:-},%X{X-B3-SpanId:-}] %level [%thread] [%file:%line] - %msg%n
UTF-8
${log_dir_business}/order/business_order.log
true
${log_dir_business}/order/%d{yyyy-MM-dd}/business_order.%d{MM-dd}.log
30
true
[%d{HH:mm:ss.SSS}] %contextName [%X{X-B3-TraceId:-},%X{X-B3-SpanId:-}] %level [%thread] [%file:%line] - %msg%n
UTF-8
${log_dir_business}/qrcode/business_qrcode.log
true
${log_dir_business}/qrcode/%d{yyyy-MM-dd}/business_qrcode.%d{MM-dd}.log
30
true
[%d{HH:mm:ss.SSS}] %contextName [%X{X-B3-TraceId:-},%X{X-B3-SpanId:-}] %level [%thread] [%file:%line] - %msg%n
UTF-8
${log_dir_business}/admin/business_admin.log
true
${log_dir_business}/admin/%d{yyyy-MM-dd}/business_admin.%d{MM-dd}.log
30
true
[%d{HH:mm:ss.SSS}] %contextName [%X{X-B3-TraceId:-},%X{X-B3-SpanId:-}] %level [%thread] [%file:%line] - %msg%n
UTF-8
${log_dir_business}/ticket/business_ticket.log
true
${log_dir_business}/ticket/%d{yyyy-MM-dd}/business_ticket.%d{MM-dd}.log
30
true
[%d{HH:mm:ss.SSS}] %contextName [%X{X-B3-TraceId:-},%X{X-B3-SpanId:-}] %level [%thread] [%file:%line] - %msg%n
UTF-8
LogFileName.java 枚举常量
public enum LogFileName {
//配置到logback.xml中的logger name="businessUser"
USERLOG("businessUser"),
ORDERLOG("businessOrder"),
QRCODELOG("qrCode"),
ADMINLOG("admin"),
TICKETLOG("ticket");
private String logFileName;
LogFileName(String fileName) {
this.logFileName = fileName;
}
public String getLogFileName() {
return logFileName;
}
public void setLogFileName(String logFileName) {
this.logFileName = logFileName;
}
}
LoggerUtils.java
public class LoggerUtils {
public static org.slf4j.Logger Logger(Class clazz) {
return LoggerFactory.getLogger(clazz);
}
/**
* 打印到指定的文件下
*
* @param desc 日志文件名称
* @return
*/
public static org.slf4j.Logger Logger(LogFileName desc) {
return LoggerFactory.getLogger(desc.getLogFileName());
}
}
AppTest.java测试类
@SpringBootTest(classes = ApplicationOpenApi.class)
public class AppTest {
Logger USERLOG = LoggerUtils.Logger(LogFileName.USERLOG);
Logger ORDERLOG = LoggerUtils.Logger(LogFileName.ORDERLOG);
Logger QRCODELOG = LoggerUtils.Logger(LogFileName.QRCODELOG);
Logger ADMINLOG = LoggerUtils.Logger(LogFileName.ADMINLOG);
Logger TICKETLOG = LoggerUtils.Logger(LogFileName.TICKETLOG);
Logger logger = LoggerFactory.getLogger(AppTest.class);
@Test
public void testGetBusinessAccount() throws Exception {
String a="123";
Map map = new HashMap();
map.put("1", 123);
map.put("2", "456");
System.out.println("2");
logger.warn("324234244234wsrwerwer");
logger.debug("324234244234wsrwerwer");
logger.error("324234244234wsrwerwer");
USERLOG.info("用户日志map:[{}]---string:[{}]",map,a);
ORDERLOG.info("订单日志");
QRCODELOG.info("二维码日志");
ADMINLOG.info("用户管理后台日志");
USERLOG.error("用户日志");
ORDERLOG.error("订单日志");
QRCODELOG.error("二维码日志");
ADMINLOG.error("用户管理后台日志");
TICKETLOG.error("车票日志");
}
}