Java 登录到 tomcat catalina.out 的最简单方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19933036/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
simplest way to log to tomcat catalina.out
提问by tokhi
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.
将日志写入的最简单方法是什么tomcat/catalina.out
?我已经尝试过System.out.print()
并且System.err.print()
工作正常。
I just want to know if there are better options to use despite of sysout
and syserr
?
我只是想知道是否有更好的选择可以使用,尽管sysout
和syserr
?
回答by user455497
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");
回答by Basil Bourque
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?adeto work with any of several pluggable implementations. Those implementations include the java.util.logging tool bundled with Java, log4j, Logback, and more.
对于您自己的日志记录(由您自己的 servlet 代码生成的日志记录项目),Simple Logging Facade for Java (SLF4J)是现代方式。该接口充当与多个可插入实现中的任何一个一起工作的外观。这些实现包括与 Java、log4j、Logback 等捆绑在一起的 java.util.logging 工具。
Logbackis 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.
Logback是你应该看看的。由多年前创建著名的 log4j 的人创建。Logback 是 log4j 的继承者。同一个人还创建了 SLF4J,以防止您被锁定在任何一个日志记录框架中。
Your servlet code ? SLF4J ? Logback ? logs
您的 servlet 代码?SLF4J ? 登录?日志
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.
您可以通过多种方式配置 LogBack 以满足您的需求。您可以写入文本文件、将日志项发送到数据库等。
Tomcat's Logging
Tomcat 的日志记录
Besides your own logging, there is the question of the logging performed by Tomcat itself, to track every incoming request.
除了您自己的日志记录之外,还有 Tomcat 本身执行的日志记录问题,以跟踪每个传入的请求。
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.
Tomcat 通过 Valve 执行此类日志记录。默认情况下,这些日志项只是写入文本文件。您可能想做的不止这些。
Logback provides an extra module, logback-accessfor use with both the servlet containers Tomcatfrom Apache & Jettyfrom 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.).
Logback 提供了一个额外的模块,logback-access用于Apache 的Tomcat和Eclipse 的Jettyservlet 容器。该模块logback-access
是 Tomcat 的 Valve 的扩展。通过扩展 Valve,您可以替换默认行为。您可以将 Tomcat 自己的日志项发送到您的 Logback 基础设施(文件、数据库记录等)。