membersound
membersound

Reputation: 86747

How to set the logfile name of Spring application and log to tomcat /logs folder?

How can I set the logging path relative to tomcat dir /logs/mylog.log?

What I tried: changing the logging.file property in application.properties

leaving the filename out: #logging.file= -> everything is logged to console, thus written into tomcat/logs/localhost.yyyy-mm-dd.log

logging.file=mylog.log -> written to console, thus same as #logging.file

logging.file=d:/mylog.log -> written to the location d:/mylog.log

logging.file=../logs/mylog.log -> written to console, thus still to localhost*.log

None was successful. I'm not interested in externalising the configuration eg by providing system or environment variables.

Upvotes: 9

Views: 9364

Answers (3)

Patrick
Patrick

Reputation: 12734

I just created a simple Spring-bootapp from spring starter build as war file. I have just this modification in @SpringBootApplication class:

@SpringBootApplication
public class LogApplication {

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

    public static void main(String[] args) {
        SpringApplication.run(LogApplication.class, args);
    }

    @Controller
    @ResponseBody
    public static class IndexController{

        @RequestMapping("/")
        public String getindex(){
            logger.error("Error Logging");
            return "Hello";
        }
    }
}

And this property in application.properties:

logging.file=../logs/mylog.log

Build the application using maven mvn clean install and put the war file inside webapps folder of tomcat. Started tomcat using startup.bat and hit successful the endpoint http://localhost:8080/demo-0.0.1-SNAPSHOT.

And the log was written in logs/mylog.log:

enter image description here

2017-01-04 14:57:10.755 ERROR 8236 --- [http-apr-8080-exec-4] com.example.LogApplication               : Error Logging

Upvotes: 5

Mike Thomsen
Mike Thomsen

Reputation: 37506

I'm going to second Tomz's response and point you to the docs because they show you how to switch over from logback to log4j which is probably easier for you.

I would strongly recommend not deploying Spring Boot in war files, but as executable fat jars. It makes things a lot easier when you can just type this to test a configuration and deploy it:

java -jar my-service.jar /opt/my-service/conf/application.yml

Upvotes: 1

Tomz
Tomz

Reputation: 83

You can make use of the environment variable for configuring the log path.

Tomcat sets a catalina.home system property which you can use

log4j.rootCategory=DEBUG,errorfile

log4j.appender.errorfile.File=${catalina.home}/logs/LogFilename.log

Note:-

This may not work On Debian (including Ubuntu), ${catalina.home} will not work because that points at /usr/share/tomcat6 which has no link to /var/log/tomcat6. Here just use ${catalina.base}. Check this link

Upvotes: 3

Related Questions