9

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.

membersound
  • 81,582
  • 193
  • 585
  • 1,120

3 Answers3

5

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
Patrick
  • 12,336
  • 15
  • 73
  • 115
  • Thanks for the detailed answer. Then maybe it's the tomcat itself that has some strange configuration causing a different behavior for me. Will report back when I found it. Meanwhile I'll accecpt your answer, as you proved the solution. – membersound Jan 04 '17 at 14:33
  • I don't know why, but I have to use the following path: `logging.file=logs/mylog.log`, otherwise the "/log" path is created in the upper folder of tomcat home, outside the tomcat folder itself. – membersound Jan 05 '17 at 11:40
  • @membersound strange. You dont have any other logging configuration file like ` logback.xml` or something else in your application? Or maybe set the `logging.path` in properties file? – Patrick Jan 05 '17 at 12:36
  • No I'm just relying on the default spring-boot configuration. tested on tomcat 7 + 8 – membersound Jan 05 '17 at 19:12
3

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

Community
  • 1
  • 1
Tomz
  • 85
  • 5
  • I cannot make use of environment variable, as I run several `war` files within the same tomcat webserver, and only want to change the logging for that specific app. an envrn var would have impact any all – membersound Jan 04 '17 at 12:43
1

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

Mike Thomsen
  • 36,828
  • 10
  • 60
  • 83