Spring Boot - no log file written (logging.file is

2019-02-03 00:29发布

问题:

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

While the console output works fine, filename.log is not created. Also, if I create the file manually, nothing is written to it. What do I miss?

回答1:

I found a solution. I am not very happy with it since it still does not answer my original question why the logging.file property is not respected.

I created the logback-spring.xml from Georges' answer in the same directory where application.properties resides. According to the documentation Spring Boot will pick it up from there. Apparently, this does not happen in my case.

I need to additionally add logging.config=classpath:logback-spring.xml in order it is picked up by Spring. The relevant parts of my application.properties are now

logging.config=classpath:logback-spring.xml
logging.file=logs/logfile.log

(I created the logs directory manually.)



回答2:

I don't know whether this would help you but I am also using Logback in my Spring-Boot project and the structure is as below

File: logback.xml

<?xml version="1.0" encoding="UTF-8"?>
<configuration xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:noNamespaceSchemaLocation="logback.xsd">

    <property resource="\application.properties"/>

    <appender name="FILE"
        class="ch.qos.logback.core.rolling.RollingFileAppender">
        <file>${app.logPathPrefix}/myproject.log</file>
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <fileNamePattern>${app.logPathPrefix}/myproject.%d{yyyy-MM-dd}.%i.log.gz</fileNamePattern>
            <timeBasedFileNamingAndTriggeringPolicy
                class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
                <maxFileSize>50MB</maxFileSize>
            </timeBasedFileNamingAndTriggeringPolicy>
            <maxHistory>30</maxHistory>
        </rollingPolicy>

        <encoder>
            <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%level] [%thread] [%logger:%line] %msg%n
            </pattern>
        </encoder>
    </appender>

    <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
        <encoder>
            <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%level] [%thread] [%logger:%line] %msg%n
            </pattern>
        </encoder>
    </appender>


    <logger name="org.springframework" level="INFO" />
    <logger name="com.mycompany" level="INFO" />
    <logger name="org.hibernate" level="DEBUG" />


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

</configuration>

File: application.properties

app.logPathPrefix=/var/log/myproject


回答3:

Here is how i managed to write output to a local file file. To disable console logging and write output only to a file you need a custom logback-spring.xml ( call it logback-spring.xml so you ll take advantage of the templating features (date formatting etc..) provided by Boot) that imports file-appender.xml instead of console-appender.xml. In order to achieve this, you must paste this code below into your logback-spring.xml file.

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <include resource="org/springframework/boot/logging/logback/defaults.xml" />
    <property name="LOG_FILE" value="${LOG_FILE:-${LOG_PATH:-${LOG_TEMP:-${java.io.tmpdir:-/tmp}}/}spring.log}"/>
    <include resource="org/springframework/boot/logging/logback/file-appender.xml" />
    <root level="INFO">
        <appender-ref ref="FILE" />
    </root>
</configuration> 

You also need to add the following to your application.properties:

logging.file=myapplication.log

Notice that this log file myapplication.log will be generated by springboot.

This is how my application structure tree looks like:

If you want to have more fun, you can locate the base.xml in your maven dependencies like this:



回答4:

If you are using Maven add the dependency :

<dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>slf4j-api</artifactId>
    <version>1.7.6</version>
</dependency>
<dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>slf4j-log4j12</artifactId>
    <version>1.7.5</version>
</dependency>

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.



回答5:

I had the same problem. It's more than likely due to file permissions on the file system. I had the application folder owned by root, but ./logs owned by the process owner. As such, the following didn't work:

logging.file=my.log

but this did

logging.file=/opt/myapp/logs/my.log



回答6:

I just used the Spring-boot provided logging mechanism. I wrote below in my 'logback.xml' file :

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<include resource="org/springframework/boot/logging/logback/defaults.xml" />
<include resource="org/springframework/boot/logging/logback/file- appender.xml" />
<root level="INFO">
<appender-ref ref="FILE" />
</root>
</configuration>

I put the both application.properties and logback.xml file under same package 'src/main/resources'. In application.properties file just added one parameter :

logging.file = xyz.log

It worked perfectly fine for me.



回答7:

In my case, I added a file "logback.xml" in one of my sub module by mistake which caused this issue, remove it and everything will be fine.



回答8:

If you are on Spring Boot then you can directly add following properties in application.properties file to set logging level, customize logging pattern and to store logs in the external file.

These are different logging levels and its order from minimum << maximum.

OFF << FATAL << ERROR << WARN << INFO << DEBUG << TRACE << ALL

# To set logs level as per your need.
logging.level.org.springframework = debug
logging.level.tech.hardik = trace

# To store logs to external file
# Here use strictly forward "/" slash for both Windows, Linux or any other os, otherwise, your logs it won't work.      
logging.file=D:/spring_app_log_file.log

# To customize logging pattern.
logging.pattern.file= "%d{yyyy-MM-dd HH:mm:ss} - %msg%n"

Please pass through this link to customize your logs more vividly.

https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-logging.html