使用logback实现按自己的需求打印日志到自定义的文件里

网友投稿 489 2022-10-01


使用logback实现按自己的需求打印日志到自定义的文件里

目录1、工具类 LoggerUtils2、枚举类 LogFileName3、logger.xml, 按需配置即可

定义工具类-创建对应的日志对象

定义枚举类-存储定义的日志文件名称

logback.xml里配置对应的日志名称和日志等级

1、工具类 LoggerUtils

import org.slf4j.Logger;

import org.slf4j.LoggerFactory;

public class LoggerUtils {

public static Logger Logger(Class clazz) {

return LoggerFactory.getLogger(clazz);

}

/**

* 打印到指定的文件下

*

* @param desc 日志文件名称

* @return

*/

public static Logger Logger(LogFileName desc) {

return LoggerFactory.getLogger(desc.getLogFileName());

}

}

2、枚举类 LogFileName

import org.apache.commons.lang3.StringUtils;

public enum LogFileName {

//配置到logback.xml中的logger name="getNextHh"

GET_NEXT_HH("getNextHh"),

GET_AGAIN_HH("getAgainHh"),

GET_AI_ACTION("getAiAction"),

TRAIN_ERROR("trainError"),

USER_ERROR("userError"),

PAY_ERROR("payError"),

REVIEW_ERROR("reviewError"),

WX_ERROR("wxError"),

SYSTEM_ERROR("systemError"),

PAY_DEBUG("payDebug");

private String logFileName;

LogFileName(String fileName) {

this.logFileName = fileName;

}

public String getLogFileName() {

return logFileName;

}

public void setLogFileName(String logFileName) {

this.logFileName = logFileName;

}

public static LogFileName getAwardTypeEnum(String value) {

LogFileName[] arr = values();

for (LogFileName item : arr) {

if (null != item && StringUtils.isNotBlank(item.logFileName)) {

return item;

}

}

return null;

}

}

3、logger.xml, 按需配置即可

xsi:noNamespaceSchemaLocation="http://padual.com/java/logback.xsd" debug="false"

scan="true" scanPeriod="30 second">

converterClass="org.springframework.boot.logging.logback.WhitespaceThrowableProxyConverter"/>

converterClass="org.springframework.boot.logging.logback.ExtendedWhitespaceThrowableProxyConverter"/>

value="${CONSOLE_LOG_PATTERN:-%clr(%d{yyyy-MM-dd HH:mm:ss:SSS}){faint} %clr(${LOG_LEVEL_PATTERN:-%5p}) %clr(${PID:- }){magenta} %clr(---){faint} %clr([%15.15t]){faint} %clr(%-40.40logger{39}){cyan} %clr(:){faint} %m%n${LOG_EXCEPTION_CONVERSION_WORD:-%wEx}}"/>

${CONSOLE_LOG_PATTERN}

UTF-8

${CONSOLE_LOG_PATTERN}

ERROR

ACCEPT

DENY

${ROOT}%d/error/systemError.%i.log

${MAXHISTORY}

${FILESIZE}

${CONSOLE_LOG_PATTERN}

ERROR

ACCEPT

DENY

${ROOT}%d/error/trainError.%i.log

${MAXHISTORY}

${FILESIZE}

${CONSOLE_LOG_PATTERN}

ERROR

ACCEPT

DENY

${ROOT}%d/error/userError.%i.log

${MAXHISTORY}

${FILESIZE}

${CONSOLE_LOG_PATTERN}

ACCEPT

DENY

${ROOT}%d/error/payError.%i.log

${MAXHISTORY}

${FILESIZE}

${CONSOLE_LOG_PATTERN}

ERROR

ACCEPT

DENY

${ROOT}%d/error/reviewError.%i.log

${MAXHISTORY}

${FILESIZE}

${CONSOLE_LOG_PATTERN}

WARN

ACCEPT

DENY

${ROOT}%d/warn.%i.log

${MAXHISTORY}

${FILESIZE}

${CONSOLE_LOG_PATTERN}

INFO

ACCEPT

DENY

${ROOT}%d/info.%i.log

${MAXHISTORY}

${FILESIZE}

${CONSOLE_LOG_PATTERN}

DEBUG

ACCEPT

DENY

${ROOT}%d/debug.%i.log

${MAXHISTORY}

${FILESIZE}

${CONSOLE_LOG_PATTERN}

DEBUG

ACCEPT

DENY

${ROOT}%d/debug/payDebug.%i.log

${MAXHISTORY}

${FILESIZE}

${CONSOLE_LOG_PATTERN}

TRACE

ACCEPT

DENY

${ROOT}%d/trace.%i.log

${MAXHISTORY}

${FILESIZE}

xsi:noNamespaceSchemaLocation="http://padual.com/java/logback.xsd" debug="false"

scan="true" scanPeriod="30 second">

converterClass="org.springframework.boot.logging.logback.WhitespaceThrowableProxyConverter"/>

converterClass="org.springframework.boot.logging.logback.ExtendedWhitespaceThrowableProxyConverter"/>

