Logback.xml配置

时间:2013-08-13 09:09:40

标签: logback

我正在尝试配置stout以保存到文件中。  但是,它没有保存到文件中 - 你知道为什么吗?  另外 - 我希望日志文件名可以在logback.xml中配置  像{LOG_FILE_NAME}这样来自cmd的东西 - 有可能吗?

这是我的logback.xml:

 <?xml version="1.0" encoding="UTF-8"?>

<!-- For assistance related to logback-translator or configuration  -->
<!-- files in general, please contact the logback user mailing list -->
<!-- at http://www.qos.ch/mailman/listinfo/logback-user             -->
<!--                                                                -->
<!-- For professional support please see                            -->
<!--    http://www.qos.ch/shop/products/professionalSupport         -->
<!--                                                                -->
<configuration>
  <appender name="defaultLog" class="ch.qos.logback.core.rolling.RollingFileAppender">
    <!--See also http://logback.qos.ch/manual/appenders.html#RollingFileAppender-->
    <File>sarit_test.log</File>
    <encoder>
      <pattern>%d{dd MMM yyyy HH:mm:ss.SSS} [%t] %-5p %x %F:%L - %m</pattern>
    </encoder>
    <rollingPolicy class="ch.qos.logback.core.rolling.FixedWindowRollingPolicy"/>
    <triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
      <MaxFileSize>50000KB</MaxFileSize>
    </triggeringPolicy>
  </appender>
  <root level="INFO">
    <appender-ref ref="defaultLog"/>
  </root>
</configuration>

5 个答案:

答案 0 :(得分:2)

第一个答案,请点击此处: https://github.com/abdulwaheed18/Slf4jTutorial

第二个答案:您必须使用SIFT appender获取文件的系统参数。

 <appender name="SIFT" class="ch.qos.logback.classic.sift.SiftingAppender">
    <!-- in the absence of the class attribute, it is assumed that the desired 
        discriminator type is ch.qos.logback.classic.sift.MDCBasedDiscriminator -->
    <discriminator>
        <key>FILE_NAME</key>
        <defaultValue>DEFAULT_FILE_NAME</defaultValue>
    </discriminator>
    <sift>
        <appender name="FILE-${FILE_NAME}"
            class="ch.qos.logback.core.rolling.RollingFileAppender">
            <filter class="ch.qos.logback.core.filter.EvaluatorFilter">
                <evaluator> <!-- defaults to type ch.qos.logback.classic.boolex.JaninoEventEvaluator -->
                    <expression>return message.contains("Broken pipe");</expression>
                </evaluator>
                <OnMismatch>NEUTRAL</OnMismatch>
                <OnMatch>DENY</OnMatch>
            </filter>
            <File>${LOGDIR}/${FILE_NAME}.log</File>
            <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
                <FileNamePattern>${LOGDIR}/${FILE_NAME}.%d{yyyy-MM-dd}.%i.log.gz
                </FileNamePattern>  <!-- keep 30 days' worth of history -->
                <MaxHistory>30</MaxHistory>
                <!-- Limit all logs size to 300MB -->
                <timeBasedFileNamingAndTriggeringPolicy
                    class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
                    <!-- or whenever the file size reaches 10MB -->
                    <maxFileSize>10MB</maxFileSize>
                </timeBasedFileNamingAndTriggeringPolicy>
            </rollingPolicy>
            <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
                <Pattern>%date [%thread] %-5level %logger{36} - %msg%n</Pattern>
            </encoder>
        </appender>
    </sift>
</appender>

答案 1 :(得分:2)

<File>节点应全部为小写字母。所以,而不是

 <File>sarit_test.log</File>

应该是

<file>sarit_test.log</file>

这是您所犯的错误之一,尝试修复它(可能会解决问题),下次请将错误消息附加到您的问题中。

答案 2 :(得分:1)

可以在命令行设置属性,如:

java -DUSER_HOME="/home/sebastien" MyApp2

您还可以在系统级别设置这些属性。 LogBack将首先查看配置属性,然后查看java系统属性,然后查看系统属性。

使用以下配置将STDOUT写入控制台和文件:

<configuration>

    <!-- LOG_FILE_NAME: Java system properties set on the command line  -->
    <!-- LOG_HOME: Set at this line below -->
    <property name="LOG_HOME" value="/home/sebastien" />

    <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
        <encoder>
            <pattern>%date %-5level [%thread] - [%logger]- %msg%n</pattern>
        </encoder>
    </appender>

    <appender name="FILE" class="ch.qos.logback.core.FileAppender">
        <file>${LOG_HOME}/${LOG_FILE_NAME}</file>
        <encoder>
            <pattern>%date %-5level [%thread] - [%logger] - %msg%n</pattern>
        </encoder>
    </appender>


    <root level="WARN">
        <appender-ref ref="STDOUT" />
        <appender-ref ref="FILE" />
    </root>
</configuration>

答案 3 :(得分:1)

我立即看到的一件事是你只打开了<rollingPolicy>,但政策本身是空的。我打赌这会产生一些问题。

对于问题的第二部分,是的,这是可能的,最简单的方法可能是定义一个“常量”,其值将由应用程序中的类设置。

我已经重新设计了你的logback.xml以包含上面的两个建议。我意识到这已经有一年了,但对于寻找类似问题的其他人来说,它仍然有用。

<configuration>
  <define name="logPath" class="path.to.your.Class.with.public.method.getLogPath">
    <key>getLogPath</key>
  </define>

  <appender name="defaultLog" class="ch.qos.logback.core.rolling.RollingFileAppender">
    <!--See also http://logback.qos.ch/manual/appenders.html#RollingFileAppender-->
    <File>${logPath}${file.separator}sarit_test.log</File>
    <encoder>
      <pattern>%d{dd MMM yyyy HH:mm:ss.SSS} [%t] %-5p %x %F:%L - %m</pattern>
    </encoder>
    <rollingPolicy class="ch.qos.logback.core.rolling.FixedWindowRollingPolicy"/>
      <fileNamePattern>${logPath}${file.separator}sarit_test.log.%i.zip</fileNamePattern>
      <minIndex>1</minIndex>
      <maxIndex>5</maxIndex>
    </rollingPolicy>
    <triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
      <MaxFileSize>50000KB</MaxFileSize>
    </triggeringPolicy>
  </appender>
  <root level="INFO">
    <appender-ref ref="defaultLog"/>
  </root>
</configuration>    

答案 4 :(得分:0)

<configuration>

    <!-- LOG_FILE_NAME: Java system properties set on the command line  -->
    <!-- LOG_HOME: Set at this line below -->
    <property name="LOG_HOME" value="/home/sebastien" />

    <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
        <encoder>
            <pattern>%date %-5level [%thread] - [%logger]- %msg%n</pattern>
        </encoder>
    </appender>

    <appender name="FILE" class="ch.qos.logback.core.RollingFileAppender">
        <file>${LOG_HOME}/${LOG_FILE_NAME}</file>
        <encoder>
            <pattern>%date %-5level [%thread] - [%logger] - %msg%n</pattern>
        </encoder>
    </appender>


    <root level="WARN">
        <appender-ref ref="STDOUT" />
        <appender-ref ref="File" />
    </root>
</configuration>

这可以很好地写入文件和控制台