文件中log4j.rootLogger属性的意义是什么log4j.properties?如果我不使用这个属性会怎么样?
示例:
# Set root logger level to DEBUG and its only appender to A1.
log4j.rootLogger=DEBUG, A1
# A1 is set to be a ConsoleAppender.
log4j.appender.A1=org.apache.log4j.ConsoleAppender
Run Code Online (Sandbox Code Playgroud)
如果我将此属性设置为ERROR模式会发生什么.
我们使用slf4j + logback,碰巧有一些使用commons-logging的第三方库.如何设置它以使用logback?
我正在使用apache.commons.logging,现在我想使用SimpleLog实现,但是当更改级别时,来自库的记录器出来了.我想把它关掉.是否有一种简单的方法来更改整个包的日志级别(可以log4j这样做)?
我试过设置
org.apache.commons.logging.simplelog.log.foo =致命
在simplelog属性文件中禁用(设置为致命可以)foo logger,但它不起作用.(foo是输出中出现的logger的名称:[INFO] foo - Message).
Java的众多(叹息......)日志框架都可以很好地显示创建日志消息的方法的源文件名的行号:
log.info("hey");
[INFO] [Foo:413] hey
Run Code Online (Sandbox Code Playgroud)
但是如果在它们之间有一个辅助方法,那么实际的调用者将是辅助方法,并且这不是太多的信息.
log_info("hey");
[INFO] [LoggingSupport:123] hey
Run Code Online (Sandbox Code Playgroud)
有没有办法告诉日志系统在确定要打印的源位置时从callstack中删除一个帧?
我想这是特定于实现的; 我需要的是Log4J通过Commons Logging,但我很想知道其他选项.
Dave Syer(SpringSource)在他的博客中写道:
不幸的是,关于commons-logging的最糟糕的事情,以及使它不受新工具欢迎的因素,也是运行时发现算法.
为什么?它的运行时发现算法有什么问题?性能?
有谁知道我的maven构建发生了什么?我收到了很多重复的警告.
[WARNING] We have a duplicate org/apache/commons/logging/impl/LogFactoryImpl$1.class in /home/shengjie/.m2/repository/commons-logging/commons-logging-api/1.0.4/commons-logging-api-1.0.4.jar
[WARNING] We have a duplicate org/apache/commons/logging/impl/LogFactoryImpl.class in /home/shengjie/.m2/repository/commons-logging/commons-logging-api/1.0.4/commons-logging-api-1.0.4.jar
[WARNING] We have a duplicate org/apache/commons/logging/impl/NoOpLog.class in /home/shengjie/.m2/repository/commons-logging/commons-logging-api/1.0.4/commons-logging-api-1.0.4.jar
[WARNING] We have a duplicate org/apache/commons/logging/impl/SimpleLog$1.class in /home/shengjie/.m2/repository/commons-logging/commons-logging-api/1.0.4/commons-logging-api-1.0.4.jar
[WARNING] We have a duplicate org/apache/commons/logging/impl/SimpleLog.class in /home/shengjie/.m2/repository/commons-logging/commons-logging-api/1.0.4/commons-logging-api-1.0.4.jar
[WARNING] We have a duplicate org/apache/commons/logging/impl/Jdk14Logger.class in /home/shengjie/.m2/repository/commons-logging/commons-logging-api/1.0.4/commons-logging-api-1.0.4.jar
Run Code Online (Sandbox Code Playgroud)
我查看了我当地的m2 repo,我在commons-logging-api jar,LogFactoryImpl.class和LogFactoryImpl $ 1.class中有两个类.与警告中提到的所有类相同.
有一点需要提一下,我在我的pom.xml中使用了shade插件.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>1.4</version>
<configuration>
<createDependencyReducedPom>true</createDependencyReducedPom>
<filters>
<filter>
<artifact>*:*</artifact>
<excludes>
<exclude>META-INF/*.SF</exclude>
<exclude>META-INF/*.DSA</exclude>
<exclude>META-INF/*.RSA</exclude>
</excludes>
</filter>
</filters>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<transformers> …Run Code Online (Sandbox Code Playgroud) 我正在开发一个简单的桌面应用程序(不是webapp).
这是我的log4j.properties:
log4j.rootCategory=INFO, stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p %t %c{2}:%L $$$$ %m%n
log4j.logger.org.springframework=ERROR,stdout
Run Code Online (Sandbox Code Playgroud)
如您所见,为了从控制台中消除Spring日志消息,我尝试了这个解决方案:
当我从我的代码调用log4j logger时,日志消息是根据上面指定的模式(这是好的).
然而,坏的部分是 - 我仍然从春天到达控制台DEBUG级别的消息......它们看起来像这里: 无法禁用日志消息
他们有不同的模式.好像他们忽略了我的设置.
我也尝试应用我在这里找到的建议:
https://spring.io/blog/2009/12/04/logging-dependencies-in-spring
在我的pom文件中:
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>3.2.0.RELEASE</version>
</dependency>
Run Code Online (Sandbox Code Playgroud)
而且没有SLF4J.如果我没有错,上述链接中提到此选项是有效选项.
我可能会在这里失踪什么?
输出-Dlog4j.debug = true
log4j: Trying to find [log4j.xml] using context classloader sun.misc.Launcher$AppClassLoader@172aa3f.
log4j: Trying to find [log4j.xml] using sun.misc.Launcher$AppClassLoader@172aa3f class loader.
log4j: Trying to find [log4j.xml] using ClassLoader.getSystemResource().
log4j: Trying to find [log4j.properties] using context classloader sun.misc.Launcher$AppClassLoader@172aa3f.
log4j: Using URL …Run Code Online (Sandbox Code Playgroud) 我目前正在考虑升级中型到大型Java代码库中的日志记录机制.当前使用Debug类上的静态方法记录消息,我建议将其从此切换为SLF4J或commons-logging.
应用程序架构师更喜欢我将依赖性封装在SLF4J上(可能将其包装在前面提到的Debug类中).这样可以更容易地在将来更改日志记录实现.
这对我来说似乎有些过分,因为SLF4J已经在抽象具体的日志记录实现.
是否值得在另一个本土抽象中包装像SLF4J这样的第三方日志抽象?
我有一个简单的控制台应用程序,它使用apache的PDFBox库,后者又使用commons日志记录.我在我的控制台中收到了很多垃圾邮件,我想抑制它:
2011年2月15日下午3:56:40 org.apache.pdfbox.util.PDFStreamEngine processOperator信息:不支持/禁用操作:EI
在我的代码中,我试图重置日志级别无济于事:
Logger.getLogger("org.apache.pdfbox.util.PDFStreamEngine").setLevel(Level.OFF);
Logger.getLogger("org.apache.pdfbox.util").setLevel(Level.OFF);
Logger.getLogger("org.apache.pdfbox").setLevel(Level.OFF);
Run Code Online (Sandbox Code Playgroud)
尽管有这些设置,但消息仍然显示在控制台上.从Commons日志记录中检索日志对象也没有帮助,因为它似乎没有办法设置级别.
有没有办法以编程方式抑制这些消息?或者我是否需要添加配置文件?