value="${CONSOLE_LOG_PATTERN:-%clr(%d{yyyy-MM-dd HH:mm:ss:SSS}){faint} %clr(${LOG_LEVEL_PATTERN:-%5p}) %clr(${PID:- }){magenta} %clr(---){faint} %clr([%15.15t]){faint} %clr(%-40.40logger{39}){cyan} %clr(:){faint} %m%n${LOG_EXCEPTION_CONVERSION_WORD:-%wEx}}"/>

${CONSOLE_LOG_PATTERN}

UTF-8

${CONSOLE_LOG_PATTERN}

ERROR

ACCEPT

DENY

${ROOT}%d/error/systemError.%i.log

${MAXHISTORY}

${FILESIZE}

${CONSOLE_LOG_PATTERN}

ERROR

ACCEPT

DENY

${ROOT}%d/error/trainError.%i.log

${MAXHISTORY}

${FILESIZE}

${CONSOLE_LOG_PATTERN}

ERROR

ACCEPT

DENY

${ROOT}%d/error/userError.%i.log

${MAXHISTORY}

${FILESIZE}

${CONSOLE_LOG_PATTERN}

ACCEPT

DENY

${ROOT}%d/error/payError.%i.log

${MAXHISTORY}

${FILESIZE}

${CONSOLE_LOG_PATTERN}

ERROR

ACCEPT

DENY

${ROOT}%d/error/reviewError.%i.log

${MAXHISTORY}

${FILESIZE}

${CONSOLE_LOG_PATTERN}

WARN

ACCEPT

DENY

${ROOT}%d/warn.%i.log

${MAXHISTORY}

${FILESIZE}

${CONSOLE_LOG_PATTERN}

INFO

ACCEPT

DENY

${ROOT}%d/info.%i.log

${MAXHISTORY}

${FILESIZE}

${CONSOLE_LOG_PATTERN}

DEBUG

ACCEPT

DENY

${ROOT}%d/debug.%i.log

${MAXHISTORY}

${FILESIZE}

${CONSOLE_LOG_PATTERN}

DEBUG

ACCEPT

DENY

${ROOT}%d/debug/payDebug.%i.log

${MAXHISTORY}

${FILESIZE}

${CONSOLE_LOG_PATTERN}

TRACE

ACCEPT

DENY

${ROOT}%d/trace.%i.log

${MAXHISTORY}

${FILESIZE}

converterClass="org.springframework.boot.logging.logback.WhitespaceThrowableProxyConverter"/>

converterClass="org.springframework.boot.logging.logback.ExtendedWhitespaceThrowableProxyConverter"/>

value="${CONSOLE_LOG_PATTERN:-%clr(%d{yyyy-MM-dd HH:mm:ss:SSS}){faint} %clr(${LOG_LEVEL_PATTERN:-%5p}) %clr(${PID:- }){magenta} %clr(---){faint} %clr([%15.15t]){faint} %clr(%-40.40logger{39}){cyan} %clr(:){faint} %m%n${LOG_EXCEPTION_CONVERSION_WORD:-%wEx}}"/>

${CONSOLE_LOG_PATTERN}

UTF-8

${CONSOLE_LOG_PATTERN}

ERROR

ACCEPT

DENY

${ROOT}%d/error/systemError.%i.log

${MAXHISTORY}

${FILESIZE}

${CONSOLE_LOG_PATTERN}

ERROR

ACCEPT

DENY

${ROOT}%d/error/trainError.%i.log

${MAXHISTORY}

${FILESIZE}

${CONSOLE_LOG_PATTERN}

ERROR

ACCEPT

DENY

${ROOT}%d/error/userError.%i.log

${MAXHISTORY}

${FILESIZE}

${CONSOLE_LOG_PATTERN}

ACCEPT

DENY

${ROOT}%d/error/payError.%i.log

${MAXHISTORY}

${FILESIZE}

${CONSOLE_LOG_PATTERN}

ERROR

ACCEPT

DENY

${ROOT}%d/error/reviewError.%i.log

${MAXHISTORY}

${FILESIZE}

${CONSOLE_LOG_PATTERN}

WARN

ACCEPT

DENY

${ROOT}%d/warn.%i.log

${MAXHISTORY}

${FILESIZE}

${CONSOLE_LOG_PATTERN}

INFO

ACCEPT

DENY

${ROOT}%d/info.%i.log

${MAXHISTORY}

${FILESIZE}

${CONSOLE_LOG_PATTERN}

DEBUG

ACCEPT

DENY

${ROOT}%d/debug.%i.log

${MAXHISTORY}

${FILESIZE}

${CONSOLE_LOG_PATTERN}

DEBUG

ACCEPT

DENY

${ROOT}%d/debug/payDebug.%i.log

${MAXHISTORY}

${FILESIZE}

${CONSOLE_LOG_PATTERN}

TRACE

ACCEPT

DENY

${ROOT}%d/trace.%i.log

${MAXHISTORY}

${FILESIZE}

