使用logback实现按自己的需求打印日志到自定义的文件里
目录
- 1、工具类 LoggerUtils
- 2、枚举类 LogFileName
- 3、logger.xml, 按需配置即可
- 定义工具类-创建对应的日志对象
- 定义枚举类-存储定义的日志文件名称
- logback.xml里配置对应的日志名称和日志等级
1、工具类 LoggerUtils
import org.slf4j.Logger; import org.slf4j.LoggerFactory; public class LoggerUtils {public staticLogger 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} 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} ERROR 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} 【使用logback实现按自己的需求打印日志到自定义的文件里】${CONSOLE_LOG_PATTERN} TRACE ACCEPT DENY ${ROOT}%d/trace.%i.log ${MAXHISTORY} ${FILESIZE}
以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。
推荐阅读
- 由浅入深理解AOP
- 【译】20个更有效地使用谷歌搜索的技巧
- 关于QueryWrapper|关于QueryWrapper,实现MybatisPlus多表关联查询方式
- mybatisplus如何在xml的连表查询中使用queryWrapper
- MybatisPlus|MybatisPlus LambdaQueryWrapper使用int默认值的坑及解决
- MybatisPlus使用queryWrapper如何实现复杂查询
- python学习之|python学习之 实现QQ自动发送消息
- 孩子不是实现父母欲望的工具——林哈夫
- opencv|opencv C++模板匹配的简单实现
- Node.js中readline模块实现终端输入