java日志之logback

最近看一个项目的日志的时候,发现一个问题,所有的输出日志,都打印了两次

启动日志如下:

2020-09-14 23:04:46 [main] INFO  o.s.c.s.PostProcessorRegistrationDelegate$BeanPostProcessorChecker - Bean 'configurationPropertiesRebinderAutoConfiguration' of type [org.springframework.cloud.autoconfigure.ConfigurationPropertiesRebinderAutoConfiguration$$EnhancerBySpringCGLIB$$1478a6f0] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2020-09-14 23:04:46 [main] INFO  o.s.c.s.PostProcessorRegistrationDelegate$BeanPostProcessorChecker - Bean 'configurationPropertiesRebinderAutoConfiguration' of type [org.springframework.cloud.autoconfigure.ConfigurationPropertiesRebinderAutoConfiguration$$EnhancerBySpringCGLIB$$1478a6f0] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2020-09-14 23:04:46 [main] INFO  o.s.c.n.eureka.InstanceInfoFactory - Setting initial instance status as: STARTING
2020-09-14 23:04:46 [main] INFO  o.s.c.n.eureka.InstanceInfoFactory - Setting initial instance status as: STARTING

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::       (v1.5.12.RELEASE)

2020-09-14 23:04:47 [main] INFO  o.s.c.c.c.ConfigServicePropertySourceLocator - Fetching config from server at: http://192.168.0.108:9201/
2020-09-14 23:04:47 [main] INFO  o.s.c.c.c.ConfigServicePropertySourceLocator - Fetching config from server at: http://192.168.0.108:9201/

所以,看一下SpringCloud+logback打印日志,是配置文件哪里出了问题?顺便总结下logback.xml的配置文件详解。
遇到的日志打印两次的问题,也在这篇详解中,logback.xml文件如下

<?xml version="1.0" encoding="UTF-8"?>
<!--在此未说明属性是非必须的,表示属性必须设置-->
<!--最小配置文件结构介绍-->
<!--
	<configuration>
		//存在一个或者多个appender,功能指定记录输出目的地,如项目日志:user.log,error.log等
		<appender></appender>
		//最多存在一个,根logger
		<root></root>
		//存在一个或者多个,普通logger
		<logger></logger>
	</configuration>
-->

<!--configuration 的属性-->
<!--
	 debug(非必须)属性:true表示输出logback内部的日志信息(不影响配置和级别) ;
     scan(非必须)属性:默认为false,true表示扫描配置文件的变化并自动重新配置,默认每隔1分钟扫描一次;
     scanPeriod(非必须)属性:搭配scan属性使用,设置扫描的间隔时间
     如:
     <configuration debug="true" scan="true" scanPeriod="1 seconds">
