Spring Boot - no log file written (logging.file is not respected)

前端 未结 15 1459
一向
一向 2020-12-14 00:01

I use Spring Boot and want it to write log output to a file.

According to the docs, this is simply done by setting

logging.file=filename.log
<         


        
15条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-14 00:55

    If you are using Maven add the dependency :

    
        org.slf4j
        slf4j-api
        1.7.6
    
    
        org.slf4j
        slf4j-log4j12
        1.7.5
    
    

    Now you have to specify a file that is called 'log4j.properties' which you have to put in the specific directory : ' src/main/resources/log4j.properties '

    Here is how the file should look for example :

    # Root logger option
    log4j.rootLogger=INFO, file, stdout
    log4j.logger.org.springframework.ws.client.MessageTracing.sent=TRACE
    log4j.logger.org.springframework.ws.client.MessageTracing.received=TRACE
    
    # Direct log messages to stdout
    log4j.appender.stdout=org.apache.log4j.ConsoleAppender
    log4j.appender.stdout.Target=System.out
    log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
    
    # log4j.appender.springlog.Threshold=INFO
    log4j.appender.springlog.layout=org.apache.log4j.PatternLayout
    log4j.appender.springlog.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n
    
    # Direct log messages to a log file
    log4j.appender.file=org.apache.log4j.RollingFileAppender
    log4j.appender.file.File=D:/example/filename.log
    log4j.appender.file.MaxFileSize=10MB
    log4j.appender.file.MaxBackupIndex=10
    log4j.appender.file.layout=org.apache.log4j.PatternLayout
    log4j.appender.file.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n
    

    Now import these :

    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    

    Declare a logger variable like this :

    final static Logger logger = Logger.getLogger(TheClassYourIn.class);
    

    And use it in the class like this :

    logger.info("Well hello world then ");
    

    This way it works for me. I hope that this answer will help you . Good luck !

    PS: log4j.appender.file.File='directory' is how you specify where the logs to be stored. If you don't specify a directory and just leave it as filename.log this file will be automaticly created in the project dir.

提交回复
热议问题