【问题标题】:Spring logging to file (log4j)春季记录到文件(log4j)
【发布时间】:2015-11-04 07:29:14
【问题描述】:

我有一个 Maven 项目,我想在其中启用 Spring 日志记录到日志文件。我使用的日志系统是 log4j。我已将它放在我的 log4j.properties 文件中,但这还不够:

log4j.category.org.springframework=ALL

我想将日志放到现有的附加程序中,或者放到单独的文件中。

【问题讨论】:

    标签: spring logging log4j


    【解决方案1】:

    对于单独的文件,使用 appender 文件定义根记录器,如下所示:

    # Define the root logger with appender file
    log4j.rootLogger = info, FILE  
    
    # Define the file appender
    log4j.appender.FILE=org.apache.log4j.DailyRollingFileAppender
    
    # Set the name of the file
    log4j.appender.FILE.File=/your/path/to/logfile/logfilename.log
    
    # Set the immediate flush to true (default)
    log4j.appender.FILE.ImmediateFlush=true
    
    # Set the threshold to debug mode
    log4j.appender.FILE.Threshold=info
    
    # Set the append to false, should not overwrite
    log4j.appender.FILE.Append=true
    
    # Set the DatePattern
    log4j.appender.FILE.DatePattern='.' yyyy-MM-dd
    
    # Define the layout for file appender
    log4j.appender.FILE.layout=org.apache.log4j.PatternLayout
    log4j.appender.FILE.layout.conversionPattern=[%d{dd/MM/yyyy HH:mm:ss} %-5p %c{1}:%L] Method:- %M : %m%n
    

    编辑

    根据doc在属性文件中设置日志级别

     log4j.level.org.springframework.web: info
    

    【讨论】:

    • 但是它将如何发现它应该记录 Spring?我已经在 log4j 属性文件中有几个附加程序,但是当我为 spring 添加行时,它没有出现在日志中。
    猜你喜欢
    • 2016-09-03
    • 1970-01-01
    • 2017-09-25
    • 2018-07-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多