我有一个Java(6)应用程序,它使用Hibernate(V3.3.2)从HSQLDB读取数据,我使用Eclipse(V3.5.1)构建和调试/运行,并且它工作正常.
然后我创建了一个GWT(V1.7)Servlet Web应用程序,将我的hibernate类复制到其中,并添加了相同的用户库依赖项.但是,当我运行servlet并尝试访问调用我的代码的URL时,我得到:
java.lang.NoClassDefFoundError: org/slf4j/LoggerFactory
at org.hibernate.cfg.Configuration.<clinit>(Configuration.java:152)
at xxx.daoimpl.DAOSession.initialise(DAOSession.java:40)
Run Code Online (Sandbox Code Playgroud)
其中DAOSession.java:40是:
AnnotationConfiguration config = new AnnotationConfiguration ();
Run Code Online (Sandbox Code Playgroud)
谷歌搜索此错误表明我从类路径中缺少slf4j-api.jar,但是如果我查看Debug属性的命令行,我可以在那里看到这个jar:
C:\java\jsedk_6\jre\bin\javaw.exe
-agentlib:jdwp=transport=dt_socket,suspend=y,address=localhost:54541
-Xmx512m -Dfile.encoding=Cp1252
-classpath
D:\dev\workspace\xxx\src;
D:\dev\workspace\xxx\resources;
D:\dev\workspace\xxx\war\WEB-INF\classes;
C:\java\eclipse\plugins\com.google.gwt.eclipse.sdkbundle.win32_1.7.1.v200909221731\gwt-windows-1.7.1\gwt-user.jar;
C:\java\eclipse\plugins\com.google.gwt.eclipse.sdkbundle.win32_1.7.1.v200909221731\gwt-windows-1.7.1\gwt-dev-windows.jar;
C:\java\hibernate-annotations-3.4.0.GA\hibernate-annotations.jar;
C:\java\hibernate-annotations-3.4.0.GA\lib\ejb3-persistence.jar;
C:\java\hibernate-annotations-3.4.0.GA\lib\hibernate-commons-annotations.jar;
C:\java\hibernate-distribution-3.3.2.GA\hibernate3.jar;
C:\java\hibernate-distribution-3.3.2.GA\lib\required\antlr-2.7.6.jar;
C:\java\hibernate-distribution-3.3.2.GA\lib\required\commons-collections-3.1.jar;
C:\java\hibernate-distribution-3.3.2.GA\lib\required\dom4j-1.6.1.jar;
C:\java\hibernate-distribution-3.3.2.GA\lib\required\javassist-3.9.0.GA.jar;
C:\java\hibernate-distribution-3.3.2.GA\lib\required\jta-1.1.jar;
C:\java\hibernate-validator-4.0.1.GA\hibernate-validator-4.0.1.GA.jar;
C:\java\hibernate-validator-4.0.1.GA\lib\validation-api-1.0.0.GA.jar;
C:\java\hibernate-validator-4.0.1.GA\lib\log4j-1.2.14.jar;
C:\java\hsqldb\lib\hsqldb.jar;
C:\java\restlet-jse-2.0m5\lib\org.restlet.jar;
C:\java\restlet-jee-2.0m5\lib\org.restlet.ext.servlet.jar;
C:\java\restlet-jse-2.0m5\lib\org.restlet.ext.xml.jar;
C:\java\slf4j-1.5.8\slf4j-api-1.5.8.jar;
C:\java\slf4j-1.5.8\slf4j-log4j12-1.5.8.jar
com.google.gwt.dev.HostedMode
...
Run Code Online (Sandbox Code Playgroud)
如果我打开jar,我可以在那里看到LoggerFactory类.
知道为什么它没有被类加载器找到?
编辑1:如果尝试从我的代码访问org.slf4j.LoggerFactory,Eclipse编译好了,但我在运行时得到了同样的错误.
编辑2:如果我添加一个带有main的Test类,它调用相同的代码并运行它,它就可以工作.所以这个类路径问题似乎特定于Servlet.
谢谢,琼
我有一些库在我身上调用System.out.println,我想通过log4j或commons日志记录重定向它们.但特别是我想保留完全限定的类名,以便我知道哪个组件生成了日志.
有一个很好的,有序的方法来实现这一目标吗?
更新:完成此操作后,我在此处发布了代码:
http :
//www.bukisa.com/articles/487009_java-how-to-redirect-stderr-and-stdout-to-commons-logging-with-the-calling-class
我正在使用spring 3.0.6.我的应用程序有很多添加日志的地方(slf4j).假设我需要在每个严重错误上添加一些函数 - 我会更好地捕获每次调用错误级别的日志记录,并且可以在它之后执行工作 - 使用异常消息发送邮件到支持,或者像那样smth - 而不是手动添加代码应用程序中的所有位置.
我创建了以下课程:
@Aspect
public class LoggingWrapper {
@Pointcut("execution (* org.slf4j.Logger.error(..))")
public void logError() {
}
@AfterReturning("logError()")
public void afterError() {
//System.out.println("LOGERROR ASPECT AFTER");
//send email...
}
}
Run Code Online (Sandbox Code Playgroud)
在春季配置:
<aop:aspectj-autoproxy />
<bean id="loggingWrapper" class="com.app.services.LoggingWrapper"/>
Run Code Online (Sandbox Code Playgroud)
Aspect在我的课程中运作良好,但对于org.slf4j.Logger - 什么都没发生
我开始使用SLF4J进行日志记录,而对我来说第一件事就是下面这段代码
public class MyClass
{
private static final logger = org.slf4j.LoggerFactory.getLogger(MyClass.class)
}
Run Code Online (Sandbox Code Playgroud)
使用类作为参数来获取记录器实例的设计原则或逻辑是什么?
我正在尝试完成一些模型的基本单元测试.但是我收到以下错误.现在看来我有两个SLF4J绑定.这是因为我使用的Mahout有一个版本的SLF4J,Play有自己的版本.
谁能告诉我如何解决这个问题?
SLF4J:请参阅http://www.slf4j.org/codes.html#multiple_bindings以获取解释.SLF4J:在类路径上检测到jcl-over-slf4j.jar和slf4j-jcl.jar,抢占StackOverflowError.SLF4J:有关详细信息,另请参见http://www.slf4j.org/codes.html#jclDelegationLoop.
java.lang.ExceptionInInitializerError
at org.slf4j.impl.StaticLoggerBinder.<init>(StaticLoggerBinder.java:82)
at org.slf4j.impl.StaticLoggerBinder.<clinit>(StaticLoggerBinder.java:51)
at org.slf4j.LoggerFactory.bind(LoggerFactory.java:121)
at org.slf4j.LoggerFactory.performInitialization(LoggerFactory.java:111)
at org.slf4j.LoggerFactory.getILoggerFactory(LoggerFactory.java:268)
at org.slf4j.LoggerFactory.getLogger(LoggerFactory.java:241)
at play.api.Logger$.<init>(Logger.scala:178)
at play.api.Logger$.<clinit>(Logger.scala)
at play.api.Application.<init>(Application.scala:106)
at play.api.test.FakeApplication.<init>(Fakes.scala:141)
at play.test.FakeApplication.<init>(FakeApplication.java:24)
at play.test.Helpers.fakeApplication(Helpers.java:86)
at databaseTest.startApp(databaseTest.java:31)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:27)
at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:31)
at org.junit.runners.ParentRunner.run(ParentRunner.java:292)
at org.junit.runner.JUnitCore.run(JUnitCore.java:157)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:71)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:199)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:62)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:120)
Caused by: java.lang.IllegalStateException: Detected both jcl-over-slf4j.jar AND slf4j-jcl.jar on the class path, …Run Code Online (Sandbox Code Playgroud) private static final Logger LOGGER = LoggerFactory.getLogger(Updater.class);
Run Code Online (Sandbox Code Playgroud)
我正在使用SLF4J和Logback
当我尝试记录语句时
LOGGER.info("{}:{}:{}", one, two, three)
Run Code Online (Sandbox Code Playgroud)
它说
cannot find symbol method info(java.lang.String,java.lang.String,java.lang.String,java.lang.String)
Run Code Online (Sandbox Code Playgroud)
我没有办法在一个info语句中记录两个以上的变量吗?
我正在使用Spring(Core/Security/ldap等)和logback.但由于某些原因,spring没有收到logback XML中设置的loggin阈值,我可以看到,当我调试spring源代码时,我看到了
final boolean debug = logger.isDebugEnabled();
Run Code Online (Sandbox Code Playgroud)
是假的
我想提一下(我不知道它是否有任何相关性)春天使用的记录器是:
org.apache.commons.logging.LogFactory
org.apache.commons.logging.Log
Run Code Online (Sandbox Code Playgroud)
而不是像我使用的SLF4j
那么我应该如何启用调试级别并将日志带入我的SLF4J配置..
谢谢
作为标题,其中2个有什么区别.
什么时候更好地使用Log4j 2 SLF4J Binding,何时更好地使用Log4j 2到SLF4J适配器?
我有一个SLF4J记录器实例的类,如:
public class MyClass {
private static final Logger log = LoggerFactory.getLogger(MyClass.class);
public void foo() {
log.warn("My warn");
}
}
Run Code Online (Sandbox Code Playgroud)
我需要用JMockit测试它,如:
@Test
public void shouldLogWarn(@Mocked Logger log) throws Exception {
new Expectations() {{
log.warn(anyString);
}};
MyClass my = new MyClass();
my.foo();
}
Run Code Online (Sandbox Code Playgroud)
经过大量搜索后我发现,我需要以某种方式使用MockUp.但无法准确地得到它.
顺便说一下,我正在使用JMockit(1.29)的最新版本,你不能再为最终静态字段设置setField(log).
我们知道在Logback 中logger继承了它root的配置.
例如:
<root level="INFO">
<appender-ref ref="FILE" />
<appender-ref ref="STDOUT" />
</root>
<logger name="com.thinkaurelius.thrift" level="ERROR"/>
<logger name="org.apache.cassandra.transport" level="DEBUG">
<appender-ref ref="QUERYLOGGER" />
</logger>
Run Code Online (Sandbox Code Playgroud)
在这里logback.xml,我们定义了一个root有两个appender:
FILE (将输出重定向到文件) STDOUT (终端打印机)我们还添加了两个loggers我们定义了一些自定义日志记录级别和自定义appender.例如:所有DEBUG日志将转到QUERYLOGGER appender(磁盘上的另一个文件)org.apache.cassandra.transport
但是,所有调试信息org.apache.cassandra.transport也会出现在STDOUT,FILE因为它继承了配置root.
如果我想禁用STDOUT和FILEappender但QUERYLOGGER只保留只有org.apache.cassandra.transportlogger的appender 怎么办?