我需要在记录器中打印类,方法和行号。为此,我使用以下条目:
<appender name="ErrorFileAppender" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${log.dir}error.log</file>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<!-- daily rollover -->
<fileNamePattern>${log.dir}error.%d{yyyy-MM-dd}.%i.log</fileNamePattern>
<maxHistory>3</maxHistory>
<!--<totalSizeCap>10MB</totalSizeCap>-->
<TimeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
<MaxFileSize>10MB</MaxFileSize>
</TimeBasedFileNamingAndTriggeringPolicy>
</rollingPolicy>
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<Pattern>
%d %-5p [%X{sessionId}] [%X{userId}] %C{1}.%-M:%L] %m%n
</Pattern>
</encoder>
<!--<encoder>
<pattern>%d %-5p [%X{sessionId}] [%X{userId}] %C{1}.%-M:%L] %m%n</pattern>
</encoder>-->
</appender>
<root level="ERROR">
<appender-ref ref="ErrorFileAppender" />
</root>
如您所见,我的模式是
%d %-5p [%X{sessionId}] [%X{userId}] %C{1}.%-M:%L] %m%n
然后我收到以下错误:
09:31:44,119 |-ERROR in ch.qos.logback.core.joran.spi.Interpreter@29:19 - RuntimeException in Action for tag [encoder] java.lang.NumberFormatException: For input string: "-"
at java.lang.NumberFormatException: For input string: "-"
at at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
at at java.lang.Integer.parseInt(Integer.java:572)
at at java.lang.Integer.parseInt(Integer.java:615)
at at ch.qos.logback.core.pattern.FormatInfo.valueOf(FormatInfo.java:73)
at at ch.qos.logback.core.pattern.parser.Parser.T(Parser.java:136)
at at ch.qos.logback.core.pattern.parser.Parser.E(Parser.java:96)
at at ch.qos.logback.core.pattern.parser.Parser.Eopt(Parser.java:115)
at at ch.qos.logback.core.pattern.parser.Parser.E(Parser.java:100)
at at ch.qos.logback.core.pattern.parser.Parser.Eopt(Parser.java:115)
at at ch.qos.logback.core.pattern.parser.Parser.E(Parser.java:100)
at at ch.qos.logback.core.pattern.parser.Parser.Eopt(Parser.java:115)
at at ch.qos.logback.core.pattern.parser.Parser.E(Parser.java:100)
at at ch.qos.logback.core.pattern.parser.Parser.Eopt(Parser.java:115)
at at ch.qos.logback.core.pattern.parser.Parser.E(Parser.java:100)
at at ch.qos.logback.core.pattern.parser.Parser.Eopt(Parser.java:115)
at at ch.qos.logback.core.pattern.parser.Parser.E(Parser.java:100)
at at ch.qos.logback.core.pattern.parser.Parser.Eopt(Parser.java:115)
at at ch.qos.logback.core.pattern.parser.Parser.E(Parser.java:100)
at at ch.qos.logback.core.pattern.parser.Parser.Eopt(Parser.java:115)
at at ch.qos.logback.core.pattern.parser.Parser.E(Parser.java:100)
at at ch.qos.logback.core.pattern.parser.Parser.Eopt(Parser.java:115)
at at ch.qos.logback.core.pattern.parser.Parser.E(Parser.java:100)
at at ch.qos.logback.core.pattern.parser.Parser.parse(Parser.java:91)
at at ch.qos.logback.core.pattern.PatternLayoutBase.start(PatternLayoutBase.java:83)
at at ch.qos.logback.classic.encoder.PatternLayoutEncoder.start(PatternLayoutEncoder.java:28)
at at ch.qos.logback.core.joran.action.NestedComplexPropertyIA.end(NestedComplexPropertyIA.java:161)
at at ch.qos.logback.core.joran.spi.Interpreter.callEndAction(Interpreter.java:309)
at at ch.qos.logback.core.joran.spi.Interpreter.endElement(Interpreter.java:193)
at at ch.qos.logback.core.joran.spi.Interpreter.endElement(Interpreter.java:179)
at at ch.qos.logback.core.joran.spi.EventPlayer.play(EventPlayer.java:62)
at at ch.qos.logback.core.joran.GenericConfigurator.doConfigure(GenericConfigurator.java:165)
at at ch.qos.logback.core.joran.GenericConfigurator.doConfigure(GenericConfigurator.java:152)
at at ch.qos.logback.core.joran.GenericConfigurator.doConfigure(GenericConfigurator.java:110)
at at ch.qos.logback.core.joran.GenericConfigurator.doConfigure(GenericConfigurator.java:53)
at at ch.qos.logback.classic.util.ContextInitializer.configureByResource(ContextInitializer.java:75)
at at ch.qos.logback.classic.util.ContextInitializer.autoConfig(ContextInitializer.java:150)
at at org.slf4j.impl.StaticLoggerBinder.init(StaticLoggerBinder.java:84)
at at org.slf4j.impl.StaticLoggerBinder.<clinit>(StaticLoggerBinder.java:55)
at at org.slf4j.LoggerFactory.bind(LoggerFactory.java:150)
at at org.slf4j.LoggerFactory.performInitialization(LoggerFactory.java:124)
at at org.slf4j.LoggerFactory.getILoggerFactory(LoggerFactory.java:412)
at at org.slf4j.LoggerFactory.getLogger(LoggerFactory.java:357)
at at org.apache.commons.logging.impl.SLF4JLogFactory.getInstance(SLF4JLogFactory.java:155)
at at org.apache.commons.logging.impl.SLF4JLogFactory.getInstance(SLF4JLogFactory.java:132)
at at org.apache.commons.logging.LogFactory.getLog(LogFactory.java:273)
at at org.springframework.context.support.AbstractApplicationContext.<init>(AbstractApplicationContext.java:164)
at at org.springframework.context.support.AbstractApplicationContext.<init>(AbstractApplicationContext.java:228)
at at org.springframework.context.support.AbstractRefreshableApplicationContext.<init>(AbstractRefreshableApplicationContext.java:89)
at at org.springframework.context.support.AbstractRefreshableConfigApplicationContext.<init>(AbstractRefreshableConfigApplicationContext.java:58)
at at org.springframework.context.support.AbstractXmlApplicationContext.<init>(AbstractXmlApplicationContext.java:61)
at at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:136)
at at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:83)
但如果我使用
%d %-5p [%X{sessionId}] [%X{userId}] %m%n
然后没有错误。
有人可以看看。这是我的pom条目:
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>jcl-over-slf4j</artifactId>
<version>1.7.25</version>
</dependency>
<dependency>
<groupId>org.logback-extensions</groupId>
<artifactId>logback-ext-spring</artifactId>
<version>0.1.4</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.2.3</version>
<scope>test</scope>
</dependency>
答案 0 :(得分:0)
将%-M
更改为%M
。
所以你的模式变成了:
<Pattern>
%d %-5p [%X{sessionId}] [%X{userId}] %C{1}.%M:%L] %m%n
</Pattern>