-->
<configuration>

	<!--设置了一个局部变量 DEV_HOME,这里的值是logs,在代码中有个根目录下的文件夹,logs-->
	<!--相当于有些博客中的FILE_PATH ,用于指定appender的目的文件存放的目录-->
	<property name="DEV_HOME" value="logs" />

	<!--这个appender的功能是将记录信息以特定格式写到控制台-->
	<!--这里使用的是layout,其实可以使用encoder代替-->
	<!--encoder:将事件转换为字符串
            默认配置为PatternLayoutEncoder类
            encoder用于替代Layout,encoder扩展了Layout功能
            Layout功能:只负责把事件转换为字符串,但是不能指定何时将记录写入到指定目的地
            encoder功能:即负责把事件转换为字符串,也可以指定何时将记录写入到指定目的地
         -->
	<!-- 指定输出格式
                %d{} :表示时间
                %thread:请求记录所在的线程名
                %-5level:用5位显示请求级别
                %logger{36}:输出logger名,{36}表示logger名最大占用的字符位数,{0}表示最简短logger名(不包含包名)。
             -->
	<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
		<layout class="ch.qos.logback.classic.PatternLayout">
			<Pattern>
				%d{yyyy-MM-dd HH:mm:ss} [%thread] %-5level %logger{36} - %msg%n
			</Pattern>
		</layout>
	</appender>

	<!-- 该appender的功能是将记录信息以特定格式写到文件,例如此处是cart.log -->
	<appender name="FILE-AUDIT"
		class="ch.qos.logback.core.rolling.RollingFileAppender">
		<!-- $使用变量DEV_HOME的格式,类似Linux中使用的格式:${DEV_HOME} -->
		<file>${DEV_HOME}/cart.log</file>
		<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
			<Pattern>
				%d{yyyy-MM-dd HH:mm:ss} - %msg%n
			</Pattern>
		</encoder>

		<!--
        ***********************以最小窗体为指定的滚动规则的appender*****************************
        RollingFileAppender类型的appender中必须包含4个子标签:<file>、<rollingPolicy>、<triggerPolicy>、<encoder>
        我这里直接就将rollingPolicy 写在了相应的appender中
        <rollingPolicy>标签用于指定滚动规则,该标签有一个属性class:用于指定实现具体的滚动规则的类。
        <triggerPolicy>标签用于指定发生滚动的条件,该标签有一个属性class:用于指定具体滚动条件的类。
        <rollingPolicy>和<triggerPolicy>中具体包含哪些子标签是由class属性指定的类来决定的,因为不同的类有不同的参数,从而需要不同的标签来传参
     -->
		<!--根据时间滚动 为滚动规则和条件的appender(最常用)-->
		<!-- 该功能:将记录信息以特定格式写到文件,当文件达到10MB时,创建以时间为特定规则新的 文件,之后的记录会写到新文件,
            文件个数最多维持10个,文件达到10个 后删除旧的文件-->
		<!--
			TimeBasedRollingPolicy实现了RollingPolicy与TriggeringPolicy,
            因此只需要<rollingPolicy>标签,不需要<TriggeringPolicy>标签
            <rollingPolicy>标签有两个子标签:<fileNamePattern>、<maxHistory>
            <fileNamePattern>:用于指定文件名命名规则
            <maxHistory>:保留文件的个数,超过了就删除创建时间最久的文件
            我这里没有写保留的文件个数,项目日志很多,不删除
         -->
		<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
			<!-- rollover daily -->
			<fileNamePattern>${DEV_HOME}/archived/cart.%d{yyyy-MM-dd}.%i.log
			</fileNamePattern>
			<timeBasedFileNamingAndTriggeringPolicy
				class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
				<maxFileSize>10MB</maxFileSize>
			</timeBasedFileNamingAndTriggeringPolicy>
		</rollingPolicy>
	</appender>

	<appender name="FILE-ERROR"
		class="ch.qos.logback.core.rolling.RollingFileAppender">
		<file>${DEV_HOME}/error.log</file>
		<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
			<Pattern>
				%d{yyyy-MM-dd HH:mm:ss} - %msg%n
			</Pattern>
		</encoder>

		<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
			<!-- rollover daily -->
			<fileNamePattern>${DEV_HOME}/archived/error.%d{yyyy-MM-dd}.%i.log
			</fileNamePattern>
			<timeBasedFileNamingAndTriggeringPolicy
				class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
				<maxFileSize>10MB</maxFileSize>
			</timeBasedFileNamingAndTriggeringPolicy>
		</rollingPolicy>
	</appender>

	<!-- Send logs to both console and file audit -->
	<!-- name:指定logger名称;level:指定logger的分配级别;additivity(非必须,默认为true):设置appender叠加性 -->
	<logger name="com.aa" level="debug" additivity="false">
		<appender-ref ref="FILE-AUDIT" />
		<appender-ref ref="STDOUT" />
	</logger>
	<logger name="org.springframework" level="info">
		<!--错误在此,导致了日志输出了两次,注释掉即可-->
		<!--或者在上面的logger中加一个 additivity="false" -->
		<!--<appender-ref ref="FILE-AUDIT" />
		<appender-ref ref="STDOUT" />-->
	</logger>

	<!--springcloud的注册中心客户端会每隔一定时间向注册中心服务端发送心跳,用此来判断注册中心服务端是否运行正常。
		这样导致不断进行日志输出,不便查看正常的业务日志输出-->
	<logger name="com.netflix.discovery.shared.resolver.aws.ConfigClusterResolver" level="warn" />
	<logger name="org.apache.http" level="info" />
	<logger name="com.netflix.discovery" level="warn" />
	<logger name="com.netflix.loadbalancer" level="warn" />


	<!-- level属性:指定根logger的分配级别 -->
	<root level="info">
		<!-- ref属性:指定根logger关联的appender -->
		<appender-ref ref="STDOUT" />
		<appender-ref ref="FILE-AUDIT" />
		<appender-ref ref="FILE-ERROR" />
	</root>

</configuration>

备注:

其实看springcloud的启动日志,可以看出springboot项目启动怎么读取配置文件的,这个之后写springboot笔记的时候输出下,启动日志如下:

Connected to the target VM, address: '127.0.0.1:63688', transport: 'socket'
23:54:17,234 |-INFO in ch.qos.logback.classic.LoggerContext[default] - Could NOT find resource [logback.groovy]
23:54:17,234 |-INFO in ch.qos.logback.classic.LoggerContext[default] - Could NOT find resource [logback-test.xml]
23:54:17,234 |-INFO in ch.qos.logback.classic.LoggerContext[default] - Found resource [logback.xml] at [file:/C:/workspace/gongfeng_workspace/cart/target/classes/logback.xml]
23:54:17,281 |-INFO in ch.qos.logback.classic.joran.action.ConfigurationAction - debug attribute not set
23:54:17,286 |-INFO in ch.qos.logback.core.joran.action.AppenderAction - About to instantiate appender of type [ch.qos.logback.core.ConsoleAppender]
23:54:17,289 |-INFO in ch.qos.logback.core.joran.action.AppenderAction - Naming appender as [STDOUT]
23:54:17,327 |-WARN in ch.qos.logback.core.ConsoleAppender[STDOUT] - This appender no longer admits a layout as a sub-component, set an encoder instead.
23:54:17,327 |-WARN in ch.qos.logback.core.ConsoleAppender[STDOUT] - To ensure compatibility, wrapping your layout in LayoutWrappingEncoder.
23:54:17,327 |-WARN in ch.qos.logback.core.ConsoleAppender[STDOUT] - See also http://logback.qos.ch/codes.html#layoutInsteadOfEncoder for details
23:54:17,327 |-INFO in ch.qos.logback.core.joran.action.AppenderAction - About to instantiate appender of type [ch.qos.logback.core.rolling.RollingFileAppender]
23:54:17,330 |-INFO in ch.qos.logback.core.joran.action.AppenderAction - Naming appender as [FILE-AUDIT]
23:54:17,341 |-INFO in c.q.l.core.rolling.TimeBasedRollingPolicy@1212772528 - No compression will be used
23:54:17,341 |-INFO in c.q.l.core.rolling.TimeBasedRollingPolicy@1212772528 - Will use the pattern logs/archived/cart.%d{yyyy-MM-dd}.%i.log for the active file
23:54:17,343 |-INFO in ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP@4470f8a6 - The date pattern is 'yyyy-MM-dd' from file name pattern 'logs/archived/cart.%d{yyyy-MM-dd}.%i.log'.
23:54:17,343 |-INFO in ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP@4470f8a6 - Roll-over at midnight.
23:54:17,347 |-INFO in ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP@4470f8a6 - Setting initial period to Mon Sep 14 23:51:46 CST 2020
23:54:17,347 |-WARN in ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP@4470f8a6 - SizeAndTimeBasedFNATP is deprecated. Use SizeAndTimeBasedRollingPolicy instead
23:54:17,347 |-WARN in ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP@4470f8a6 - For more information see http://logback.qos.ch/manual/appenders.html#SizeAndTimeBasedRollingPolicy
23:54:17,349 |-INFO in ch.qos.logback.core.rolling.RollingFileAppender[FILE-AUDIT] - Active log file name: logs/cart.log
23:54:17,349 |-INFO in ch.qos.logback.core.rolling.RollingFileAppender[FILE-AUDIT] - File property is set to [logs/cart.log]
23:54:17,350 |-INFO in ch.qos.logback.core.joran.action.AppenderAction - About to instantiate appender of type [ch.qos.logback.core.rolling.RollingFileAppender]
23:54:17,350 |-INFO in ch.qos.logback.core.joran.action.AppenderAction - Naming appender as [FILE-ERROR]
23:54:17,351 |-INFO in c.q.l.core.rolling.TimeBasedRollingPolicy@2089016471 - No compression will be used
23:54:17,351 |-INFO in c.q.l.core.rolling.TimeBasedRollingPolicy@2089016471 - Will use the pattern logs/archived/error.%d{yyyy-MM-dd}.%i.log for the active file
23:54:17,352 |-INFO in ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP@7748410a - The date pattern is 'yyyy-MM-dd' from file name pattern 'logs/archived/error.%d{yyyy-MM-dd}.%i.log'.
23:54:17,352 |-INFO in ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP@7748410a - Roll-over at midnight.
23:54:17,352 |-INFO in ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP@7748410a - Setting initial period to Mon Sep 14 23:51:46 CST 2020
23:54:17,352 |-WARN in ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP@7748410a - SizeAndTimeBasedFNATP is deprecated. Use SizeAndTimeBasedRollingPolicy instead
23:54:17,352 |-WARN in ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP@7748410a - For more information see http://logback.qos.ch/manual/appenders.html#SizeAndTimeBasedRollingPolicy
23:54:17,352 |-INFO in ch.qos.logback.core.rolling.RollingFileAppender[FILE-ERROR] - Active log file name: logs/error.log
23:54:17,352 |-INFO in ch.qos.logback.core.rolling.RollingFileAppender[FILE-ERROR] - File property is set to [logs/error.log]
23:54:17,353 |-INFO in ch.qos.logback.classic.joran.action.LoggerAction - Setting level of logger [com.aa] to DEBUG
23:54:17,353 |-INFO in ch.qos.logback.classic.joran.action.LoggerAction - Setting additivity of logger [com.aa] to false
23:54:17,353 |-INFO in ch.qos.logback.core.joran.action.AppenderRefAction - Attaching appender named [FILE-AUDIT] to Logger[com.aa]
23:54:17,354 |-INFO in ch.qos.logback.core.joran.action.AppenderRefAction - Attaching appender named [STDOUT] to Logger[com.aa]
23:54:17,354 |-INFO in ch.qos.logback.classic.joran.action.LoggerAction - Setting level of logger [org.springframework] to INFO
23:54:17,354 |-INFO in ch.qos.logback.classic.joran.action.LoggerAction - Setting level of logger [com.netflix.discovery.shared.resolver.aws.ConfigClusterResolver] to WARN
23:54:17,354 |-INFO in ch.qos.logback.classic.joran.action.LoggerAction - Setting level of logger [org.apache.http] to INFO
23:54:17,354 |-INFO in ch.qos.logback.classic.joran.action.LoggerAction - Setting level of logger [com.netflix.discovery] to WARN
23:54:17,354 |-INFO in ch.qos.logback.classic.joran.action.LoggerAction - Setting level of logger [com.netflix.loadbalancer] to WARN
23:54:17,354 |-INFO in ch.qos.logback.classic.joran.action.RootLoggerAction - Setting level of ROOT logger to INFO
23:54:17,354 |-INFO in ch.qos.logback.core.joran.action.AppenderRefAction - Attaching appender named [STDOUT] to Logger[ROOT]
23:54:17,354 |-INFO in ch.qos.logback.core.joran.action.AppenderRefAction - Attaching appender named [FILE-AUDIT] to Logger[ROOT]
23:54:17,354 |-INFO in ch.qos.logback.core.joran.action.AppenderRefAction - Attaching appender named [FILE-ERROR] to Logger[ROOT]
23:54:17,354 |-INFO in ch.qos.logback.classic.joran.action.ConfigurationAction - End of configuration.
23:54:17,354 |-INFO in ch.qos.logback.classic.joran.JoranConfigurator@740773a3 - Registering current configuration as safe fallback point

