如何使用Logback捕获Java程序中的System.out消息?
例如,我想使用此代码:
System.out.println("test: console out to file instead");
Run Code Online (Sandbox Code Playgroud)
...并将其输出捕获到文件中.
可以使用logback.xml配置文件完成吗?
即使我已经向以下尝试提供帮助的用户颁发奖金,但最初的问题仍未得到答复.没有实际可行的解决方案来确保在junit测试中遵守logback.groovy配置的日志记录.测试加载了logback配置,它确实报告了正确的级别,但实际的测试记录(仅通过slf4j)仍处于ANY或TRACE级别
我知道其他人遇到了同样的问题,并且由于控制台日志记录过于冗长,大型项目的测试需要花费更长时间才会非常烦人.我无法继续向这个问题投掷赏金.我希望有人提出一个很好的解决方案,允许通过系统属性在不同级别正确配置测试日志.然后,可以为项目创建不同的配置,以便可以在不同的日志记录阈值级别配置测试.
我的日志记录是通过logback.groovy文件进行logback配置的
现在,当我的Maven POM项目聚合所有其他项目时,它会传递所有System属性以设置正确的日志记录级别.
但是,当junit测试运行时,由于某种原因,即使测试类静态@beforeClass确保正确配置了logback,记录器也不会获取正确的级别.
测试中的记录器不是问题所在 - 嗯 - 是的 - 它们真正的问题是运行的代码段中的记录器(我所有程序记录器到处都是)被设置为错误的记录级别.在配置程序测试时,他们没有了解日志记录的内容.
但是,当logback使用logback.goovy文件初始化时,项目报告正确.但是,实际日志记录级别设置为TRACE或ALL
从下面的输出中可以看出,logback已配置为INFO.但TRACE(最后一行)的第一个项目日志声明表明没有被提取.
救命.
-------------------------------------------------------
T E S T S
-------------------------------------------------------
Running groovy.text.StreamingTemplateEngineTest
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 1.245 sec
Running net.abcd.templating.InlinerTest
01:22:15,265 |-INFO in ch.qos.logback.classic.gaffer.ConfigurationDelegate@1e8c3cb - Added status listener of type [ch.qos.logback.core.status.OnConsoleStatusListener]
01:22:15,290 |-INFO in ch.qos.logback.classic.gaffer.ConfigurationDelegate@1e8c3cb - Setting ReconfigureOnChangeFilter scanning period to 5 minutes
01:22:15,290 |-INFO in ReconfigureOnChangeFilter{invocationCounter=0} - Will scan for changes in [[C:\Users\ABDC\Dropbox\workspace\abcd\AbcdTemplating\conf\logback.groovy]] every 300 seconds.
01:22:15,290 |-INFO …
Run Code Online (Sandbox Code Playgroud) 在我的输出中,我有来自Jersey的JUL记录消息
03.12.2010 14:14:55 com.sun.jersey.api.core.PackagesResourceConfig init
INFO: Scanning for root resource and provider classes in the packages:
Run Code Online (Sandbox Code Playgroud)
以编程方式我想把它们关掉,所以我尝试了
Logger.getLogger("com.sun.jersey.api.core.PackagesResourceConfig").setLevel( Level.SEVERE );
Run Code Online (Sandbox Code Playgroud)
要么
Logger.getLogger("com.sun.jersey").setLevel( Level.SEVERE );
Run Code Online (Sandbox Code Playgroud)
但这不起作用.
有趣的是这个全局配置有效:
Logger.getLogger( "com" ).setLevel( Level.SEVERE );
Run Code Online (Sandbox Code Playgroud)
要么
Logger.getLogger( "" ).setLevel( Level.SEVERE );
Run Code Online (Sandbox Code Playgroud)
为什么?
我在我的webapp中使用postgres db.
我添加了以下依赖项pom.xml
:
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.5</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>jul-to-slf4j</artifactId>
<version>1.7.5</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.0.13</version>
</dependency>
Run Code Online (Sandbox Code Playgroud)
在IntelliJ Idea控制台日志中,我看到以下文本(深色,这意味着它是stdout-ed):
Nov 17, 2013 12:32:40 PM org.apache.naming.NamingContext lookup
WARNING: Unexpected exception resolving reference
org.postgresql.util.PSQLException: ???????????????: ???????????????????????????????????? "traffic" ?????? ?????????????????? ???????????????????????? ????????????????????????????????? (?????? ??????????????????)
at org.postgresql.core.v3.ConnectionFactoryImpl.doAuthentication(ConnectionFactoryImpl.java:291)
at org.postgresql.core.v3.ConnectionFactoryImpl.openConnectionImpl(ConnectionFactoryImpl.java:108)
at org.postgresql.core.ConnectionFactory.openConnection(ConnectionFactory.java:66)
at org.postgresql.jdbc2.AbstractJdbc2Connection.<init>(AbstractJdbc2Connection.java:125)
at org.postgresql.jdbc3.AbstractJdbc3Connection.<init>(AbstractJdbc3Connection.java:30)
at org.postgresql.jdbc3g.AbstractJdbc3gConnection.<init>(AbstractJdbc3gConnection.java:22)
at org.postgresql.jdbc4.AbstractJdbc4Connection.<init>(AbstractJdbc4Connection.java:30)
at org.postgresql.jdbc4.Jdbc4Connection.<init>(Jdbc4Connection.java:24)
at org.postgresql.Driver.makeConnection(Driver.java:393)
at org.postgresql.Driver.connect(Driver.java:267)
at org.apache.tomcat.jdbc.pool.PooledConnection.connectUsingDriver(PooledConnection.java:267)
[...skipped...]
javax.management.remote.rmi.RMIConnectionImpl.doPrivilegedOperation(RMIConnectionImpl.java:1420)
at javax.management.remote.rmi.RMIConnectionImpl.invoke(RMIConnectionImpl.java:848)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at …
Run Code Online (Sandbox Code Playgroud) 我的 tomcat 日志当前混合了不同格式的日志消息。所以我尝试使用 JUL 到 SLF4J 桥来设置 SLF4J。
我最初收到的日志消息如下
五月 23, 2013 7:57:17 PM org.apache.catalina.core.StandardService startInternal
INFO: 启动服务 Catalina
五月 23, 2013 7:57:17 PM org.apache.catalina.core.StandardEngine startInternal
INFO: 启动 Servlet 引擎:Apache Tomcat/7.0.29
2013-05-23 19:57:47,224 [localhost-startStop-1]从类路径资源加载XML bean定义[META-INF/cxf/cxf.xml]
2013-05-23 19: 57:47,246 [localhost-startStop-1] 从类路径资源加载 XML bean 定义 [META-INF/cxf/cxf-servlet.xml]
我从这里关注了博客.. http://hwellmann.blogspot.com/2012/11/logging-with-slf4j-and-logback-in.html
我正在为 SLF4J 使用以下 jar: jul-to-slf4j-1.7.5.jar、slf4j-api-1.7.5.jar
并在logging.properties中
处理程序 = org.slf4j.bridge.SLF4JBridgeHandler
我目前没有收到混合消息,但在启动 tomcat 时收到。它不是打印tomcat格式,而是抛出Handler错误
下面是日志的片段
Handler error
java.lang.ClassNotFoundException: org.slf4j.bridge.SLF4JBridgeHandler
at java.net.URLClassLoader$1.run(URLClassLoader.java:217)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:205)
at java.lang.ClassLoader.loadClass(ClassLoader.java:321)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:294)
at java.lang.ClassLoader.loadClass(ClassLoader.java:266)
at org.apache.juli.ClassLoaderLogManager.readConfiguration(ClassLoaderLogManager.java:521)
at …
Run Code Online (Sandbox Code Playgroud) 我希望从我的应用程序中嵌入的HornetQ的所有日志记录委托给slf4j.不幸的是,任何尝 我已经尝试过了
SLF4JBridgeHandler.install();
Log4jLogDelegateFactory
("hornetq-configuration.xml"经过<log-delegate-factory-class-name>org.hornetq.integration.logging.Log4jLogDelegateFactory</log-delegate-factory-class-name>
)LogDelegate
和LogDelegateFactory
但无论我做什么,下面的线路都没有抓住:
13.07.2011 17:42:11 org.hornetq.core.logging.impl.JULLogDelegate warn
WARNUNG: AIO wasn't located on this platform, it will fall back to using pure Java NIO. If your platform is Linux, install LibAIO to enable the AIO journal
13.07.2011 17:42:11 org.hornetq.core.logging.impl.JULLogDelegate info
INFO: Using NIO Journal
13.07.2011 17:42:11 org.hornetq.core.logging.impl.JULLogDelegate info
INFO: Started Netty Acceptor version 3.1.5.GA-r1772
Run Code Online (Sandbox Code Playgroud)
我错过了什么?
JDK11引入了新的HTTP Client,具有许多传统java.net.HttpURLConnection
类所缺乏的功能。我遇到的第一个问题是如何在新添加的 HTTP 客户端中正确启用日志记录?