simplest way to log to tomcat catalina.out

2019-02-17 16:54发布

What is the simplest way to write logs to tomcat/catalina.out? I have tried System.out.print() and System.err.print() is working fine.

I just want to know if there are better options to use despite of sysout and syserr?

2条回答
The star\"
2楼-- · 2019-02-17 17:08
import java.util.logging.*

Logger.getLogger (YourClass.class.getName()).log(Level.WARNING, e.getMessage(), e);
Logger.getLogger (YourClass.class.getName()).log(Level.INFO, "hello world");
查看更多
Fickle 薄情
3楼-- · 2019-02-17 17:22

Your Logging

For your own logging (logging items generated by your own servlet code), Simple Logging Facade for Java (SLF4J) is the modern way. This interface acts as a façade to work with any of several pluggable implementations. Those implementations include the java.util.logging tool bundled with Java, log4j, Logback, and more.

Logback is the one you should look at. Created by the man who years ago created the famous log4j. Logback is meant to be the successor to log4j. The same man also created SLF4J as a way to prevent you from getting locked into any one logging framework.

Your servlet code ↔ SLF4J ↔ Logback ↔ logs

You can configure LogBack in a variety of ways to suit your needs. You can write to text files, send the logging items to a database, and more.

Tomcat’s Logging

Besides your own logging, there is the question of the logging performed by Tomcat itself, to track every incoming request.

Tomcat performs such logging through a Valve. By default, those logging items are simply written to a text file. You may want to do more than that.

Logback provides an extra module, logback-access for use with both the servlet containers Tomcat from Apache & Jetty from Eclipse. The module logback-access is an extension of Tomcat's Valve. By extending Valve, you can replace the default behavior. You can send Tomcat’s own logging items to your Logback infrastructure (files, database records, etc.).

查看更多
登录 后发表回答