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
<
Check the version of the Springboot parent.
if it is 2.3.x+ then the property should be logging.fil.name=yourapplog.log
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
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:
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
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.
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.