如何将注销操作记录到文件中? [英] How can I log the logback actions into a file?

查看:103
本文介绍了如何将注销操作记录到文件中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用Logback将"ch.qos.logback"类记录到日志文件中,但是它只是在控制台中记录日志,而不是在文件中记录.

I'd like to log the "ch.qos.logback" classes into a log file using Logback, but it is logging just in the Console and not in the file.

有可能吗?

我需要它来调查一些有关回发的问题.

I need it for an investigation of some problems with logback.

这是我的登录配置文件:

This is my logback configuration file:

<?xml version="1.0" encoding="ISO-8859-1"?>
<configuration scan="true" scanPeriod="60 seconds">

    <property name="base-path" value="../../xpto/sysX/logs"/>
    <property name="application-name" value="app_X"/>

    <appender class="ch.qos.logback.core.ConsoleAppender" name="CONSOLE">
        <param name="Threshold" value="INFO"/> 
        <encoder>
            <pattern>%d{dd/MM/yyyy HH:mm:ss.SSS} %-5level %logger{30} - %msg%n</pattern>
        </encoder>
    </appender>
    <appender class="ch.qos.logback.core.rolling.RollingFileAppender" name="FILE_LOGBACK">
            <param name="Threshold" value="DEBUG"/>
            <file>${base-path}/mylog.log</file>
            <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
                <fileNamePattern>${base-path}/%d{yyyy-MM-dd_HH}/mylog.%i.log</fileNamePattern>
                <maxHistory>72</maxHistory>
                <timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
                    <maxFileSize>20MB</maxFileSize>
                </timeBasedFileNamingAndTriggeringPolicy>
            </rollingPolicy>
            <encoder>
                <pattern>%date{yyyy-MM-dd HH:mm:ss.SSS} [%.30thread] %-5level %logger{50} - %msg%n</pattern>
            </encoder>
        </appender>
        <appender class="ch.qos.logback.classic.AsyncAppender" name="FILE_LOGBACK_ASYNC">
            <param name="Threshold" value="DEBUG"/>
            <appender-ref ref="FILE_LOGBACK"/>
            <queueSize>1000</queueSize>
            <discardingThreshold>0</discardingThreshold>
        </appender> 
        <logger additivity="false" level="DEBUG" name="ch.qos.logback">
            <appender-ref ref="FILE_LOGBACK"/>
        </logger>
        <root level="INFO">
           <appender-ref ref="CONSOLE"/> 
           <appender-ref ref="FILE_LOGBACK_ASYNC"/>
        </root>
</configuration>

这是一个登录控制台的示例,我想将其包含在文件中,而不仅仅是在控制台中.

This is a example logged in console, I'd like to have it in the file and not just in the Console.

14:17:37,117 |-INFO in ch.qos.logback.core.joran.action.AppenderAction - Naming appender as [FILE_LOGBACK]
14:17:37,117 |-WARN in ch.qos.logback.core.joran.util.PropertySetter@7f7f557 - No setter for property [Threshold] in ch.qos.logb ack.core.rolling.RollingFileAppender.
14:17:37,118 |-INFO in c.q.l.core.rolling.TimeBasedRollingPolicy@671885015 - No compression will be used
14:17:37,118 |-INFO in c.q.l.core.rolling.TimeBasedRollingPolicy@671885015 - Will use the pattern ../../xpto/sysX/logs/%d{ yyyy-MM-dd_HH}/mylog.%i.log for the active file
14:17:37,119 |-INFO in ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP@35ca01cb - The date pattern is 'yyyy-MM-dd_HH' from fil e name pattern '.../../xpto/sysX/logs/%d{yyyy-MM-dd_HH}/mylog.%i.log'.
14:17:37,119 |-INFO in ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP@35ca01cb - Roll-over at the top of every hour.
14:17:37,119 |-INFO in ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP@35ca01cb - Setting initial period to Mon Oct 02 14:17:3

我正在使用此依赖项:

log4j-over-slf4j-1.7.2.jar
logback-classic-1.1.7.jar
logback-core-1.1.7.jar
slf4j-api-1.7.2.jar

预先感谢

推荐答案

对于背景,此输出...

For background, this ouput ...

14:17:37,117 |-INFO in ch.qos.logback.core.joran.action.AppenderAction - Naming appender as [FILE_LOGBACK]
14:17:37,117 |-WARN in ch.qos.logback.core.joran.util.PropertySetter@7f7f557 - No setter for property [Threshold] in ch.qos.logb ack.core.rolling.RollingFileAppender.
14:17:37,118 |-INFO in c.q.l.core.rolling.TimeBasedRollingPolicy@671885015 - No compression will be used
14:17:37,118 |-INFO in c.q.l.core.rolling.TimeBasedRollingPolicy@671885015 - Will use the pattern ../../xpto/sysX/logs/%d{ yyyy-MM-dd_HH}/mylog.%i.log for the active file
14:17:37,119 |-INFO in ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP@35ca01cb - The date pattern is 'yyyy-MM-dd_HH' from fil e name pattern '.../../xpto/sysX/logs/%d{yyyy-MM-dd_HH}/mylog.%i.log'.
14:17:37,119 |-INFO in ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP@35ca01cb - Roll-over at the top of every hour.
14:17:37,119 |-INFO in ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP@35ca01cb - Setting initial period to Mon Oct 02 14:17:3

...在以下情况下由Logback发出:

... is emitted by Logback under the following conditions:

  • 您的配置已启用调试(例如<configuration debug="true">...</configuration>
  • 登录已确定您的配置中有某些内容可保证发出ERROR或WARN消息
  • 在您的类路径中找到多个Logback配置文件

这些日志事件是在使用配置初始化Logback之前发出的,因此在发出它们时,Logback对配置的附加程序等一无所知.这些事件的唯一安全目标是ConsoleAppender,Logback为此目的创建了ConsoleAppender

These log events are emitted before Logback has been initialised with your configuration so at the time they are emitted Logback knows nothing about your configured appenders etc. The only safe target for these event is a ConsoleAppender which Logback creates for this purpose.

因此,无法告诉Logback将其自身的on-startup-log-events定向到文件追加器.

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

但是,您实际上想要此Logback输出吗?如果没有,那么您可以通过...之一来抑制它.

However, do you actually want this Logback output? If not then you can suppress it by either of ...

  • 消除其原因;更改您的配置,以使debug=false并且没有ERROR/WARN事件,并且在类路径上只有一个Logback配置文件
  • 将无操作状态侦听器添加到您的配置文件:<statusListener class="ch.qos.logback.core.status.NopStatusListener" />
  • Removing its cause; change your configuration so that debug=false and there are no ERROR/WARN events and there is a single Logback configuration file on the classpath
  • Add the no-op status listener to your configuration file: <statusListener class="ch.qos.logback.core.status.NopStatusListener" />

如果您确实想要此输出,则可以...

If you do want this output then you could ...

  • 在运行Java进程时,将STDOUT重定向到日志文件.例如:
    • 将此(在初始化Logback之前)添加到Java应用程序的main方法中:System.setOut(new PrintStream("/Users/al/Projects/Sarah2/std.out"));
    • 将Java输出放置到文件中:java -jar ... > /some/directory/application_stdout.log
    • Redirect STDOUT to a log file when running your Java process. For example:
      • Add this to the main method in your Java application (before initialising Logback): System.setOut(new PrintStream("/Users/al/Projects/Sarah2/std.out"));
      • Pipe the Java output to a file: java -jar ... > /some/directory/application_stdout.log

      这篇关于如何将注销操作记录到文件中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