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
<
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
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.)
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
In my case, I was using logging.file
in the application property file.
Instead, I need to used logging.file.name
, since then I can be able to get the logs into the directed path file.
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.
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.
springApplication.setDefaultProperties(properties);
like thispublic 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);
}
-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.
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.