关于 logback 使用遇到问题与解决问题记录

logback.xml文件中的配置

<?xml version="1.0" encoding="UTF-8" ?>
<!-- 日志组件启动时,打印调试信息,并监控此文件变化,周期300秒 -->
<configuration scan="true" scanPeriod="300 seconds" debug="false">
	<!--针对jul的性能优化 -->
	<contextListener class="ch.qos.logback.classic.jul.LevelChangePropagator">
		<resetJUL>true</resetJUL>
	</contextListener>
	<!-- 配置文件,包括此文件内的所有变量的配置 -->
	<property name="LOG_PATH" value="${user.dir}/logs" />
	<property name="APP_NAME" value="server" />
	<!-- contextName主要是为了区分在一个web容器下部署多个应用启用jmx时,不会出现混乱 -->
	<contextName>${APP_NAME}</contextName>
	<!-- ***************************************************************** -->
	<!-- 配置输出到控制台,仅在开发测试时启用输出到控制台 ,下面的语句在window环境下生效,使用mac或者ubuntu的同学,请自己构造下 -->
	<!-- ***************************************************************** -->
	<if condition='property("os.name").toUpperCase().contains("WINDOWS") || property("os.name").toUpperCase().contains("MAC")'>
		<then>
			<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
				<encoder>
					<pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} %-5level [%thread] %logger{0}:%L- %msg%n</pattern>
				</encoder>
			</appender>
			<root>
				<appender-ref ref="STDOUT" />
			</root>
		</then>
	</if>
	<!-- ***************************************************************** -->
	<!-- info级别的日志appender -->
	<!-- ***************************************************************** -->
	<appender name="APP-INFO" class="ch.qos.logback.core.rolling.RollingFileAppender">
		<file>${LOG_PATH}/${APP_NAME}-info-30dt.log</file>
		<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
			<fileNamePattern>${LOG_PATH}/${APP_NAME}-info-30dt.log.%d{yyyy-MM-dd}.%i
			</fileNamePattern>
			<timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
				<maxFileSize>1024MB</maxFileSize>
			</timeBasedFileNamingAndTriggeringPolicy>
		</rollingPolicy>
		<encoder>
			<pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} %-5level [%thread] %logger{0}:%L- %msg%n</pattern>
		</encoder>
	</appender>
	<!-- ***************************************************************** -->
	<!-- error级别日志appender -->
	<!-- ***************************************************************** -->
	<appender name="APP-ERR" class="ch.qos.logback.core.rolling.RollingFileAppender">
		<file>${LOG_PATH}/${APP_NAME}-error-30dt.log</file>
		<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
			<fileNamePattern>${LOG_PATH}/${APP_NAME}-error-30dt.%d{yyyy-MM-dd}.%i
			</fileNamePattern>
			<timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
				<maxFileSize>1024MB</maxFileSize>
			</timeBasedFileNamingAndTriggeringPolicy>
		</rollingPolicy>
		<filter class="ch.qos.logback.classic.filter.ThresholdFilter">
			<level>ERROR</level>
		</filter>
		<encoder>
			<pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} %-5level [%thread] %logger{0}:%L- %msg%n</pattern>
		</encoder>
	</appender>

	<!-- 根日志logger -->
	<root level="DEBUG">
		<appender-ref ref="APP-ERR" />
		<appender-ref ref="APP-INFO" />
		<appender-ref ref="STDOUT" />
	</root>

</configuration>

遇到问题(1)

SLF4J: Class path contains multiple SLF4J bindings.
SLF4J: Found binding in [jar:file:/Users/shuang/maven_repository/ch/qos/logback/logback-classic/1.0.13/logback-classic-1.0.13.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: Found binding in [jar:file:/Users/shuang/maven_repository/org/slf4j/slf4j-log4j12/1.7.12/slf4j-log4j12-1.7.12.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: See http://www.slf4j.org/codes.html#multiple_bindings for an explanation
SLF4J: Actual binding is of type [ch.qos.logback.classic.util.ContextSelectorStaticBinder]
log4j:WARN No appenders could be found for logger (com.alibaba.druid.pool.DruidDataSource).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.

 上面的问题是因为存在相同的jar包,检查一下是否存在slf4j与log4j等相关重复jar包

遇到问题(2)

14:49:53,026 |-ERROR in ch.qos.logback.core.joran.conditional.IfAction - Could not find Janino library on the class path. Skipping conditional processing.
14:49:53,026 |-ERROR in ch.qos.logback.core.joran.conditional.IfAction - See also http://logback.qos.ch/codes.html#ifJanino
14:49:53,125 |-ERROR in ch.qos.logback.core.joran.action.AppenderRefAction - Could not find an appender named [STDOUT]. Did you define it below instead of above in the configuration file?
14:49:53,125 |-ERROR in ch.qos.logback.core.joran.action.AppenderRefAction - See http://logback.qos.ch/codes.html#appender_order for more details.

 上面的问题是缺少下面的jar包

   <!-- The org.codehaus.janino:commons-compiler:2.6.1 dependency -->
    <!-- will be automatically pulled in by Maven's transitivity rules -->
    <dependency>
        <groupId>org.codehaus.janino</groupId>
        <artifactId>janino</artifactId>
        <version>2.6.1</version>
    </dependency>

猜你喜欢

转载自yeshuang.iteye.com/blog/2378747