How to login to tomcat using slf4j and java.util.logging

I created a webapp that runs on Tomcat 8. As always, I want to use slf4j, in which case, backed up by java.util.logging (because this is the default value for Tomcat). The relevant dependencies are as follows:

<!-- logging --> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> <version>1.7.10</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-jdk14</artifactId> <version>1.7.10</version> <scope>runtime</scope> </dependency> 

I have added several log statements to the code (information and debugging), but I cannot find them. Neither in console output (if I run Tomcat from eclipse), nor in any of the log files created in the / logs folder (my application works fine).

So what's missing here? Should I add something to conf / logging.properties (I don't want to package any log configuration in my application)? Is there an example of how to configure logging for a given Webapp deployed to tomcat?

+5
source share
1 answer

The only problem was the configuration on the Tomcat side. Since Tomcat does not use simple java.util.logging, the configuration is slightly different. Therefore, adding some lines to conf / logging.properties helped:

 ... handlers = 1catalina.org.apache.juli.AsyncFileHandler, 2localhost.org.apache.juli.AsyncFileHandler, 3manager.org.apache.juli.AsyncFileHandler, 4host-manager.org.apache.juli.AsyncFileHandler, 5reportExport.org.apache.juli.AsyncFileHandler, java.util.logging.ConsoleHandler .handlers = 1catalina.org.apache.juli.AsyncFileHandler, java.util.logging.ConsoleHandler ############################################################ # Handler specific properties. # Describes specific configuration info for Handlers. ############################################################ 1catalina.org.apache.juli.AsyncFileHandler.level = FINE 1catalina.org.apache.juli.AsyncFileHandler.directory = ${catalina.base}/logs 1catalina.org.apache.juli.AsyncFileHandler.prefix = catalina. 2localhost.org.apache.juli.AsyncFileHandler.level = FINE 2localhost.org.apache.juli.AsyncFileHandler.directory = ${catalina.base}/logs 2localhost.org.apache.juli.AsyncFileHandler.prefix = localhost. 3manager.org.apache.juli.AsyncFileHandler.level = FINE 3manager.org.apache.juli.AsyncFileHandler.directory = ${catalina.base}/logs 3manager.org.apache.juli.AsyncFileHandler.prefix = manager. 4host-manager.org.apache.juli.AsyncFileHandler.level = FINE 4host-manager.org.apache.juli.AsyncFileHandler.directory = ${catalina.base}/logs 4host-manager.org.apache.juli.AsyncFileHandler.prefix = host-manager. 5reportExport.org.apache.juli.AsyncFileHandler.level = FINE 5reportExport.org.apache.juli.AsyncFileHandler.directory = ${catalina.base}/logs 5reportExport.org.apache.juli.AsyncFileHandler.prefix = reportExport. java.util.logging.ConsoleHandler.level = FINE java.util.logging.ConsoleHandler.formatter = org.apache.juli.OneLineFormatter ############################################################ # Facility specific properties. # Provides extra control for each logger. ############################################################ org.apache.catalina.core.ContainerBase.[Catalina].[localhost].level = INFO org.apache.catalina.core.ContainerBase.[Catalina].[localhost].handlers = 2localhost.org.apache.juli.AsyncFileHandler org.apache.catalina.core.ContainerBase.[Catalina].[localhost].[/manager].level = INFO org.apache.catalina.core.ContainerBase.[Catalina].[localhost].[/manager].handlers = 3manager.org.apache.juli.AsyncFileHandler org.apache.catalina.core.ContainerBase.[Catalina].[localhost].[/host-manager].level = INFO org.apache.catalina.core.ContainerBase.[Catalina].[localhost].[/host-manager].handlers = 4host-manager.org.apache.juli.AsyncFileHandler com.prodyna.reportExport.level = FINE com.prodyna.reportExport.handlers = 5reportExport.org.apache.juli.AsyncFileHandler # For example, set the org.apache.catalina.util.LifecycleBase logger to log # each component that extends LifecycleBase changing state: #org.apache.catalina.util.LifecycleBase.level = FINE # To see debug messages in TldLocationsCache, uncomment the following line: #org.apache.jasper.compiler.TldLocationsCache.level = FINE 

So, I added the aditional handler "5reportExport.org.apache.juli.AsyncFileHandler" to get the material in a separate file. Then I configured this handler, like the others. The most important part at the end:

 com.prodyna.reportExport.level = FINE com.prodyna.reportExport.handlers = 5reportExport.org.apache.juli.AsyncFileHandler 

This uses a configured handler for all classes in the com.prodyna.reportExport package.

+2
source

Source: https://habr.com/ru/post/1215151/


All Articles