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

目录

  • 1、工具类 LoggerUtils
  • 2、枚举类 LogFileName
  • 3、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, 按需配置即可
${CONSOLE_LOG_PATTERN}UTF-8${CONSOLE_LOG_PATTERN}ERRORACCEPTDENY${ROOT}%d/error/systemError.%i.log${MAXHISTORY}${FILESIZE}${CONSOLE_LOG_PATTERN}ERRORACCEPTDENY${ROOT}%d/error/trainError.%i.log${MAXHISTORY}${FILESIZE}${CONSOLE_LOG_PATTERN}ERRORACCEPTDENY${ROOT}%d/error/userError.%i.log${MAXHISTORY}${FILESIZE}${CONSOLE_LOG_PATTERN}ERRORACCEPTDENY${ROOT}%d/error/payError.%i.log${MAXHISTORY}${FILESIZE}${CONSOLE_LOG_PATTERN}ERRORACCEPTDENY${ROOT}%d/error/reviewError.%i.log${MAXHISTORY}${FILESIZE}${CONSOLE_LOG_PATTERN}WARNACCEPTDENY${ROOT}%d/warn.%i.log${MAXHISTORY}${FILESIZE}${CONSOLE_LOG_PATTERN}INFOACCEPTDENY${ROOT}%d/info.%i.log${MAXHISTORY}${FILESIZE}${CONSOLE_LOG_PATTERN}DEBUGACCEPTDENY${ROOT}%d/debug.%i.log${MAXHISTORY}${FILESIZE}${CONSOLE_LOG_PATTERN}DEBUGACCEPTDENY${ROOT}%d/debug/payDebug.%i.log${MAXHISTORY}${FILESIZE}【使用logback实现按自己的需求打印日志到自定义的文件里】${CONSOLE_LOG_PATTERN}TRACEACCEPTDENY${ROOT}%d/trace.%i.log${MAXHISTORY}${FILESIZE}

以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。

    推荐阅读