6

I am trying to configure the stout to save into a file. However, it is not saved to a file - do you have an idea why?. also - I want the log file name would be configurable inside the logback.xml something like {LOG_FILE_NAME} which will come from the cmd - is it possible?

This is my 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>
4

5 回答 5

2

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

 <File>sarit_test.log</File>

它应该是

<file>sarit_test.log</file>

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

于 2014-05-14T11:28:10.610 回答
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>
于 2013-08-23T10:12:02.880 回答
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>
于 2013-10-10T08:18:48.400 回答
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>    
于 2015-07-13T23:13:50.173 回答
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>

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

于 2020-01-22T18:10:02.263 回答