简体   繁体   中英

How to set logging.path for spring-boot apps?

spring-boot provides several logging.* settings that can be applied in application.properties , like:

logging.level.=DEBUG
logging.file=myfile.log
logging.path=d:/logs/

Problem: myfile.log is generated, BUT inside the classpath! Why isn't spring taking my absolute path into account?

The Spring Boot documentation states

By default, Spring Boot will only log to the console and will not write log files. If you want to write log files in addition to the console output you need to set a logging.file or logging.path property (for example in your application.properties).

and then describes how the logging.file and logging.path properties work. You should only set one.

If logging.file is set, it will write to that specific file. The documentation states

Names can be an exact location or relative to the current directory.

So you're likely writing to your current directory, which happens to be the same as your classpath.

If you set logging.path , Spring Boot

Writes spring.log to the specified directory. Names can be an exact location or relative to the current directory.

Check that your current directory isn't your classpath, if you don't want them to mix, and adapt one of the logging.file and logging.path accordingly.

I don't know if this is still needed, but you can set the absolute path with following code according to your example

logging.path=D:\logs\logfile.txt

You can alter the filename and the path like this. If the folder doesn't exist it gets created. On Windows you have to work with \\ as seperator, while on Linux and Mac you need / as seperator.

REMEMBER: You cannot have logging.file AND logging.path in your properties together. It is either .file OR .path ... in your case the path.

Tested 2 Minutes before posting

for Spring boot v2.3.4 and above for sure: it is NOT logging.file=.... , it is logging.file. name =....

You can also have this configuration on your app.properties . Thats how it is working in one of my projects.

logging.path=../logs

logging.file=${logging.path}/fileName.log

So, you can have both properties and one refers to the other.

For spring-boot version 2.3.x and above:

logging:
  level:
    root: INFO
    my.app: INFO
  file:
    path: './logs/${spring.application.name}'
    name: ${logging.file.path}/my-app.log

logging.file.name :Log file name (for instance, myapp.log ). Names can be an exact location or relative to the current directory.

logging.file.path : Location of the log file. For instance, /var/log .

Reference: Spring-Boot Core Properties

I have set logging.file=C:/usr/local/tomcat/logs/hib.log in the application.properties and the setting like below in class

private static final Logger logger = LogManager.getLogger(ChargeMasterController.class);

logger.info("Total time taken for  is ---------------------------" + time  + " ms");

Logs are getting printed fine in the path mentioned as logging.file.

Now I want to print my logs in 2 different files for 2 different classes(In same package), how can I set 2 logging.file in application.properties

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM