Home > Net >  java.util.logging.FileHandler is not working after app is deployed to tomcat
java.util.logging.FileHandler is not working after app is deployed to tomcat

Time:07-01

I have a java web application that I deployed to tomcat. I am using java.util.logging for logging. Here is the logging.properties file.

handlers=java.util.logging.FileHandler,org.apache.juli.FileHandler,java.util.logging.ConsoleHandler

java.util.logging.ConsoleHandler.level = INFO
java.util.logging.ConsoleHandler.formatter = java.util.logging.SimpleFormatter

java.util.logging.FileHandler.level = INFO
java.util.logging.FileHandler.pattern =%h/DemoWebAppLogs/DemoWebApp%u%g.log
java.util.logging.FileHandler.append=true
java.util.logging.FileHandler.limit = 5000
java.util.logging.FileHandler.count = 5000
java.util.logging.FileHandler.formatter = java.util.logging.SimpleFormatter


And this is GitHub link of my code.
When I debug/run the app on eclipse both console and file logging are working fine.
In C:/Users/User DemoWebAppLogs folder is created and DemoWebApp00.log and
DemoWebApp00.log.lck file is created.
But when I deploy the app on tomcat, file logging is not working (console logging is working). Only DemoWebAppLogs folder is created but there are no .log or .lck files.
I exported DempWebApp.war file using eclipse and put that file in CATALINA_HOME/webapps. I'm using tomcat 9.0.55.

CodePudding user response:

In your code you don't need to call LogManager.readConfiguration under Tomcat. Per the docs:

JULI is enabled by default, and supports per classloader configuration, in addition to the regular global java.util.logging configuration. This means that logging can be configured at the following layers:

  • Globally. That is usually done in the ${catalina.base}/conf/logging.properties file. The file is specified by the java.util.logging.config.file System property which is set by the startup scripts. If it is not readable or is not configured, the default is to use the ${java.home}/lib/logging.properties file in the JRE.
  • In the web application. The file will be WEB-INF/classes/logging.properties

Simply place the logging.properties in WEB-INF/classes.

But when I deploy the app on tomcat, file logging is not working (console logging is working). Only DemoWebAppLogs folder is created but there are no .log or .lck files.

The FileHandler was never opened because it wasn't called, the path is pointing somewhere else, or the open failed.

The root logger can delay loading the handlers until it sees a log message. Try setting the levels to ALL. Check all of the log files for Tomcat including any console output from the Tomcat process. If logging failed to load there should be an error message in those logs.

  • Related