I want to log some behavior of my web application which also implements hibernate, spring and so on. When I tried to implement log4j logger from apache I had some troubles.
When I turn on logger it is also debugging hibernate and spring which I don't want. I tried to configure properties file to the specify the package of my project but it does not work.
Here is my code of property file:
log4j.rootCategory=ERROR, O
log4j.category.com.my.package= DEBUG, FILE, O
log4j.appender.FILE=org.apache.log4j.RollingFileAppender
log4j.appender.FILE.File=log/logger.log
log4j.appender.O=org.apache.log4j.ConsoleAppender
.... and some layout
It works when I switch rootCategory = DEBUG but it is also debugging the hibernate and spring as I said.
Yes, you have to specfiy the log level per package:
log4j.logger.org.hibernate=info
log4j.logger.org.springframework=info
log4j.logger.com.yourapplication=debug
Note that you should switch from categories (obsolete) to loggers. So log4j.rootLogger=...
You would need to know the name of the loggers that are actually writing stuff... The simplest way is to set the root category to error:
log4j.rootCategory=ERROR, 0
Then set the level for your logs accordingly:
log4j.com.your.package=DEBUG...
Setting the rootCategory to DEBUG will turn everything to DEBUG, unless you specifically configure a logger otherwise.
B.T.W, this is NOT a hibernate issue, this is related to how you are configuring your logger.
Related
I need to add to my logging.properties the line:
sun.net.www.protocol.http.HttpURLConnection.level = ALL
But this file is generated using log4j.xml, which is in xml format. And I'm not finding how to correctly format this property to xml to generate this line.
using log4j-1.2.8, JRE and JDK 1.8_181, Wildfly 10.1.0.Final
At first glance, you will not be able to configure the logging level for sun.net.www.protocol.http.HttpURLConnection using Log4j, no matter if you use properties or xml configuration files.
As you can see in the source code of the HttpURLConnection class, its logging is based in PlatformLogger, which in turn uses java.util.logging.
The PlatformLogger javadoc describes how it should be configured:
Platform logger provides an API for the JRE components to log
messages. This enables the runtime components to eliminate the
static dependency of the logging facility and also defers the
java.util.logging initialization until it is enabled.
In addition, the PlatformLogger API can be used if the logging
module does not exist.
If the logging facility is not enabled, the platform loggers
will output log messages per the default logging configuration
(see below). In this implementation, it does not log the
the stack frame information issuing the log message.
When the logging facility is enabled (at startup or runtime),
the java.util.logging.Logger will be created for each platform
logger and all log messages will be forwarded to the Logger
to handle.
Logging facility is "enabled" when one of the following
conditions is met:
a system property "java.util.logging.config.class" or
"java.util.logging.config.file" is set
java.util.logging.LogManager or java.util.logging.Logger
is referenced that will trigger the logging initialization.
Default logging configuration:
global logging level = INFO
handlers = java.util.logging.ConsoleHandler
java.util.logging.ConsoleHandler.level = INFO
java.util.logging.ConsoleHandler.formatter = java.util.logging.SimpleFormatter
Basically, you need to configure the system property java.util.logging.config.file pointing to an appropriate properties file with the required properties:
handlers=java.util.logging.ConsoleHandler
java.util.logging.ConsoleHandler.level=INFO
sun.net.www.protocol.http.HttpURLConnection.level=ALL
Please, consider review this related SO question, it provides different examples and further information about how to do it.
As suggested in that question, you can use the system property javax.net.debug as well:
-Djavax.net.debug=all
If you want to integrate Log4j and java.util.logging, you can try using libraries like SLF4J and the corresponding bridges. This related SO question provides more information.
Remark: Since Log4j 1.2.8 is almost 20 years old, declared end-of-life 8 years ago and with several outstanding vulnerabilities, I don't believe you want to use it.
As you can see in jccampanero's answer, the HttpURLConnection class uses PlatformLogger for logging, which in Java 7 or 8 delegates all logging to java.util.logging.LogManager.
Several alternative implementations of java.util.logging.LogManager exist, but WildFly uses its own implementation called JBoss LogManager, which is the easiest to use. To modify your logging configuration, you just need to edit your server's configuration and add:
<subsystem xmlns="urn:jboss:domain:logging:3.0">
...
<logger category="sun.net.www.protocol.http.HttpURLConnection">
<level name="DEBUG"/>
</logger>
</subsystem>
(cf. documentation).
I am using Spring Boot with Logback for logging. Currently, we have root logger set to DEBUG and then we have app package logger like com.abc.xyz set to DEBUG.
Now, what's happening is all frameworks - Hibernate, Spring etc. are printing their DEBUG logs. What we want that all framework should print only WARN and below logs, so below are 2 options which I am thinking:
Option 1: Set the ROOT logger to WARN. However I am not sure what are its implications and even not sure whether it will help in achieving what I am looking for or not.
Option 2: Create specific loggers like org.springframework and org.hibernate and set them to WARN.
My questions:
Which of the above approach is best way to have all framework logging to WARN level?
If I set the ROOT logger to WARN then will it help? And what will be its other implications?
Is it fine to set ROOT logger to WARN? Can it potentially cause to loose some application logging?
In Spring Boot, ERROR, WARN and INFO levels are always printed as default for both the root logger and all dependencies.
If you wish to print additional logs, you need to specify that in your application.properties:
logging.level.org.hibernate=DEBUG
logging.level.com.abc.xyz=DEBUG
If you set your ROOT logger to WARN, you will only see warning and error messages (unless you override this setting for different package).
I would suggest leaving the default settings for all Spring-related dependencies since this will allow you to see all info, warning and error messages. If you really need to see just warning and error messages, set the root logger to WARN.
Then, you can specify any other classes to log to DEBUG in your properties.
I'm running a web application in a Weblogic server (Im not realy familiar with ).
Via JVM args a log4j config is passed with log level DEBUG to the application.
In the log file I can also find some log entries of DEBUG level.
So far so good.
During debugging I found some calls to logger.debug() that are not in the log file.
private static final org.slf4j.Logger logger = LoggerFactory.getLogger(ActionCtr.class);
The method call is definitely hit but nothing is written to a file.
If I do a step into during debugging I see in the logger:
org.slf4j.impl.JDK14LoggerAdapter(com.example.application.ActionCtr)
"java.util.logging.FileHandler.pattern" -> "%h/java%u.log"
And this leads to some questions for me (as I can not change the running application):
1) how could it bee that it uses the application is using a mixed up log4j and java.util.logging
2) How could I determine what is used in what classes?
3) There is no %h/java%u.log (~/java*.log) so I've tried to provide a java.util.logging conform properties file,
but this changed nothing - how an I determine where the running logger got its config from to configure it right?
1) how could it be that it uses the application is using a mixed up log4j and java.util.logging
Any of those logging frameworks could be used by the application directly or a dependent library that the application is using. It doesn't take too many dependencies to end up with a bunch of logging framework hitchhikers.
The SLF4J manual explains how that logging framework wrangles all of these other frameworks. This why you are seeing the org.slf4j.impl.JDK14LoggerAdapter.
How could I determine what is used in what classes?
Assuming you mean direct usages you can use Jdeps or Javap.
how an I determine where the running logger got its config from to configure it right?
The JConsole tool can access the JUL loggers at runtime. It will also show you all of the system properties which may include paths to logging.properties files.
If the application is pragmatically configuring the logging in an non-standard way then one option would be to use the java.security.debug using the access option. Run the application under a security manager will all or all required permissions but then enable access tracing.
My application is using Spring to handle the interaction with database (Sql Server)
And commons-logging-1.1.1.jar, log4j-1.2.17.jar, slf4j-api-1.6.3.jar and slf4j-log4j12-1.7.6.jar are put into build path for the logging framework of the application.
The last two logging jar (slf4j-api and slf4j-log4j12) are for another component inside the application to use log4j.
Here is my questions:
When Spring-Jdbc runtime excecption happens, the exception is only showed in the console of eclipse with the font color red. The exception is NOT logged into the log file. But the normal log (like log.info(...)) are all in the log file. Why can't the run-time exception be in the log file and how to solve this problem.
When I use SimpleJdbcCall to call the stored procedure with parameters in MapSqlParameterSource, the following log shows up:
14:43:30 [INFO ] Added default SqlReturnUpdateCount parameter named #update-count-1
14:43:30 [INFO ] Added default SqlReturnUpdateCount parameter named #update-count-1
......
It's really annoying because the number of this message is so large. I want to turn off this particular log message without affecting another logging with the same level (INFO)
And my log4j.xml is fine I think because the logging are basically fine except the above issues.
Spring is using commons-logging internally that's why you can see the messages in your eclipse console. To redirect commons-logging to slf4j/log4j you need to remove commons-logging-1.1.1.jar from your classpath and add jcl-over-slf4j.jar from your slf4j version. To get rid of the dublicate red eclipse messages (jul and jcl) you can set the logging level in logging.properties for the console handler to warning:
java.util.logging.ConsoleHandler.level = WARNING
Second issue was solved here.
I am logging issues at my application like that:
private static Logger logger = LoggerFactory.getLogger(Student.class);
...
logger.info(msg);
logger.debug(another_msg);
I use Java and Spring framework running on Apache Tomcat 6 and slf4j for logging. When I debug my application on Intellij IDEA I see two tabs: Server and Tomcat Log. That logs are seen at Server tab. Under tomcat folder there is no file that records that logs. However I want to see it at file end if I can I want to change the log level(debug, info etc.) to see the logs.
How can I do it?
Search for the log4j.properties file in your application.
In the log4j.properties you specify the path for the log file.
See here
sl4j is not complete logging implementation, it is a facade to which we can couple other frameworks like log4j, java.util.logging, commons etc. so identify your logging framework and check the corresponding doc for the configuration file.FOr log4j it will be log4j.properties