Raihan
Raihan

Reputation: 10405

Using custom logging.properties with jersey

I am writing a RESTful web app using Jersey with Tomcat as the container. I am using Java logging API (which is also used by both Jersey and Tomcat) to generate logs from my app. But the console logger that shows logs generated by Jersey and Tomcat only showing logs from my app up to INFO level. I have placed a custom logging.properties file with a ConsoleHandler and .level=ALL in WebContent/META-INF/classes folder. But it's still not showing any CONFIG or FINE level logging from my app.

How do I find out which logging.properties file is in effect? The one in jre/lib or tomcat/conf or somewhere else? More importantly, how do I make my app to use the custom logging.properties?

Upvotes: 2

Views: 1541

Answers (2)

ocarlsen
ocarlsen

Reputation: 1440

They way Java Util Logging works, it does not automatically find the logging.properties on your classpath. You need to set the java.util.logging.config.file system property.

For example, say this is your custom logging.properties:

# Handler details
handlers=java.util.logging.ConsoleHandler
java.util.logging.ConsoleHandler.level=ALL
java.util.logging.SimpleFormatter.format=%4$-7s [%3$s] %5$s%6$s%n

# Log level details
.level=ALL

Place the file in the root of your classpath, e.g. src/main/resources if you are using Maven. Depending on how you deploy your application, set the system property java.util.logging.config.file to logging.properties. For example, if you are deploying with tomcat7-maven-plugin:

mvn tomcat7:deploy -Djava.util.logging.config.file=logging.properties

You will see everything:

Jan 03, 2019 12:09:05 AM org.apache.catalina.startup.HostConfig deployWAR
INFO: Deploying web application archive /usr/local/tomcat/webapps/Example.war
Jan 03, 2019 12:09:05 AM org.apache.tomcat.util.scan.StandardJarScanner scan
FINER: Scanning WEB-INF/lib for JARs
Jan 03, 2019 12:09:05 AM org.apache.tomcat.util.scan.StandardJarScanner scan
FINE: Scanning JAR [/WEB-INF/lib/jersey-common-2.27.jar] from WEB-INF/lib
Jan 03, 2019 12:09:05 AM org.apache.tomcat.util.scan.StandardJarScanner process
FINER: Scanning JAR at URL [file:/usr/local/tomcat/webapps/Example/WEB-INF/lib/jersey-common-2.27.jar]
Jan 03, 2019 12:09:05 AM org.apache.tomcat.util.scan.StandardJarScanner scan
...

Upvotes: 0

gaurav5430
gaurav5430

Reputation: 13902

This is how I made it work on Tomcat running from within Eclipse:

  1. Find out the path to tomcat running within Eclipse: Where can I view Tomcat log files in Eclipse?

  2. Create a logging.properties file there. In my case, the path was .../EclipseWorkSpace/.metadata/.plugins/org.eclipse.wst.server.core/tmp0/conf (You can copy this logging file from within your actual tomcat conf folder.)

  3. Within your application, create a logging.properties file in src/main/resources. (did not work when i directly created in WEB-INF/classes as mentioned in tomcat documentation :

Example logging.properties for the servlet-examples web application to be placed in WEB-INF/classes inside the web application: from https://tomcat.apache.org/tomcat-9.0-doc/logging.html )

  1. set your tomcat logging parameters in the above file, which overrides the default logging.properties file from /.metadata/.plugins/org.eclipse.wst.server.core/tmp0/conf from inside your app:

    handlers= java.util.logging.ConsoleHandler
    
    .level= FINE
    
    java.util.logging.ConsoleHandler.level = FINE
    java.util.logging.ConsoleHandler.formatter = java.util.logging.SimpleFormatter
    

This will log 'FINE' level logs from Tomcat.

  1. Set logging properties in your jersey app:

    register(new LoggingFeature(Logger.getLogger(MyApplication.class.getName()), Level.INFO, LoggingFeature.Verbosity.PAYLOAD_TEXT, 2048));
    

    This will only log 'INFO' logs from jersey.

So, 4 and 5 effectively means that Tomcat would log all FINE logs, but Jersey would only log INFO logs, so in your log file (catalina.out) , you will only see INFO logs from Jersey, but FINE logs from everything else in Tomcat.

Upvotes: 1

Related Questions