简体   繁体   English

如何配置log4j和logback以正确登录到一个文件?

[英]How to configure log4j and logback to log into one single file properly?

I have a java web app that uses logback with slf4j for logging. 我有一个Java Web应用程序,它使用slf4j和logback进行日志记录。 And this project has a dependency jar (which is a sub project). 这个项目有一个依赖罐(这是一个子项目)。 And this dependency jar uses org.apache.log4j.Logger for logging. 并且此依赖项jar使用org.apache.log4j.Logger进行日志记录。 All logs must go into one log file. 所有日志必须放入一个日志文件。 My problem is, whatever I am logging with the code in the jar file is not being written to the log file. 我的问题是,无论用jar文件中的代码记录的任何内容均未写入日志文件。 Initially I had logback.xml. 最初我有logback.xml。 To resolve the above problem I added log4j.properties file to my web app. 为了解决上述问题,我将log4j.properties文件添加到了Web应用程序。 It resolved my problem and now I can see all logs in one file. 它解决了我的问题,现在我可以在一个文件中看到所有日志。

Again my new probelm is: Earlier the log file was rolling every day according to the rolling policy in logback.xml. 同样,我的新探针是:早先日志文件是根据logback.xml中的滚动策略每天滚动的。 Now it is not rolling. 现在它没有滚动。 Even after I configured the rolling policy in log4j.properties that matches with the rolling policy that is in logback.xml, rolling is not happening. 即使我在log4j.properties中配置了与logback.xml中的滚动策略匹配的滚动策略,也不会发生滚动。

I don't want to replace log4j in my dependency with logback. 我不想用logback替换依赖项中的log4j。 And also I want to write alll my logs into one file. 我也想将所有日志写入一个文件。 Is it possible to this, how can I resolve my issue? 有可能吗,如何解决我的问题?

FYI: I have "log4j-over-slf4j" dependency included in my pom.xml 仅供参考:我的pom.xml中包含“ log4j-over-slf4j”依赖项

Log4j.properties: Log4j.properties:

log4j.appender.file=org.apache.log4j.DailyRollingFileAppender
log4j.appender.file.File=SOME_PATH/logs/studentApp.log
log4j.appender.file.rollingPolicy = org.apache.log4j.rolling.TimeBasedRollingPolicy
log4j.appender.file.rollingPolicy.FileNamePattern = SOME_PATH/logs/studentApp.%d{yyyy-MM-dd}.log    
log4j.appender.file.maxBackupIndex=14
log4j.appender.file.threshold=DEBUG
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=[%-5p] %d %c - %m%n
log4j.rootLogger=DEBUG,file

Assuming your rolling policy is correct, it's possible that log4j picks up the wrong configuration. 假设您的滚动策略正确,则log4j可能选择了错误的配置。 log4j looks for the first configuration file in the classpath (first .xml then .properties ) and if it happens to be the configuration from one of your dependencies your rolling policy will not work. log4j在类路径中查找第一个配置文件(第一个.xml然后是.properties ),如果碰巧是您的某个依赖项中的配置,则滚动策略将不起作用。

To test this you can add a system property -Dlog4j.configuration=... with the path to your configuration to the tomcat startup script. 要对此进行测试,您可以添加系统属性-Dlog4j.configuration=... ,其中包含tomcat启动脚本的配置路径。 Or simply set your log level to TRACE/DEBUG and see if it affects the output. 或者只是将日志级别设置为TRACE / DEBUG,然后查看它是否影响输出。

Edit: your log4j config looks good, but the date pattern is missing. 编辑:您的log4j配置看起来不错,但是缺少日期模式。 Try adding log4j.appender.file.DatePattern='.'yyyy-MM-dd-HH-mm (note that I've set the pattern to roll-over every minute, for easier testing) 尝试添加log4j.appender.file.DatePattern='.'yyyy-MM-dd-HH-mm (请注意,我已将模式设置为每分钟滚动一次,以便于测试)

Edit : you can remove every line containing rollingPolicy and maxBackupIndex - those will not be picked up in this case 编辑 :您可以删除包含rollingPolicymaxBackupIndex每一行-在这种情况下,这些行将不会被拾取

The following configuration will create a .log file for the current date and keep adding the log in that file. 以下配置将为当前日期创建一个.log文件,并继续在该文件中添加日志。 When the date chanages, it will create a new file with the current date. 当日期改变时,它将使用当前日期创建一个新文件。 This configuration will also zip the files as or when they exceed a certain size limit. 当文件超过一定大小限制时,此配置还将压缩文件。

    <appender name="FILE"
        class="ch.qos.logback.core.rolling.RollingFileAppender">
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">

            <fileNamePattern>log.%d{yyyy-MM-dd}.%i.log.zip</fileNamePattern>

                          <timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
                                <!-- or whenever the file size reaches 500MB -->
                                <maxFileSize>300MB</maxFileSize>
                        </timeBasedFileNamingAndTriggeringPolicy>
        </rollingPolicy>
        <encoder>
            <pattern>%date %level [%thread] %logger{0} - %msg%n</pattern>
        </encoder>
    </appender>

Hope it helps. 希望能帮助到你。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM