欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

logBack用法,记录

程序员文章站 2022-04-01 20:41:54
...
1. Logback使用手册
 系统中的日志采用 slf4j+logback实现
 引入记录日志模块
 Pom.xml文件中配置
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version> 1.0.13 </version>
</dependency>
    代码:
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Logger log = LoggerFactory.getLogger(Clazz.class);
 logback.xml配置文件中
<root level=”INFO”>
<appender-ref=”STDOUT” />   在上生产系统时去掉。
<appender-ref=”allRolling” />
<appender-ref=”errorRolling” />
</root>
2. 程序中记录日志常用方法
 Logger.error(“the error code is {}”,”1111”);
其中{}是要替换的参数,在评估是否作记录后,仅当需要作记录时,logger 才会格式化消息,
值替换"{}"。换句话说,当记录语句被禁用时,这种方法不会产生参数构造所带来的性能消耗。
 Logger.error(“the error code is {}, the reason is{} the money is {}”,“1111”,”reason”,99.9);
此方法适合多个参数
 如需打印出异常堆栈信息
Logger.error(“the error code is {}, the reason is{} the money is {}”,“1111”,”reason”,99.9,e);
此方法适合多个参数并打印出异常信息
3. 日志输出效果

4. 日志的自定义扩展
如需定义act.biz业务日志类型
配置如下:
 Logger log = LoggerFactory.getLogger(“act.biz”);
 配置文件追加
<logger name=" act.biz " level="INFO" >
  <appender-ref ref="actbizRolling"/>
  </logger>

<appender name=" actbizRolling"  class="ch.qos.logback.core.rolling.RollingFileAppender">
   <file>${LOG_HOME_SERVICE}/paff-${bySecond}.txt</file>
   <rollingPolicy  class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
  <fileNamePattern>${LOG_HOME_SERVICE}/paff-%d{yyyy-MM-dd_HH-mm}.%i.zip</fileNamePattern>
    <maxHistory>30</maxHistory>
     <TimeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
    <MaxFileSize>10MB</MaxFileSize>
</TimeBasedFileNamingAndTriggeringPolicy>   
</rollingPolicy>
   <encoder>
      <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
      <immediateFlush>false</immediateFlush>
   </encoder>
</appender>
5. FAQ
 日志不输出的一个原因
<immediateFlush>false</immediateFlush> ,logback会buffer,然后输入.所有时看到文件为空



<?xml version="1.0" encoding="UTF-8" ?>
<configuration>

	<property file="/proj/envconfig/projectName/env.properties" />

	<substitutionProperty name="LOG_HOME_ALL"
		value="${LOG_HOME}/${PROJECT_NAME}/all" />
	<substitutionProperty name="LOG_HOME_ERROR"
		value="${LOG_HOME}/${PROJECT_NAME}/error" />
		
	<timestamp key="bySecond" datePattern="yyyyMMdd'T'HHmmss" />

	<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
		<encoder>
			<pattern>
				%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n
			</pattern>
			<immediateFlush>false</immediateFlush>
		</encoder>
	</appender>

	<appender name="allRolling"
		class="ch.qos.logback.core.rolling.RollingFileAppender">
		<file>${LOG_HOME_ALL}/${PROJECT_NAME}-all.log</file>
		<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
			<fileNamePattern>${LOG_HOME_ALL}/${PROJECT_NAME}-all-%d{yyyyMMdd}.%i.zip
			</fileNamePattern>
			<maxHistory>30</maxHistory>
			<TimeBasedFileNamingAndTriggeringPolicy
				class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
				<MaxFileSize>100MB</MaxFileSize>
			</TimeBasedFileNamingAndTriggeringPolicy>
		</rollingPolicy>
		<encoder>
			<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n
			</pattern>
			<charset>UTF-8</charset>
			<immediateFlush>false</immediateFlush>
		</encoder>
	</appender>

	<!-- error appender -->
	<appender name="errorRolling"
		class="ch.qos.logback.core.rolling.RollingFileAppender">
		<filter class="ch.qos.logback.classic.filter.LevelFilter">
			<level>ERROR</level>
			<onMatch>ACCEPT</onMatch>
			<onMismatch>DENY</onMismatch>
		</filter>

		<file>${LOG_HOME_ERROR}/${PROJECT_NAME}-error.log</file>

		<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
			<fileNamePattern>${LOG_HOME_ERROR}/${PROJECT_NAME}-error-%d{yyyyMMdd}.%i.zip
			</fileNamePattern>
			<maxHistory>30</maxHistory>
			<TimeBasedFileNamingAndTriggeringPolicy
				class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
				<MaxFileSize>100MB</MaxFileSize>
			</TimeBasedFileNamingAndTriggeringPolicy>
		</rollingPolicy>

		<encoder>
			<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n
			</pattern>
			<charset>UTF-8</charset>
			<immediateFlush>false</immediateFlush>
		</encoder>
	</appender>
		
	<logger name="proj.all" level="INFO">
		<appender-ref ref="allRolling" />
	</logger>
	
	<!-- root logger -->
	<root level="INFO">
		<!-- only for local test -->
		<!-- <appender-ref ref="STDOUT" /> -->
		<appender-ref ref="allRolling" />
		<appender-ref ref="errorRolling" />
	</root>

</configuration>

相关标签: java logback