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

SpringSpring BootLogbackSlf4j

Spring Problem Overview


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?

Spring Solutions


Solution 1 - Spring

Use logging.file.name instead of logging.file

In higher versions of spring-boot-parent(from version 2.2.0), property logging.file is deprecated.

Solution 2 - Spring

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][1] in the same directory where application.properties resides. According to the [documentation][2] 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.) [1]: https://stackoverflow.com/a/38584210/6313877 [2]: http://docs.spring.io/spring-boot/docs/current/reference/html/howto-logging.html

Solution 3 - Spring

In my case, I used below, in the application property file. > logging.file

Instead, I need to use the below one: > logging.file.name

Since then, I could be able to get the logs into the directed path file.

Solution 4 - Spring

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

Solution 5 - Spring

Spring Boot: Version 2.4.3

Either one of them should be used in application.properties file: logging.file.name or logging.file.path

For example:

logging.file.name=logs/myapp.log
logging.file.path=logs

You don't have to create logs directory. It will be created automatically in class path.

To see other deprecated properties, read this class file ~/.m2/repository/org/springframework/boot/spring-boot/2.4.3/spring-boot-2.4.3.jar!/org/springframework/boot/logging/LoggingSystemProperties.class

Solution 6 - Spring

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

enter image description here

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

Solution 7 - Spring

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:

enter image description here

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

enter image description here

Solution 8 - Spring

Check the version of the Springboot parent.

if it is 2.3.x+ then the property should be logging.file.name=yourapplog.log

Solution 9 - Spring

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.

Solution 10 - Spring

If you want to place log file in specific directory like D drive then below changes can be done in spring boot project

1.Create logback-spring.xml configuration file and mention the package name of which you want to create logs

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <include resource="org/springframework/boot/logging/logback/base.xml" />
    
        <logger name="com.example.demo" 
        level="DEBUG" >
           
        </logger>
</configuration>

2.Also make below addition in application.properties file

logging.config=classpath:logback-spring.xml
logging.file.name=F:/Springbootlogs/filename.log

Note: The mentioned changes are for version 2.4.3

Solution 11 - Spring

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.

Solution 12 - Spring

Sorry for the late reply. It seems spring's logger reads the property from its own classpath.Due to precedence, it's not respecting the properties supplied.

Some tricks to get around:
  1. In main class set the property variable using springApplication.setDefaultProperties(properties); like this

public static void main(String[] args) {
        SpringApplication springApplication = new SpringApplication(MainClass.class);
        Properties properties = new Properties();
        properties.put("logging.file", logDirectory);
        springApplication.setDefaultProperties(properties);
        springApplication.run(args);
    }

  1. Pass the property as JVM parameter -Dlogging.file=/location/output.log.

Both of the above are not the best ones as in order to define other logging properties they also should follow the same way.

Solution

Define a property file and put all you logging configurations in that and specify the file in -Dspring.config.location. This is a derivation of my other problem and this is how I resolved that. check that out in order to know other solutions that I've tried and their challenges.

Solution 13 - Spring

I also faced the same issue in windows operating system . I just changed -> logging.file to logging.file.name=D:/customer_projects/flight_reservation_system/logs/reservation.log

Note: Spring-boot version which i used is 2.4.1

in windows operating system when you copy file path from file explorer you will get file path like D:\customer_projects\flight_reservation_system\logs\flight_reservation.log

Above file path will not work. so you need to change file path like D:/customer_projects/flight_reservation_system/logs/reservation.log then it created a log file in our specified path. thank you.

Solution 14 - Spring

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.

Solution 15 - Spring

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

Solution 16 - Spring

I was also facing same issues, since i just copied the path as windows(uses "" in path) provide.

Fixed just by changing : back slash ("") to forward slash ("/") in path.

Note: Strictly forward slash ("/") should be used in path, OS is not the constraint.

ex:- logging.file.name=D:/Logs/server.log

Solution 17 - Spring

I run my spring boot service using command line argument which works fine. All the spring boot console log writes to the file. I don't have any logging configured in my application.properties file. Spring boot version: 2.0.5.RELEASE

In windows:

java -jar target\microservice-0.0.1.jar --logging.file=C:\\logs\\microservice.log

In Linux

java -jar target\microservice-0.0.1.jar --logging.file=\var\log\microservice.log

Solution 18 - Spring

In my case I pasted some typical config and somehow most probably messed my logging pattern (logging.pattern.file)

Commenting it out solved my own issue (file was created, but nothing was written in it, even though there was console output and root logging level was set to DEBUG) - no errors were given otherwise.

[edit] In other case (I always seem to bump into this problem), I was referencing a JAR file with classes stripped from a web application (WAR), which contained a logback.xml, not to mention AppInitializer - I suspect AppInitializer wouldn't be a problem, since it has a completely different package name and shouldn't be scanned by Spring auto config.. but logback.xml was being detected, I guess, in the classpath, and messed everything completely. I knew it was a hack to reference a WAR, but I was hoping for a quick fix - fixing that, breaking something else. Mani's answer is related.

Solution 19 - Spring

For Springboot 2.4.2, Below Application.yml configurations works :

logging:
    file:
        name: logpath

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionChristoph M&#246;biusView Question on Stackoverflow
Solution 1 - SpringVivek RajaView Answer on Stackoverflow
Solution 2 - SpringChristoph MöbiusView Answer on Stackoverflow
Solution 3 - SpringIndrajeet GourView Answer on Stackoverflow
Solution 4 - SpringbeaudetView Answer on Stackoverflow
Solution 5 - SpringSathishView Answer on Stackoverflow
Solution 6 - SpringRavindran KanniahView Answer on Stackoverflow
Solution 7 - SpringAchillesVanView Answer on Stackoverflow
Solution 8 - SpringBitlaView Answer on Stackoverflow
Solution 9 - SpringLazar LazarovView Answer on Stackoverflow
Solution 10 - SpringYoshita MahajanView Answer on Stackoverflow
Solution 11 - SpringJulyView Answer on Stackoverflow
Solution 12 - SpringManiView Answer on Stackoverflow
Solution 13 - SpringRamView Answer on Stackoverflow
Solution 14 - SpringPunitView Answer on Stackoverflow
Solution 15 - SpringHardik PatelView Answer on Stackoverflow
Solution 16 - SpringBhupendra KumarView Answer on Stackoverflow
Solution 17 - SpringvkramsView Answer on Stackoverflow
Solution 18 - Springhello_earthView Answer on Stackoverflow
Solution 19 - Springankita singhView Answer on Stackoverflow