converterClass="org.springframework.boot.logging.logback.ExtendedWhitespaceThrowableProxyConverter"/>

value="${CONSOLE_LOG_PATTERN:-%clr(%d{yyyy-MM-dd HH:mm:ss:SSS}){faint} %clr(${LOG_LEVEL_PATTERN:-%5p}) %clr(${PID:- }){magenta} %clr(---){faint} %clr([%15.15t]){faint} %clr(%-40.40logger{39}){cyan} %clr(:){faint} %m%n${LOG_EXCEPTION_CONVERSION_WORD:-%wEx}}"/>

${CONSOLE_LOG_PATTERN}

UTF-8

${CONSOLE_LOG_PATTERN}

ERROR

ACCEPT

DENY

${ROOT}%d/error/systemError.%i.log

${MAXHISTORY}

${FILESIZE}

${CONSOLE_LOG_PATTERN}

ERROR

ACCEPT

DENY

${ROOT}%d/error/trainError.%i.log

${MAXHISTORY}

${FILESIZE}

${CONSOLE_LOG_PATTERN}

ERROR

ACCEPT

DENY

${ROOT}%d/error/userError.%i.log

${MAXHISTORY}

${FILESIZE}

${CONSOLE_LOG_PATTERN}

ACCEPT

DENY

${ROOT}%d/error/payError.%i.log

${MAXHISTORY}

${FILESIZE}

${CONSOLE_LOG_PATTERN}

ERROR

ACCEPT

DENY

${ROOT}%d/error/reviewError.%i.log

${MAXHISTORY}

${FILESIZE}

${CONSOLE_LOG_PATTERN}

WARN

ACCEPT

DENY

${ROOT}%d/warn.%i.log

${MAXHISTORY}

${FILESIZE}

${CONSOLE_LOG_PATTERN}

INFO

ACCEPT

DENY

${ROOT}%d/info.%i.log

${MAXHISTORY}

${FILESIZE}

${CONSOLE_LOG_PATTERN}

DEBUG

ACCEPT

DENY

${ROOT}%d/debug.%i.log

${MAXHISTORY}

${FILESIZE}

${CONSOLE_LOG_PATTERN}

DEBUG

ACCEPT

DENY

${ROOT}%d/debug/payDebug.%i.log

${MAXHISTORY}

${FILESIZE}

${CONSOLE_LOG_PATTERN}

TRACE

ACCEPT

DENY

${ROOT}%d/trace.%i.log

${MAXHISTORY}

${FILESIZE}

value="${CONSOLE_LOG_PATTERN:-%clr(%d{yyyy-MM-dd HH:mm:ss:SSS}){faint} %clr(${LOG_LEVEL_PATTERN:-%5p}) %clr(${PID:- }){magenta} %clr(---){faint} %clr([%15.15t]){faint} %clr(%-40.40logger{39}){cyan} %clr(:){faint} %m%n${LOG_EXCEPTION_CONVERSION_WORD:-%wEx}}"/>

${CONSOLE_LOG_PATTERN}

UTF-8

${CONSOLE_LOG_PATTERN}

ERROR

ACCEPT

DENY

${ROOT}%d/error/systemError.%i.log

${MAXHISTORY}

${FILESIZE}

${CONSOLE_LOG_PATTERN}

ERROR

ACCEPT

DENY

${ROOT}%d/error/trainError.%i.log

${MAXHISTORY}

${FILESIZE}

${CONSOLE_LOG_PATTERN}

ERROR

ACCEPT

DENY

${ROOT}%d/error/userError.%i.log

${MAXHISTORY}

${FILESIZE}

${CONSOLE_LOG_PATTERN}

ACCEPT

DENY

${ROOT}%d/error/payError.%i.log

${MAXHISTORY}

${FILESIZE}

${CONSOLE_LOG_PATTERN}

ERROR

ACCEPT

DENY

${ROOT}%d/error/reviewError.%i.log

${MAXHISTORY}

${FILESIZE}

${CONSOLE_LOG_PATTERN}

WARN

ACCEPT

DENY

${ROOT}%d/warn.%i.log

${MAXHISTORY}

${FILESIZE}

${CONSOLE_LOG_PATTERN}

INFO

ACCEPT

DENY

${ROOT}%d/info.%i.log

${MAXHISTORY}

${FILESIZE}

${CONSOLE_LOG_PATTERN}

DEBUG

ACCEPT

DENY

${ROOT}%d/debug.%i.log

${MAXHISTORY}

${FILESIZE}

${CONSOLE_LOG_PATTERN}

DEBUG

ACCEPT

DENY

${ROOT}%d/debug/payDebug.%i.log

${MAXHISTORY}

${FILESIZE}

${CONSOLE_LOG_PATTERN}

TRACE

ACCEPT

DENY

${ROOT}%d/trace.%i.log

${MAXHISTORY}

${FILESIZE}


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:远程办公爆发式增长,数据安全成关键
下一篇:【PyHacker编写指南】经常会用到的模块
相关文章

 发表评论

暂时没有评论,来抢沙发吧~