是否可以使用logback.xml配置来自ch.qos.logback的日志消息?

时间:2018-11-09 13:18:14

标签: logback

我已成功将Logback用于应用程序,但无法记录来自ch.qos.logback的消息。我知道有人无法记录初始消息(下面的答案确认了这一点),这很有意义。我不明白的是为什么我无法记录登录完成自我配置后生成的消息。

So, there is no way to tell Logback to direct its own on-startup-log-events to a file appender.

"The first log output by ch.qos.logback can't be redirected using the log configuration because this output happens while Logback reads it's configuration -> hen / egg style problem."

logback.xml(当无法登录电子邮件服务器时,此确切代码将从ch.qos.logback.classic.net.SMTPAppender生成ERROR消息):

<?xml version="1.0" encoding="UTF-8"?>
<!--  see http://logback.qos.ch/manual/configuration.html -->

<configuration scan="true" debug="true">
  <timestamp key="bySecond" datePattern="yyyyMMdd'T'HHmmss"/> 

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

  <appender name="testFile" class="ch.qos.logback.core.FileAppender">
    <file> testFile.txt </file>
    <append>false</append>
    <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
        <Pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg~%n</Pattern>
    </encoder>
  </appender>

   <appender name="GMAIL_EMAIL" class="ch.qos.logback.classic.net.SMTPAppender">
    <smtpHost>smtp.gmail.com</smtpHost>
    <smtpPort>587</smtpPort>
    <asynchronousSending>true</asynchronousSending>

    <username>xxx</username>
    <password>xxx</password>
    <STARTTLS>true</STARTTLS>

    <to>xxx</to>
    <from>xxx</from>
    <subject>Error: %logger{20} - %m</subject>
    <layout class="ch.qos.logback.classic.PatternLayout">
      <Pattern>%d{HH:mm:ss} [%thread] %-5level %logger{36} - %msg~%n</Pattern>
   </layout> 
   </appender>

  <root level="INFO">
    <appender-ref ref="STDOUT" />
    <appender-ref ref="testFile" />
    <appender-ref ref="GMAIL_EMAIL" />
  </root>

  <logger name="ch.qos.logback" level="DEBUG" >
  </logger> 

</configuration>

调试控制台输出:

13:05:07,626 |-INFO in ch.qos.logback.classic.LoggerContext[default] - Found resource [conf\logback.xml] at [file:/C:/OleAppnWorkArea/staging__unity_group14b/conf/logback.xml]
13:05:07,806 |-INFO in ReconfigureOnChangeFilter{invocationCounter=0} - Will scan for changes in [[C:\OleAppnWorkArea\staging__unity_group14b\conf\logback.xml]] every 60 seconds. 
13:05:07,806 |-INFO in ch.qos.logback.classic.joran.action.ConfigurationAction - Adding ReconfigureOnChangeFilter as a turbo filter
13:05:07,806 |-INFO in ch.qos.logback.core.joran.action.TimestampAction - Using current interpretation time, i.e. now, as time reference.
13:05:07,816 |-INFO in ch.qos.logback.core.joran.action.TimestampAction - Adding property to the context with key="bySecond" and value="20181109T130507" to the LOCAL scope
13:05:07,816 |-INFO in ch.qos.logback.core.joran.action.AppenderAction - About to instantiate appender of type [ch.qos.logback.core.ConsoleAppender]
13:05:07,816 |-INFO in ch.qos.logback.core.joran.action.AppenderAction - Naming appender as [STDOUT]
13:05:07,882 |-INFO in ch.qos.logback.core.joran.action.AppenderAction - About to instantiate appender of type [ch.qos.logback.core.FileAppender]
13:05:07,887 |-INFO in ch.qos.logback.core.joran.action.AppenderAction - Naming appender as [testFile]
13:05:07,897 |-INFO in ch.qos.logback.core.FileAppender[testFile] - File property is set to [testFile.txt]
13:05:07,899 |-INFO in ch.qos.logback.core.joran.action.AppenderAction - About to instantiate appender of type [ch.qos.logback.classic.net.SMTPAppender]
13:05:07,910 |-INFO in ch.qos.logback.core.joran.action.AppenderAction - Naming appender as [GMAIL_EMAIL]
13:05:07,950 |-INFO in ch.qos.logback.classic.joran.action.RootLoggerAction - Setting level of ROOT logger to INFO
13:05:07,950 |-INFO in ch.qos.logback.core.joran.action.AppenderRefAction - Attaching appender named [STDOUT] to Logger[ROOT]
13:05:07,951 |-INFO in ch.qos.logback.core.joran.action.AppenderRefAction - Attaching appender named [testFile] to Logger[ROOT]
13:05:07,951 |-INFO in ch.qos.logback.core.joran.action.AppenderRefAction - Attaching appender named [GMAIL_EMAIL] to Logger[ROOT]
13:05:07,952 |-INFO in ch.qos.logback.classic.joran.action.LoggerAction - Setting level of logger [ch.qos.logback] to DEBUG
13:05:07,952 |-INFO in ch.qos.logback.classic.joran.action.ConfigurationAction - End of configuration.
13:05:07,953 |-INFO in ch.qos.logback.classic.joran.JoranConfigurator@481a996b - Registering current configuration as safe fallback point
13:05:07.966 [main] ERROR xxx - error to use for debugging logging~
13:05:07,972 |-INFO in ch.qos.logback.classic.net.SMTPAppender[GMAIL_EMAIL] - SMTPAppender [GMAIL_EMAIL] is tracking [1] buffers
13:05:07,997 |-INFO in ch.qos.logback.classic.net.SMTPAppender[GMAIL_EMAIL] - About to send out SMTP message "Error: xxx - error to use for debugging logging" to [xxx]
13:05:09,019 |-ERROR in ch.qos.logback.classic.net.SMTPAppender[GMAIL_EMAIL] - Error occurred while sending e-mail notification. javax.mail.AuthenticationFailedException: 535-5.7.8 Username and Password not accepted. Learn more at
535 5.7.8  https://support.google.com/mail/?p=BadCredentials o27-v6sm5469472wro.24 - gsmtp at javax.mail.AuthenticationFailedException: 535-5.7.8 Username and Password not accepted. Learn more at
535 5.7.8  https://support.google.com/mail/?p=BadCredentials o27-v6sm5469472wro.24 - gsmtp
    at  at com.sun.mail.smtp.SMTPTransport$Authenticator.authenticate(SMTPTransport.java:965)
    at  at com.sun.mail.smtp.SMTPTransport.authenticate(SMTPTransport.java:876)
    at  at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:780)
    at  at javax.mail.Service.connect(Service.java:388)
    at  at javax.mail.Service.connect(Service.java:246)
    at  at javax.mail.Service.connect(Service.java:195)
    at  at javax.mail.Transport.send0(Transport.java:254)
    at  at javax.mail.Transport.send(Transport.java:124)
    at  at ch.qos.logback.core.net.SMTPAppenderBase.sendBuffer(SMTPAppenderBase.java:394)
    at  at ch.qos.logback.core.net.SMTPAppenderBase$SenderRunnable.run(SMTPAppenderBase.java:677)
    at  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
    at  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
    at  at java.lang.Thread.run(Thread.java:748)

testFile.txt仅包含:

13:05:07.966 [main] ERROR xxx- error to use for debugging logging~

ch.qos.logback.classic.net.SMTPAppender生成的消息遵循与启动消息相同的模式,而不是我在附加器STDOUT中定义的模式,这表明它们甚至没有使用我的STDOUT附加器。此外,它们甚至没有出现在testFile附加器输出中。

请问有人对此有何启示?我已经阅读了文档并进行了此测试,但没有其他途径。

1 个答案:

答案 0 :(得分:0)

Logback不使用记录器报告其自己的日志输出。 Logback自己的日志称为“状态消息”。请参阅status printing and status listeners

上的文档