2020-09-14 23:54:17 [main] INFO  o.s.c.a.AnnotationConfigApplicationContext - Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@1623134f: startup date [Mon Sep 14 23:54:17 CST 2020]; root of context hierarchy
2020-09-14 23:54:17 [background-preinit] INFO  o.h.validator.internal.util.Version - HV000001: Hibernate Validator 5.3.6.Final
2020-09-14 23:54:17 [main] INFO  o.s.b.f.a.AutowiredAnnotationBeanPostProcessor - JSR-330 'javax.inject.Inject' annotation found and supported for autowiring
2020-09-14 23:54:18 [main] INFO  o.s.c.s.PostProcessorRegistrationDelegate$BeanPostProcessorChecker - Bean 'configurationPropertiesRebinderAutoConfiguration' of type [org.springframework.cloud.autoconfigure.ConfigurationPropertiesRebinderAutoConfiguration$$EnhancerBySpringCGLIB$$8a7f2f69] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2020-09-14 23:54:18 [main] INFO  o.s.c.n.eureka.InstanceInfoFactory - Setting initial instance status as: STARTING

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::       (v1.5.12.RELEASE)

2020-09-14 23:54:19 [main] INFO  o.s.c.c.c.ConfigServicePropertySourceLocator - Fetching config from server at: http://192.168.0.108:9201/
2020-09-14 23:54:19 [main] INFO  o.s.c.c.c.ConfigServicePropertySourceLocator - Located environment: name=cart, profiles=[default], label=null, version=3b69dd3b641d995d2197ba7a14db05f77479efcb, state=null
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值