Christoph Möbius
Christoph Möbius

Reputation: 1402

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

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?

Upvotes: 81

Views: 139718

Answers (26)

Akash Verma
Akash Verma

Reputation: 45

I don't know if you made the same mistake as I did (Using the Backward Slash '') while defining the path. If so change it to Forward Slash '/'

Upvotes: 0

Cemalettin Durgun
Cemalettin Durgun

Reputation: 1

On my macOS system, this setting below worked for me. myapp.log file was created in my project root directory.

logging.file.name = ./<logging-in-a-spring-boot-project-end>/myapp.log

<logging-in-a-spring-boot-project-end> given above represents the My Project path name.

Upvotes: 0

Pima UA
Pima UA

Reputation: 1

For application.yml below mentioned configurations worked for me:

    logging:
      file.name: logs/dev_app.log

Upvotes: 0

likejudo
likejudo

Reputation: 3716

I don't know why, but

logging.file.path=../tmp

does not work. It is ignored.

only this works

logging.file.name=../tmp/regular-encourager-quotes.log

Spring Boot starter is 2.7.5

Java 17

Upvotes: 0

sachin gupta
sachin gupta

Reputation: 11

logging.file.path=/home/log/
logging.file.name=/home/log/agricom.log

mention the path with filename in "logging.file.name". then it should work fine.

Upvotes: 1

Fazal Haroon
Fazal Haroon

Reputation: 1163

Instead of the following in application.properties

logging.file = logFileName.log

use the below one

logging.file.name= appLogName.log

Check the Screenshot for your reference. enter image description here

Upvotes: 1

oubaydos
oubaydos

Reputation: 192

my problem was that the file is not getting created automatically, and even when i create it in the project root folder, it does not being written into, so eventually, i tried creating a folder and a file inside it logs/logfile.lo and setting logging.file.name=scripts/logfile.log and the application.proprieties file and it worked

Upvotes: 0

ankita singh
ankita singh

Reputation: 11

For Springboot 2.4.2, Below Application.yml configurations works :

logging:
    file:
        name: logpath

Upvotes: 0

beaudet
beaudet

Reputation: 948

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

Upvotes: 11

Indrajeet Gour
Indrajeet Gour

Reputation: 4490

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.

Upvotes: 25

Sathish
Sathish

Reputation: 12703

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

Upvotes: 8

Yoshita Mahajan
Yoshita Mahajan

Reputation: 453

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

Upvotes: 3

Bitla
Bitla

Reputation: 81

Check the version of the Springboot parent.

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

Upvotes: 5

Ram
Ram

Reputation: 57

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.

Upvotes: 1

Vivek Raja
Vivek Raja

Reputation: 1089

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.

Upvotes: 97

hello_earth
hello_earth

Reputation: 1562

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.

Upvotes: 0

Vikram
Vikram

Reputation: 7517

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

Upvotes: 0

Bhupendra Kumar
Bhupendra Kumar

Reputation: 179

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

Upvotes: 0

Mani
Mani

Reputation: 5648

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.

Upvotes: 1

Hardik Patel
Hardik Patel

Reputation: 1138

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

Upvotes: 0

July
July

Reputation: 913

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.

Upvotes: 1

Punit
Punit

Reputation: 344

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.

Upvotes: 1

Christoph M&#246;bius
Christoph M&#246;bius

Reputation: 1402

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.)

Upvotes: 33

AchillesVan
AchillesVan

Reputation: 4356

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

Upvotes: 6

Ravindran Kanniah
Ravindran Kanniah

Reputation: 550

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

Upvotes: 5

Lazar Lazarov
Lazar Lazarov

Reputation: 2532

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.

Upvotes: 3

Related Questions