logback 配置文件模板与输出格式参数说明

1,输出格式详解:

该部分内容为转载内容,由该链接地址作者提供:原文:https://blog.csdn.net/qq_34208844/article/details/86289218

部分标签解释

  • withJansi: 是否配合jansi使用
  • filter: 日志过滤器
  • layout: 布局, 配合内置模板使用
  • pattern: 日志模板, 有内置的日志模板可以直接使用, 例如: ${FILE_LOG_PATTERN}
  • encoder: 编码器, 支付转码
  • charset: 支付编码, 一般为utf-8内置转换器可以直接通过调用内置的转换器将日志取出来, 打印出来

部分特殊字符串解释

  • %d: 表示日期
  • %thread: 表示线程名
  • %-5level: 级别从左显示5个字符宽度
  • %msg: 日志消息
  • %n: 是换行符

更多内置特殊字符

defaultConverterMap.put("d", DateConverter.class.getName());
defaultConverterMap.put("date", DateConverter.class.getName());
defaultConverterMap.put("r", RelativeTimeConverter.class.getName());
defaultConverterMap.put("relative", RelativeTimeConverter.class.getName());
defaultConverterMap.put("level", LevelConverter.class.getName());
defaultConverterMap.put("le", LevelConverter.class.getName());
defaultConverterMap.put("p", LevelConverter.class.getName());
defaultConverterMap.put("t", ThreadConverter.class.getName());
defaultConverterMap.put("thread", ThreadConverter.class.getName());
defaultConverterMap.put("lo", LoggerConverter.class.getName());
defaultConverterMap.put("logger", LoggerConverter.class.getName());
defaultConverterMap.put("c", LoggerConverter.class.getName());
defaultConverterMap.put("m", MessageConverter.class.getName());
defaultConverterMap.put("msg", MessageConverter.class.getName());
defaultConverterMap.put("message", MessageConverter.class.getName());
defaultConverterMap.put("C", ClassOfCallerConverter.class.getName());
defaultConverterMap.put("class", ClassOfCallerConverter.class.getName());
defaultConverterMap.put("M", MethodOfCallerConverter.class.getName());
defaultConverterMap.put("method", MethodOfCallerConverter.class.getName());
defaultConverterMap.put("L", LineOfCallerConverter.class.getName());
defaultConverterMap.put("line", LineOfCallerConverter.class.getName());
defaultConverterMap.put("F", FileOfCallerConverter.class.getName());
defaultConverterMap.put("file", FileOfCallerConverter.class.getName());
defaultConverterMap.put("X", MDCConverter.class.getName());
defaultConverterMap.put("mdc", MDCConverter.class.getName());
defaultConverterMap.put("ex", ThrowableProxyConverter.class.getName());
defaultConverterMap.put("exception", ThrowableProxyConverter.class.getName());
defaultConverterMap.put("rEx", RootCauseFirstThrowableProxyConverter.class.getName());
defaultConverterMap.put("rootException", RootCauseFirstThrowableProxyConverter.class.getName());
defaultConverterMap.put("throwable", ThrowableProxyConverter.class.getName());
defaultConverterMap.put("xEx", ExtendedThrowableProxyConverter.class.getName());
defaultConverterMap.put("xException", ExtendedThrowableProxyConverter.class.getName());
defaultConverterMap.put("xThrowable", ExtendedThrowableProxyConverter.class.getName());
defaultConverterMap.put("nopex", NopThrowableInformationConverter.class.getName());
defaultConverterMap.put("nopexception", NopThrowableInformationConverter.class.getName());
defaultConverterMap.put("cn", ContextNameConverter.class.getName());
defaultConverterMap.put("contextName", ContextNameConverter.class.getName());
defaultConverterMap.put("caller", CallerDataConverter.class.getName());
defaultConverterMap.put("marker", MarkerConverter.class.getName());
defaultConverterMap.put("property", PropertyConverter.class.getName());
defaultConverterMap.put("n", LineSeparatorConverter.class.getName());

