Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 914
membersound Avatar asked Jan 04 '17 12:01

membersound


People also ask

What is the name of logfile in Tomcat?

The main Apache Tomcat configuration file is at /opt/bitnami/tomcat/conf/server. xml. Once Apache Tomcat starts, it will create several log files in the /opt/bitnami/tomcat/logs directory. The main log file is the catalina.

How do I specify a logging file in spring boot?

To make Spring Boot write its log to disk, set the path and filename. With this configuration, Spring Boot will write to the console and also to a log file called spring. log , at the path you specify.

Which is the default logging file in spring boot spring log?

The default logging configuration in Spring Boot is a Logback implementation at the info level for logging the output to console. The first info log is printed, followed by a seven-line banner of Spring and then the next info log. The debug statement is suppressed.

Which is the default logging file name in spring boot?

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.


2 Answers

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
like image 165
Patrick Avatar answered Oct 11 '22 05:10

Patrick


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

like image 25
Tomz Avatar answered Oct 11 '22 04:10

Tomz