in one of my elicpse project I have come across this logger related snippet, I noticed that it is from slf4j library.
private static final Logger logger = LoggerFactory
.getLogger(someclass.class);
Then in some point of the code i noticed this,
logger.debug("Found {} object",
numberofobject);
My question is, when I run the program I do not see the logger output, how can I see that in eclipse?
You need to properly configure the binding for slf4j. slf4j is just a facade, not a logging tool.
Since 1.6.0 if no binding is found on the class path, then SLF4J will default to a no-operation implementation.
Have a look at the officiel manual here on how to configure slf4j.
you need to add a logging library to your project that implements the slf (just as Burkhard mentioned)
You need to configure the logger of your choice to log debug messages to the console (e.g. with a console appender in log4j)
Related
I am trying to upgrade infinispan from 8.2.4.Final to 9.1.0.Final, but get some errors from embedded slf4j while building tomcat war-file.
Logs:
SLF4J: Class path contains multiple SLF4J bindings.
SLF4J: Found binding in
[jar:file:/C:/tomcat/webapps/ROOT/WEB-INF/lib/infinispan-embedded-9.1.0.Final.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: Found binding in
[jar:file:/C:/tomcat/webapps/ROOT/WEB-INF/lib/logback-classic-1.1.6.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: See http://www.slf4j.org/codes.html#multiple_bindings for an
explanation.
SLF4J: Actual binding is of type
[org.jboss.slf4j.JBossLoggerFactory] 20-Jul-2017 16:07:34.170
ERROR
[RMI TCP Connection(5)-127.0.0.1]
com.myapp.context.LogbackLoggingConfigurator.configureLoggingExternal
Loading logger configuration from C:\my-files\conf\logback.xml
java.lang.ClassCastException: org.jboss.slf4j.JBossLoggerFactory
cannot be cast to ch.qos.logback.classic.LoggerContext
The code, mentioned in logs:
public void configureLogging(final URL config) {
final LoggerContext lc = (LoggerContext) LoggerFactory.getILoggerFactory();
try {
lc.reset();
final JoranConfigurator configurator = new JoranConfigurator();
configurator.setContext(lc);
configurator.doConfigure(config);
} catch (final JoranException je) {
logger.error("Unable to configure logback", je);
}
StatusPrinter.printInCaseOfErrorsOrWarnings(lc);
}
And wrong line is here: final LoggerContext lc = (LoggerContext) LoggerFactory.getILoggerFactory();
Firstly, I excluded slf4j-api from "infinispan-embedded" dependency, but unsuccessfully because slf4j is hardcoded.
Secondly, I added <packagingExcludes>WEB-INF/lib/infinispan-embedded-9.1.0.Final.jar!/org/slf4j/impl/StaticLoggerBinder.class</packagingExcludes> to maven-war-plugin (maybe with '!' symbol we cannot get access to nested jars, but I don't find another variants). Attempt was failed.
So, how can I fix this exception? Maybe it is possible to use maven-shade-plugin? But it seems unacceptable for my project.
I have some problem. I solve it with a trick. I exclude logback from dependencies. I used infinispan-embedded as logger. But I think it is not right way to solve the problem. I think "http://infinispan.org/" developers need to export codes about logger as another jar. Then we can exclude in maven. Actually It is not right way to be dependent logger jar because of another task.
I have some problem. I solve it with a trick. I exclude logback from dependencies. I used infinispan-embedded as logger. But I think it is not right way to solve the problem. I think "http://infinispan.org/" developers need to export codes about logger as another jar. Then we can exclude in maven. Actually It is not right way to be dependent logger jar because of another task.
I wrote to infinispan team and they quick respond. As they said infinispan-embedded is "uber-jar" it is mean "all dependencies in one". You can use each dependencies of infinispan instead of it. infinispan-core and which you want to use.
You can see detail of the opened issue by clicking on this link https://developer.jboss.org/message/975209?et=watches.email.thread#975209
on jboss website.
I have configuration the logback with specified custom logback.xml file, but the log it prints is not what I want.
This is my code to initial logback:
private void initLogBack() throws JoranException {
JoranConfigurator configurator = new JoranConfigurator();
configurator.setContext(createLogbackContext());
configurator.doConfigure(mycustomLogbackConf);
}
I think it may read some unexpected "logback.xml" files from somewhere I don't know. Is there any way to print all the configuration information that logback used?
e.g.
The configuration files it uses
The loggers defined
The debug levels defined
Is it possible?
logback 1.0.4 version has a fix with which you can set debug level at jvm level by using a property
-Dlogback.debug=true
Reference: http://jira.qos.ch/browse/LOGBACK-527
Hope it helps.
Can anyone tell me the difference between slf4j-log4j and log4j-over-slf4j? Which is more standard to use in a Java web application? I currently have both on the classpath and that is causing a runtime exception as the web server is trying to prevent a StackOverFlowException from happening.
Exception:
java.lang.IllegalStateException:
Detected both log4j-over-slf4j.jar AND slf4j-log4j12.jar on the class path
slf4j-log4j is using log4j as an implementation of slf4j.
log4j-over-slf4j causes calls to the log4j API to be 'routed' to slf4j.
You cannot use both of these JAR's at the same time.
Both are valid libraries to use and are equally 'standard', it depends on the project.
In general, if your project is using log4j already and you don't have the ability to update all of your log4j Loggers to slf4j Loggers; log4j-over-slf4j is a quick fix to be able to start using slf4j immediately.
However, if your project is new or does not have an existing logging mechanism and you choose to use slf4j, slf4j-log4j would be the way to go as it is just specifying slf4j should be bound to log4j.
That being said, I agree with c12's comment. Stop using log4j and instead use slf4j and logback.
in my project , org.slf4j.impl.Log4jLoggerFactory is in
activemq-all-5.7.0.jar
not in slf4j-log4j12.jar
the exception message mislead me
I'm trying to setup logging of application on Glassfish server. I use SLF4j to aggregate everything and Logback binding to write the files. Also I added log4j-over-slf4j, jul-to-slf4j and jcl-over-slf4j libraries to the project. Of course slf4j-api and Logback ones are there too. All the libraries are in project WEB-INF/lib and in glassfish/lib/endorsed.
So the idea is: redirect everything to SLF4j and then log with Logback.
In the code I have the following piece for test:
jclLog.debug("Login JCL: Debug level");
jclLog.error("Login JCL: Error level");
log4jLog.debug("Login Log4j: Debug level");
log4jLog.error("Login Log4j: Error level");
slfLog.debug("Login SLF4j: Debug level");
slfLog.error("Login SLF4j: Error level");
After execution there are log entries og Log4j and SLF4j in Logback-configured log files. But JCL entries are not there. They get to server.log file instead.
The question is: why JCL entries are catched with Glassfish logger and how could it be prevented?
You can't (yet) do this. See http://java.net/jira/browse/GLASSFISH-6666 for the relevant bug report.
I'm sure this is easy, but I'm failing to find it.
I have a org.apache.commons.logging.Log instance for logging, and I see that the source code of one of my dependencies has statements like:
if (logger.isDebugEnabled())
logger.debug("Doing stuff.");
I would like to enable debugging, so that I could see these messages. I'm using Maven to build, and run tests. I don't particularly care whether the solution is a command-line argument, adding something to pom.xml, or using code to set the logger itself.
In your log4j.properties specify log4j.logger.com.yourpackage=debug. If you don't have a log4j.properties, get a default one (google it) and place it on the root of your classpath.
(commons-logging will delegate to log4j)
org.apache.commons.logging.Log is not a logging framework. It is an abstract interface which helps you to have any concrete logging implementation under it.
There is a good chance that you are using log4j underneath the org.apache.commons.logging.Log api . If that is the case then place a log4j.properties at the root of your classpath. If you already have one then you just need to change the log level as specified in the file .
If you are using any other logger like jdk logger you need to find the logging configuration file for the same.