When you create Tomcat embedded JAR file and install it on a server, how can access to the Exceptions? Recently my server and process stops suddenly and I do not know what is going on. I checked Windows Event viewer but could not find anything. Is there any place that exception writes for example on a file?
-
Look into the catalina.out file in your TOMCAT_HOME/logs directory – Jens Aug 07 '17 at 11:43
-
@Jens where is TOMCAT_HOME? – Mahdi Aug 07 '17 at 11:45
-
It depends on where you have installed it – Jens Aug 07 '17 at 11:46
-
I embedded TomCat with my jar file. So really I do not know where is it. – Mahdi Aug 07 '17 at 11:47
-
search for the file Name on your computer – Jens Aug 07 '17 at 11:48
-
@Jens ok I found about 6 folder all in user\AppData\Local\Temp\2 but all have empty folders like work\Tomcat\localhost\ROOT and there is no log file or folder. – Mahdi Aug 07 '17 at 11:53
-
Spring Boot has Tomcat running inside it. You should use some kind of logging (e.g. log4j). Where it's written to depends on your deployment. Is there a console or command shell running? If that hasn't disappeared, look for messages there. – duffymo Aug 07 '17 at 11:55
-
Check this answer, I think it responds to your question: https://stackoverflow.com/a/31939886/869462 – Witz Aug 07 '17 at 11:57
2 Answers
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).
java -Dlogging.file=target/my_logfile.log -jar spring_boot_app.jar
or
java -Dlogging.path=target/logs -jar spring_boot_app.jar
(Will create spring.log file inside target/logs)
To make life easier you can use logback.xml or logback-spring.xml
Following is a sample of logback.xml. Include it inside src/main/resources directory.
<?xml version="1.0" encoding="UTF-8"?>
<configuration scan="true">
<include resource="org/springframework/boot/logging/logback/base.xml"/>
<!-- The FILE and ASYNC appenders are here as examples for a production configuration -->
<appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>logFile.%d{yyyy-MM-dd}.log</fileNamePattern>
<maxHistory>90</maxHistory>
</rollingPolicy>
<encoder>
<charset>utf-8</charset>
<Pattern>%d %-5level [%thread] %logger{0}: %msg%n</Pattern>
</encoder>
</appender>
<appender name="ASYNC" class="ch.qos.logback.classic.AsyncAppender">
<queueSize>512</queueSize>
<appender-ref ref="FILE"/>
</appender>
<!-- your project package -->
<logger name="com.test" level="INFO"/>
<!-- <logger name="javax.activation" level="WARN"/>
<logger name="javax.mail" level="WARN"/>
<logger name="javax.xml.bind" level="WARN"/>
<logger name="ch.qos.logback" level="WARN"/>
<logger name="com.codahale.metrics" level="WARN"/>
<logger name="com.ryantenney" level="WARN"/>
<logger name="com.sun" level="WARN"/>
<logger name="com.zaxxer" level="WARN"/>
<logger name="io.undertow" level="WARN"/>
<logger name="io.undertow.websockets.jsr" level="ERROR"/>
<logger name="org.apache" level="WARN"/>
<logger name="org.apache.catalina.startup.DigesterFactory" level="OFF"/>
<logger name="org.bson" level="WARN"/>
<logger name="org.hibernate.validator" level="WARN"/>
<logger name="org.hibernate" level="WARN"/>
<logger name="org.hibernate.ejb.HibernatePersistence" level="OFF"/>
<logger name="org.springframework" level="WARN"/>
<logger name="org.springframework.web" level="WARN"/>
<logger name="org.springframework.security" level="WARN"/>
<logger name="org.springframework.cache" level="WARN"/>
<logger name="org.thymeleaf" level="WARN"/>
<logger name="org.xnio" level="WARN"/>
<logger name="springfox" level="WARN"/>
<logger name="sun.rmi" level="WARN"/>
<logger name="liquibase" level="WARN"/>
<logger name="LiquibaseSchemaResolver" level="INFO"/>
<logger name="sun.rmi.transport" level="WARN"/> -->
<contextListener class="ch.qos.logback.classic.jul.LevelChangePropagator">
<resetJUL>true</resetJUL>
</contextListener>
<root level="INFO">
<appender-ref ref="CONSOLE"/>
<appender-ref ref="FILE"/>
</root>
</configuration>
Above configuration will generate the logs to the the working directory of the JAR file with the names like logFile.yyyy.MM.dd.log

- 10,333
- 4
- 33
- 71
-
I get the first part and it is works. for better life i want to use second part of your answer. If it is ok just this code with logback.xml named file in resources? – Mahdi Aug 07 '17 at 12:21
-
Yes, I tested it wit the latest spring boot `1.5.6`. It should work. Please replace `com.test` with your project's base package. – 11thdimension Aug 07 '17 at 12:23
-
what is the benefit of using logback.xml vs @mike adamenko's answer. I mean simply put 3 lines in properties? – Mahdi Aug 07 '17 at 12:30
-
In the current configuration log files will be produced in the same directory as JAR file. `fileNamePattern` tag can be edited to include a path like `logs/logFile...`. Benefit of `logback.xml` is that it will centralize the logging configuration. you can set can specify the log4j like rolling appender and other configuration here. – 11thdimension Aug 07 '17 at 12:33
-
-
You're welcome, I just removed the `SizeBasedTriggeringPolicy` from the `logback.xml`. It seemed unnecessary. – 11thdimension Aug 07 '17 at 12:44
-
how can handle which logs be saved? for Example I want to just log Null pointer exceptions. – Mahdi Aug 13 '17 at 07:46
-
and it is another problem: I used to run command from root of my .jar file. when I call it from scheduler like c:/myfolder/my.jar it not compile with Exception of backLog that can not create log files. how I can handle this? – Mahdi Aug 13 '17 at 10:06
-
May be the directory mentioned in the logback doesn't exist, you can mention an absolute URL for logs. For filter NPE take a look at https://logback.qos.ch/manual/filters.html – 11thdimension Aug 13 '17 at 15:25
You should add the application.properties file in your classpath and set logging properties there.
If the only change you need to make to logging is to set the levels of various loggers then you can do that in application.properties using the "logging.level" prefix, e.g.
logging.level.org.springframework.web=DEBUG
logging.level.org.hibernate=ERROR
logging.file=C:\myapplication.log
You can also set the location of a file to log to (in addition to the console) using "logging.file".
See here

- 2,944
- 1
- 15
- 28
-
-
jar is a simple zip archive. By the way, you can add it to your classpath. Updated the answer. – Mike Adamenko Aug 07 '17 at 12:23
-
It would like be logging.file=mylogfile.log and it will be save on JAR root. thank you your answer was useful. – Mahdi Aug 07 '17 at 12:38