实际开发项目中有时候需要将某些信息单独输出到一个文件以做分析,为了让日志信息更清晰,这时就可以用到log4j2的自定义日志级别输出到独立的文件中。
log4j2依赖包导入什么的这里就不说了,直接上配置和代码。
因为项目在线上使用的是apollo自动化配置,而线上apollo配置文件用的是properties格式,所以这里我没有用xml格式配置。xml方式的配置百度一下有很多,也是官方推荐的配置方式,我这里就不写了。
1、配置属性文件 log4j2.properties
# 根记录器,所有记录器的父辈
rootLogger.level = info
rootLogger.type = AsyncRoot
#文件路径
property.filePath = /Users/apple/ane/logs/${appName}
property.filePattern = %d{yyyy-MM-dd}_%i
property.appName = app1
#输出格式
property.layoutPattern = %level|${appName}|%d{yyyy-MM-dd HH:mm:ss}|%t|%logger{36}|%m%n
property.layoutPatternC = %level|${appName}|%d{yyyy-MM-dd HH:mm:ss}|%t|%logger{36}|%m%n
# 级别过滤(过滤日志记录)
#filters = threshold
filter.threshold.type = ThresholdFilter
# 只记录debug级别以上的日志,大小写无关:(ALL<)TRACE<DEBUG<INFO<WARN<ERROR<FATAL(<OFF)
filter.threshold.level = trace
appender.console.type = Console
appender.console.name = STDOUT
appender.console.target = SYSTEM_OUT
appender.console.layout.type = PatternLayout
appender.console.layout.pattern = ${layoutPatternC}
rootLogger.appenderRef.stdout.ref = STDOUT
#自定义日志配置CUSTOM
#level值
customLevel.CUSTOM = 50
rootLogger.appenderRef.CUSTOM.ref = CustomRollingFile
appender.CUSTOM.fileName = ${filePath}/custom/custom.log
appender.CUSTOM.filePattern = ${filePath}/custom/custom_${filePattern}.log
#日志文件类型:文件大小滚动到设定大小自动生成一个指定格式名称的文件
appender.CUSTOM.type = RollingFile
appender.CUSTOM.name = CustomRollingFile
appender.CUSTOM.layout.type = PatternLayout
#日志输出格式
appender.CUSTOM.layout.pattern = %m%n
appender.CUSTOM.policies.type = Policies
appender.CUSTOM.policies.time.type = TimeBasedTriggeringPolicy
appender.CUSTOM.policies.time.interval = 1
appender.CUSTOM.policies.time.modulate = true
#日志文件大小限制10M
appender.CUSTOM.policies.size.type = SizeBasedTriggeringPolicy
appender.CUSTOM.policies.size.size = 10M
appender.CUSTOM.policies.start.type = OnStartupTriggeringPolicy
appender.CUSTOM.strategy.type = DefaultRolloverStrategy
appender.CUSTOM.strategy.fileIndex = nomax
#删除规则设置,超过一天的文件会被删除
appender.CUSTOM.strategy.delete.type = Delete
appender.CUSTOM.strategy.delete.basePath = ${filePath}/custom/
appender.CUSTOM.strategy.delete.maxDepth = 1
appender.CUSTOM.strategy.delete.iffilename.type = IfFileName
appender.CUSTOM.strategy.delete.iffilename.glob = custom_*.log
appender.CUSTOM.strategy.delete.iffilename.iflastmodified.type = IfLastModified
appender.CUSTOM.strategy.delete.iffilename.iflastmodified.age = 1d
#日志级别匹配,只有CUSTOM级别的日志信息才会写入到文件
appender.CUSTOM.filter.levelRange.type = LevelRangeFilter
appender.CUSTOM.filter.levelRange.minLevel = CUSTOM
appender.CUSTOM.filter.levelRange.maxLevel = CUSTOM
appender.CUSTOM.filter.levelRange.onMatch = ACCEPT
appender.CUSTOM.filter.levelRange.onMismatch = DENY
这里还只是一个自定义级别的,如果我要配置多个自定义级别怎么办了?
不慌,喝口水,来,看配置
# 根记录器,所有记录器的父辈
rootLogger.level = info
rootLogger.type = AsyncRoot
#文件路径
property.filePath = /Users/apple/ane/logs/${appName}
property.filePattern = %d{yyyy-MM-dd}_%i
property.appName = app2
#输出格式
property.layoutPattern = %level|${appName}|%d{yyyy-MM-dd HH:mm:ss}|%t|%logger{36}|%m%n
property.layoutPatternC = %level|${appName}|%d{yyyy-MM-dd HH:mm:ss}|%t|%logger{36}|%m%n
# 级别过滤(过滤日志记录)
#filters = threshold
filter.threshold.type = ThresholdFilter
# 只记录debug级别以上的日志,大小写无关:(ALL<)TRACE<DEBUG<INFO<WARN<ERROR<FATAL(<OFF)
filter.threshold.level = trace
appender.console.type = Console
appender.console.name = STDOUT
appender.console.target = SYSTEM_OUT
appender.console.layout.type = PatternLayout
appender.console.layout.pattern = ${layoutPatternC}
rootLogger.appenderRef.stdout.ref = STDOUT
#自定义日志配置CUSTOM_ADD
customLevel.CUSTOM_ADD = 70
rootLogger.appenderRef.CUSTOM_ADD.ref = CustomAddRollingFile
appender.CUSTOM_ADD.fileName = ${filePath}/custom/custom_add.log
appender.CUSTOM_ADD.filePattern = ${filePath}/custom/custom_add_${filePattern}.log
appender.CUSTOM_ADD.type = RollingFile
appender.CUSTOM_ADD.name = CustomAddRollingFile
appender.CUSTOM_ADD.layout.type = PatternLayout
appender.CUSTOM_ADD.layout.pattern = %m%n
appender.CUSTOM_ADD.policies.type = Policies
appender.CUSTOM_ADD.policies.time.type = TimeBasedTriggeringPolicy
appender.CUSTOM_ADD.policies.time.interval = 1
appender.CUSTOM_ADD.policies.time.modulate = true
appender.CUSTOM_ADD.policies.size.type = SizeBasedTriggeringPolicy
appender.CUSTOM_ADD.policies.size.size = 10M
appender.CUSTOM_ADD.policies.start.type = OnStartupTriggeringPolicy
appender.CUSTOM_ADD.strategy.type = DefaultRolloverStrategy
appender.CUSTOM_ADD.strategy.fileIndex = nomax
appender.CUSTOM_ADD.strategy.delete.type = Delete
appender.CUSTOM_ADD.strategy.delete.basePath = ${filePath}/custom/
appender.CUSTOM_ADD.strategy.delete.maxDepth = 1
appender.CUSTOM_ADD.strategy.delete.iffilename.type = IfFileName
appender.CUSTOM_ADD.strategy.delete.iffilename.glob = custom_add_*.log
appender.CUSTOM_ADD.strategy.delete.iffilename.iflastmodified.type = IfLastModified
appender.CUSTOM_ADD.strategy.delete.iffilename.iflastmodified.age = 1d
appender.CUSTOM_ADD.filter.levelRange.type = LevelRangeFilter
appender.CUSTOM_ADD.filter.levelRange.minLevel = CUSTOM_ADD
appender.CUSTOM_ADD.filter.levelRange.maxLevel = CUSTOM_ADD
appender.CUSTOM_ADD.filter.levelRange.onMatch = ACCEPT
appender.CUSTOM_ADD.filter.levelRange.onMismatch = DENY
#自定义日志配置CUSTOM_UPT
customLevel.CUSTOM_UPT = 60
rootLogger.appenderRef.CUSTOM_UPT.ref = CustomUptRollingFile
appender.CUSTOM_UPT.fileName = ${filePath}/custom/custom_upt.log
appender.CUSTOM_UPT.filePattern = ${filePath}/custom/custom_upt_${filePattern}.log
appender.CUSTOM_UPT.type = RollingFile
appender.CUSTOM_UPT.name = CustomUptRollingFile
appender.CUSTOM_UPT.layout.type = PatternLayout
appender.CUSTOM_UPT.layout.pattern = %m%n
appender.CUSTOM_UPT.policies.type = Policies
appender.CUSTOM_UPT.policies.time.type = TimeBasedTriggeringPolicy
appender.CUSTOM_UPT.policies.time.interval = 1
appender.CUSTOM_UPT.policies.time.modulate = true
appender.CUSTOM_UPT.policies.size.type = SizeBasedTriggeringPolicy
appender.CUSTOM_UPT.policies.size.size = 10M
appender.CUSTOM_UPT.strategy.type = DefaultRolloverStrategy
appender.CUSTOM_UPT.strategy.fileIndex = nomax
appender.CUSTOM_UPT.strategy.delete.type = Delete
appender.CUSTOM_UPT.strategy.delete.basePath = ${filePath}/custom/
appender.CUSTOM_UPT.strategy.delete.maxDepth = 1
appender.CUSTOM_UPT.strategy.delete.iffilename.type = IfFileName
appender.CUSTOM_UPT.strategy.delete.iffilename.glob = custom_upt_*.log
appender.CUSTOM_UPT.strategy.delete.iffilename.iflastmodified.type = IfLastModified
appender.CUSTOM_UPT.strategy.delete.iffilename.iflastmodified.age = 1d
appender.CUSTOM_UPT.filter.levelRange.type = LevelRangeFilter
appender.CUSTOM_UPT.filter.levelRange.minLevel = CUSTOM_UPT
appender.CUSTOM_UPT.filter.levelRange.maxLevel = CUSTOM_UPT
appender.CUSTOM_UPT.filter.levelRange.onMatch = ACCEPT
appender.CUSTOM_UPT.filter.levelRange.onMismatch = DENY
#自定义日志配置CUSTOM_DEL
customLevel.CUSTOM_DEL = 50
rootLogger.appenderRef.CUSTOM_DEL.ref = CustomDelRollingFile
appender.CUSTOM_DEL.fileName = ${filePath}/custom/custom_del.log
appender.CUSTOM_DEL.filePattern = ${filePath}/custom/custom_del_${filePattern}.log
appender.CUSTOM_DEL.type = RollingFile
appender.CUSTOM_DEL.name = CustomDelRollingFile
appender.CUSTOM_DEL.layout.type = PatternLayout
appender.CUSTOM_DEL.layout.pattern = %m%n
appender.CUSTOM_DEL.policies.type = Policies
appender.CUSTOM_DEL.policies.time.type = TimeBasedTriggeringPolicy
appender.CUSTOM_DEL.policies.time.interval = 1
appender.CUSTOM_DEL.policies.time.modulate = true
appender.CUSTOM_DEL.policies.size.type = SizeBasedTriggeringPolicy
appender.CUSTOM_DEL.policies.size.size = 10M
appender.CUSTOM_DEL.strategy.type = DefaultRolloverStrategy
appender.CUSTOM_DEL.strategy.fileIndex = nomax
appender.CUSTOM_DEL.strategy.delete.type = Delete
appender.CUSTOM_DEL.strategy.delete.basePath = ${filePath}/custom/
appender.CUSTOM_DEL.strategy.delete.maxDepth = 1
appender.CUSTOM_DEL.strategy.delete.iffilename.type = IfFileName
appender.CUSTOM_DEL.strategy.delete.iffilename.glob = custom_del_*.log
appender.CUSTOM_DEL.strategy.delete.iffilename.iflastmodified.type = IfLastModified
appender.CUSTOM_DEL.strategy.delete.iffilename.iflastmodified.age = 1d
appender.CUSTOM_DEL.filter.levelRange.type = LevelRangeFilter
appender.CUSTOM_DEL.filter.levelRange.minLevel = CUSTOM_DEL
appender.CUSTOM_DEL.filter.levelRange.maxLevel = CUSTOM_DEL
appender.CUSTOM_DEL.filter.levelRange.onMatch = ACCEPT
appender.CUSTOM_DEL.filter.levelRange.onMismatch = DENY
多个自定义级别的配置我也是研究了很久才成功,不管是log4j2官网还是百度都没有找到相应的properties配置,只能自己一个一个属性去试,还好最终成功配置好了。
2、创建一个日志工具类 CustomLogUtil
import org.apache.logging.log4j.Level;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import java.util.List;
public class CustomLogUtil {
private static Logger logger = LogManager.getLogger(CustomLogUtil.class);
public final static Level CUSTOM_LEVEL = Level.forName("CUSTOM",50);
/**
* 自定义级别日志打印
* @param logs
*/
public static void printCustomLog(String logs){
logger.log(CUSTOM_LEVEL, logs);
}
public static void printCustomLog(Object obj){
logger.log(CUSTOM_LEVEL, JacksonUtil.writeValueWithSplitString(obj, c));
}
}
JacksonUtil.writeValueSplitString()方法是我自定义的方法,用来把对象转成我要的json格式。你们直接用alibaba的json工具包就行了。
有多个自定义级别的话就创建多个Level对象,注意级别名和级别值不要一样
public final static Level CUSTOM_ADD_LEVEL = Level.forName("CUSTOM_ADD",70);
public final static Level CUSTOM_UPT_LEVEL = Level.forName("CUSTOM_UPT",60);
public final static Level CUSTOM_DEL_LEVEL = Level.forName("CUSTOM_DEL",50);
3、使用
在要输出日志的地方直接通过下面的方式调用就行了
CustomLogUtil.printCustomLog("我是自定义级别日志");