如何自定义输出样式

字符颜色定义

使用%加上颜色代号, 然后内容放在后面的括号()里面

例如:

%white(这里放输出内容)
配合特殊字符串, 可以输入时间, 线程名等

内置的一些样式

代码示例

使用内置模板

 <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
     <filter class="com.wang.cloud.store.common.LogFilter"/>
     <layout class="ch.qos.logback.classic.PatternLayout">
         <pattern>${FILE_LOG_PATTERN}pattern>
     layout>
 appender>

使用自定义模板

<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
     <filter class="com.wang.cloud.store.common.LogFilter"/>
     <withJansi>falsewithJansi>
     <encoder>
         <pattern>%white(%d{yyyy-MM-dd HH:mm:ss}) %highlight(%lsn) %green([%thread]) %highlight(%-5level) %boldMagenta(%logger{10}) - %cyan(%msg%n)pattern>
         <charset>utf8charset>
     encoder>
 appender>

2,自定义模板




<configuration scan="true" scanPeriod="60 seconds" debug="false">
	
	<springProperty scope="context" name="log_dir" source="logging.self.file">springProperty>
	
	<property name="maxHistory" value="60">property>
	
	<property name="log_pattern" value="%d{yyyy-MM-dd HH:mm:ss.SSS} %-5level --- [%thread] %class#%method@%line : %msg %n">property>

	
	<appender name="STDOUT"
		class="ch.qos.logback.core.ConsoleAppender">
		<encoder>
			<pattern>${log_pattern}pattern>
		encoder>
	appender>

	
	<appender name="ERROR"
		class="ch.qos.logback.core.rolling.RollingFileAppender">
		
		<rollingPolicy
			class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
			
			<fileNamePattern>${log_dir}/error-log-%d{yyyy-MM-dd}.logfileNamePattern>
			
			<maxHistory>${maxHistory}maxHistory>
		rollingPolicy>
		<encoder>
			<pattern>${log_pattern}pattern>
		encoder>
	appender>

	
	<appender name="INFO"
		class="ch.qos.logback.core.rolling.RollingFileAppender">
		<rollingPolicy
			class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
			
			<fileNamePattern>${log_dir}/info-log-%d{yyyy-MM-dd}.logfileNamePattern>
			
			<maxHistory>${maxHistory}maxHistory>
		rollingPolicy>
		<encoder>
			<pattern>${log_pattern}pattern>
		encoder>
	appender>

	
	<appender name="ACCESS"
		class="ch.qos.logback.core.rolling.RollingFileAppender">
		<rollingPolicy
			class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
			
			<fileNamePattern>${log_dir}/access-log-%d{yyyy-MM-dd}.logfileNamePattern>
			
			<maxHistory>${maxHistory}maxHistory>
		rollingPolicy>
		<encoder>
			<pattern>${log_pattern}pattern>
		encoder>
	appender>

	
	<appender name="SYS-USER"
		class="ch.qos.logback.core.rolling.RollingFileAppender">
		<rollingPolicy
			class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
			
			<fileNamePattern>${log_dir}/sys_user-log-%d{yyyy-MM-dd}.logfileNamePattern>
			
			<maxHistory>${maxHistory}maxHistory>
		rollingPolicy>
		<encoder>
			<pattern>${log_pattern}pattern>
		encoder>
	appender>

	
	<logger name="java.sql.Connection" level="DEBUG" />
	<logger name="java.sql.Statement" level="DEBUG" />
	<logger name="java.sql.PreparedStatement" level="DEBUG" />
	
	<root>
		<level value="INFO" />
		<appender-ref ref="STDOUT" />
		<appender-ref ref="INFO" />
		<appender-ref ref="ERROR" />
		<appender-ref ref="ACCESS" />
		<appender-ref ref="SYS-USER" />
	root>
configuration>

你可能感兴趣的:(log)