How to configure commons-logging for JUnit testing of Hibernate - java

I'm trying to debug some hibernate functionality in a spring app with a junit test and commons logging, but I can't seem to get anything other than the default INFO messages to appear. I'm also running these junit tests from Eclipse.
I've had no luck from the spring forums either.
I'm particularly interested in the debug logging output by Hibernate (to try and figure out why it takes 23 seconds to run this test).
Current output shows the default setting of INFO:
Mar 29, 2011 4:44:35 PM org.springframework.test.AbstractTransactionalSpringContextTests onSetUp
INFO: Began transaction: transaction manager [org.springframework.orm.hibernate3.HibernateTransactionManager#5f873eb2]; defaultRollback true
testGetSubjectsForSite time: [00:00:00:068]
Mar 29, 2011 4:44:58 PM org.springframework.test.AbstractTransactionalSpringContextTests endTransaction
INFO: Rolled back transaction after test execution
I've tried to add a commons-logging.properties file to the classpath (the same location as the hibernate.properties and test-components.xml) but still only the default INFO messages appear.
Here's the commons-logging.properties file:
org.apache.commons.logging.Log=org.apache.commons.logging.impl.Jdk14Logger
# handlers
handlers=java.util.logging.ConsoleHandler
# default log level
.level=FINE
org.springframework.level=FINE
org.hibernate.level=FINE
# level for the console logger
java.util.logging.ConsoleHandler.level=FINE
java.util.logging.ConsoleHandler.formatter=java.util.logging.SimpleFormatter
Is anyone able to shed any light on why I can't get the DEBUG messages to print out? Is there a logging setting I'm missing?
Edit: I've tried FINEST and DEBUG to no avail.

Unfortunately, it seems the logging configuration file used by Jdk14Logger should be specified at runtime.
See the following file in your JDK directory: JDK_HOME/jre/lib/logging.properties (it's the default one used if no config file is found)
Moreover, the file path should be absolute, otherwise it's relative to the folder where the JRE is executed - see the code of java.util.logging.LogManager.readConfiguration()
Also see:
http://www.javapractices.com/topic/TopicAction.do?Id=143
http://cyntech.wordpress.com/2009/01/09/how-to-use-commons-logging/

Your default and hibernate logging is at Level "FINE" which is more of a "INFO" in log4j terms.
You need set DEBUG level for org.hibernate which in JDK logging is equal to 'FINEST'
Set
org.hibernate.level=FINEST (in the above log which should enable debug logs)

Related

install4j log file i4j_log - Change log level

In our install4j Auto updater we are facing some problems and we are trying to find out more information from the log file i4j_log_xxx from the Users/Temp folder.
The contents inside this file start with the follwing log level [INFO]....
[INFO] logger started at Thu Nov 25 19:34:10 CET 2021
The question for me is how to change the log level from INFO to DEBUG?. Or is this even possible via arguments?. From the install4j documentation, I was not able to find any information on the log level changes.
It is not possible to change the log level. All messages are logged, their severity is displayed as [INFO] or [ERROR], but there is no filter.

Remote IE click has no effect

Nothing happens after clicking on the web element. I have even print the statement after the click operation which is executed correctly but after that is is failing as click operation is not changing the object state.
Steps to reproduce -
create a selenium script to load google home page.
click on the I'm feeling Lucky button`enter code here.
it should load the doodle page.
When I run this script on my local machine, it worked fine. Now i have created an executable jar file using maven and eclipse to run my test suit and downloaded this jar on my remote desktop machine on server with all the necessary dependent files.
When I run the test using CMD java -jar "test-suit.jar".
It is opening IE browser successfully and load the url . It is clicking on I'm feeling lucky but it does not load doodle page after click and test fails . no error is given just i got this log:
log4j:WARN No appenders could be found for logger (org.apache.http.client.protoc
ol.RequestAddCookies).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more in
fo.
Sep 20, 2018 7:28:39 AM io.github.bonigarcia.wdm.WebDriverManager getDriversFrom
Xml
INFO: Reading https://selenium-release.storage.googleapis.com/ to seek IEDriverS
erver
Sep 20, 2018 7:28:40 AM io.github.bonigarcia.wdm.WebDriverManager getLatest
INFO: Latest version of IEDriverServer is 3.14
Sep 20, 2018 7:28:40 AM io.github.bonigarcia.wdm.Downloader checkBinary
INFO: Using binary driver previously downloaded
Sep 20, 2018 7:28:40 AM io.github.bonigarcia.wdm.WebDriverManager exportDriver
INFO: Exporting webdriver.ie.driver as C:\Users\debolin.dhar.m2\repository\webd
river\IEDriverServer\Win32\3.14\IEDriverServer.exe
null
true
null
false
Started InternetExplorerDriver server (32-bit)
3.14.0.0
Listening on port 24032
Only local connections are allowed
Sep 20, 2018 7:28:47 AM org.openqa.selenium.remote.ProtocolHandshake createSessi
on
INFO: Detected dialect: W3C
true
false
clicked on I'm feeling lucky
doodle Page doesn't exist
===============================================
Suite
Total tests run: 4, Failures: 1, Skips: 1
I don't understand what could be the error. Whether it is not clicking and not throwing any error also or I need to take certain additional settings to be set on my server machine or change in my script to get it work.

Embedded Tomcat using log4j for logging

I'm using embedded Tomcat 8.5.4, i.e.,
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-core</artifactId>
<version>8.5.4</version>
</dependency>
The implementation is working perfectly (Tomcat works like a charm), the only thing that's bothering me, is that the embedded Tomcat logs on System.out. Internally within my application I'm using log4j for logging, so this leads to the following logging mixture (and not logging of Tomcat to any file):
...
2017-07-30 17:57:54 DEBUG EmbeddedTomcat:136 - Binding servlet 'sample' to path '/sample/*'.
Jul 30, 2017 5:57:54 PM org.apache.coyote.AbstractProtocol init
INFO: Initializing ProtocolHandler ["http-nio-15000"]
Jul 30, 2017 5:57:54 PM org.apache.tomcat.util.net.NioSelectorPool getSharedSelector
INFO: Using a shared selector for servlet write/read
Jul 30, 2017 5:57:54 PM org.apache.catalina.core.StandardService startInternal
INFO: Starting service Tomcat
Jul 30, 2017 5:57:54 PM org.apache.catalina.core.StandardEngine startInternal
INFO: Starting Servlet Engine: Apache Tomcat/8.5.4
Jul 30, 2017 5:57:54 PM org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler [http-nio-15000]
2017-07-30 17:57:54 INFO EmbeddedTomcat:80 - Successfully started Tomcat on port 15000 (base: null, url: http://localhost:15000).
...
In this snippet the first and the last line (after and before the ...) are logged by my application using log4j and the configuration of log4j (which writes to a file and the System.out). Nevertheless, the middle part (logging of Tomcat) is handled by the Embedded Tomcat and I have no clue, how to get Tomcat to use the available log4j (and it's configuration).
I tried to add the following dependency (a 8.5.4 version is not available on Maven Repository or Maven Central), but without any success.
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-logging-log4j</artifactId>
<version>8.5.2</version>
</dependency>
Does anyone know how to get the Embedded Tomcat to log using log4j (Version 1, I'm not using log4j2)?
I looked at/tried the following StackOverflow answers:
https://tomcat.apache.org/tomcat-8.0-doc/logging.html
So I looked at the documentation, which mentions log4j as logging framework. It mentions tomcat-juli-adapters.jar, which I could not find for the embedded version (is it the same as the "normal" Tomcat?). How would I do that programmatically, i.e., within my Embedded Tomcat implementation.
Tomcat Logging using log4j?
That's not really the problem I'm having, it's not based on the Embedded Tomcat, the version is pretty old, and I'm actually using log4j not System.out.
Embedded Tomcat logging over logback / sl4j
This question actually deals with logback and the author mentions that I found some info about using a standalone tomcat with log4j, but the standalone is different, I see the author is using similar dependencies, but not sure if there was ever a solution.
How to enable embedded tomcat logging
First I thought his may be the solution, but it all just deals with an additional logging for the Embedded Tomcat. I want the Embedded Tomcat to use the applications log4j, so one log4j.properties file, which defines how everything is logged.
Logging in Embedded Tomcat
I'm not sure why this answer is even marked as correct, but this is just an explanation how Tomcat writes the catalina.out file and not how the logging of the Embedded Tomcat works.
It took me a while, but after I got the sources of the 8.5.4 implementation, I realized that the juli logging implementation is added in the core jar.
Version <= 8.5.2
So I went back and started to work with the 8.5.2 version and used tomcat-embed-logging-log4j-8.5.2.jar, as well as tomcat-embed-core-8.5.2.jar. The first important thing to notice is, that against most of the documentation online, it is important that you do not add the tomcat-embed-logging-juli-8.5.2.jar. With that said, the 8.5.2 version works with log4j out of the box and there is nothing more to be done.
Version > 8.5.2
When using a newer version of the embedded Tomcat, i.e., 8.5.4. or even the newest one 8.5.19, the LogFactory is already included in the jar. Thus, when adding the older tomcat-embed-logging-log4j-8.5.2.jar on the classpath there are two LogFactory implementations available now. The first one is provided with the core and loads the DirectJDKLog (I refer to this one as Core-LogFactory) and the second one is provided through log4j (referred to as Log4j-LogFactory). Thus, when the LogFactory is loaded from the class-path, the Core-LogFactory is picked (because it is in the same jar and thus "closer" (no need to get too deep into the classpath loading order)). In general, it is bad practice to have the same class (in the same package) on the classpath. This will only lead to confusion and you mostly never know which class is actually used (yes I know there are ways and rules, but long story short, it is not good). Thus, I decided instead of using the tomcat-embed-logging-log4j-8.5.2.jar furthermore, I followed the ServiceLoader approach, which is actually implemented in the Core-LogFactory of the newer versions (https://svn.apache.org/repos/asf/tomcat/trunk/java/org/apache/juli/logging/LogFactory.java).
private LogFactory() {
// Look via a ServiceLoader for a Log implementation that has a
// constructor taking the String name.
ServiceLoader<Log> logLoader = ServiceLoader.load(Log.class);
Constructor<? extends Log> m=null;
for (Log log: logLoader) {
Class<? extends Log> c=log.getClass();
try {
m=c.getConstructor(String.class);
break;
}
catch (NoSuchMethodException | SecurityException e) {
throw new Error(e);
}
}
discoveredLogConstructor=m;
}
To do so, I added the file org.apache.juli.logging.Log in the META-INF/services folder (within my jar/sources) and added the fully qualified name of my "own" Log implementation, i.e., net.meisen.tomcat.logging.Log4jLog, which is as following:
package net.meisen.tomcat.logging;
import org.apache.juli.logging.Log;
import org.apache.log4j.Logger;
public class Log4jLog implements Log {
private final Logger logger;
// this constructor is important, otherwise the ServiceLoader cannot start
public Log4jLog() {
logger = Logger.getLogger(Log4jLog.class);
}
// this constructor is needed by the LogFactory implementation
public Log4jLog(final String name) {
logger = Logger.getLogger(name);
}
// now we have to implement the `Log` interface
#Override
public boolean isFatalEnabled() {
return true;
}
// ... more isLevelEnabled()
#Override
public boolean isTraceEnabled() {
return logger.isTraceEnabled();
}
// ... and also all the fatal(...) - trace(...) methods
#Override
public void fatal(final Object msg) {
logger.fatal(msg);
}
#Override
public void fatal(final Object msg, final Throwable throwable) {
logger.fatal(msg, throwable);
}
}
And et voilĂ , here is the final result:
2017-07-31 19:27:04 TRACE EmbeddedTomcat:48 - Initializing Tomcat on port 15000 (base: null)...
2017-07-31 19:27:33 INFO Http11NioProtocol:69 - Initializing ProtocolHandler ["http-nio-15000"]
2017-07-31 19:27:33 INFO NioSelectorPool:69 - Using a shared selector for servlet write/read
2017-07-31 19:27:33 INFO StandardService:69 - Starting service [Tomcat]
2017-07-31 19:27:33 INFO StandardEngine:69 - Starting Servlet Engine: Apache Tomcat/8.5.19
2017-07-31 19:27:34 WARN SessionIdGeneratorBase:79 - Creation of SecureRandom instance for session ID generation using [SHA1PRNG] took [170] milliseconds.
2017-07-31 19:27:34 INFO Http11NioProtocol:69 - Starting ProtocolHandler ["http-nio-15000"]
2017-07-31 19:27:34 INFO EmbeddedTomcat:80 - Successfully started Tomcat on port 15000 (base: null, url: http://localhost:15000).
Appendix:
Here are some links that helped me to figure out the ServiceLoader stuff and why I decided pretty fast not to have the same class in the same package in different jars in my project:
ServiceLoader using ClassLoader pointing to different path
Java ServiceLoader explanation
http://tshikatshikaaa.blogspot.com/2013/03/java-serviceloader-example.html

Figuring out Spring Logging

I've inherited a, well, messy, Spring MVC application. Some things don't work and I want to enable DEBUG or even TRACE logging to get more information and found that even that is not that simple.
When starting the application (by running a Tomcat 8 server from Eclipse), it seems to output two kinds of log messages. Here is an example of the first kind:
Jun 20, 2017 1:47:07 PM org.hibernate.Version logVersion INFO:
HHH000412: Hibernate Core {4.3.8.Final} Jun 20, 2017 1:47:07 PM
org.hibernate.cfg.Environment <clinit> INFO: HHH000206:
hibernate.properties not found Jun 20, 2017 1:47:07 PM
org.hibernate.cfg.Environment buildBytecodeProvider INFO: HHH000021:
Bytecode provider name : javassist
These messages are all in red. The second kind looks like this:
13:47:16.949 [localhost-startStop-1] INFO org.springframework.web.servlet.DispatcherServlet:489 - FrameworkServlet 'spring': initialization started
13:47:16.952 [localhost-startStop-1] INFO org.springframework.web.context.support.XmlWebApplicationContext:583 - Refreshing WebApplicationContext for namespace 'spring-servlet': startup date [Tue Jun 20 13:47:16 IDT 2017]; parent: Root WebApplicationContext
13:47:16.952 [localhost-startStop-1] INFO org.springframework.beans.factory.xml.XmlBeanDefinitionReader:317 - Loading XML bean definitions from ServletContext resource [/WEB-INF/spring-servlet.xml]
These are all in black.
Obviously there are at least two active logging formats. This is were it gets weird:
I found numerous log4j2.xml as well as log4j.propertiesfiles. All of them seem to be ignored. Changing them makes no difference. I just deleted them all, and it did not affect the logging output. In addition, log4j 1.2, log4j 2.0.2 and slf4j are all Maven dependencies. pom.xml references version 1.2, other Maven dependencies probably need the other versions.
My question is - how do I configure the logging myself? Placing a log4j.xml file under WEB-INF (were all other property files are located) doesn't make any difference. Neither that placing a log4j.properties file there. I need to somehow turn debug printouts of log4j itself, so I can figure out where it's reading it's configuration from.
I tried adding -Dlog4j.debug as a VM argument to the Tomcat run configuarion. Other than printing -Dlog4j.debug command line argument or something similar, nothing changed.
You can use following code snippet that I have used for my project. Over here class name is FolderReader.class and placed lo4j.properties file in folder "conf".Passing argument "../conf/log4j.properties" as args[2] argument when running the application.
final FileSystemXmlApplicationContext applicationContext = new FileSystemXmlApplicationContext(args[0]);
Logger logger=Logger.getLogger(FolderReader.class);
PropertyConfigurator.configure(args[2]);
logger.info("Intializing beans..");
ServiceContext serviceContext = (ServiceContext) BeanFactoryLocator.getBean("serviceContext");
PropertyConfigurator.configure(args[2]);loads the log4j.properties file from location. Configuration is taken from this property file and formatting is done accordingly.

Spring and logging properties file

I'm trying to use Spring. A dependency of Spring is
So I have added commons-logging-1.1.1.jar to my build path. This all works fine. And I get stuff like this:
Feb 28, 2013 2:40:39 PM org.springframework.context.support.AbstractApplicationContext prepareRefresh
INFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext#c63a8af: startup date [Thu Feb 28 14:40:39 GMT 2013]; root of context hierarchy
This is all great but I don't want this printed to my standard out, I would prefer it to be sent to a file.
DirectoryStructure:
project
src
com.myproject.classes
myspring.xml
log4j.properties
test
com.myproject.classes
logs
myproject.log
So I would like to get all logs redirected from the standard out to the log file instead. Is this possible with Spring?
I've used the properties file from here: http://www.tutorialspoint.com/spring/logging_with_log4j.htm,
where I got my Spring example from. But this doesn't seem to make any difference. I've added some rubbish in there too to see if any exceptions get thrown, but nothing. I've tried moving the file around to different directories, but it still doesn't work.
Here it is:
# Define the root logger with appender file
log4j.rootLogger = DEBUG, FILE
# Define the file appender
log4j.appender.FILE=org.apache.log4j.FileAppender
# Set the name of the file
log4j.appender.FILE.File=logs/myproject.log
# Set the immediate flush to true (default)
log4j.appender.FILE.ImmediateFlush=true
# Set the threshold to debug mode
log4j.appender.FILE.Threshold=debug
# Set the append to false, overwrite
log4j.appender.FILE.Append=false
# Define the layout for file appender
log4j.appender.FILE.layout=org.apache.log4j.PatternLayout
log4j.appender.FILE.layout.conversionPattern=%m%n
Anyone got any ideas?
Thanks in advance.
Try this configuration : http://forum.springsource.org/showthread.php?94000-org-springframework-util-Log4jConfigurer-is-overriding-my-log4j-xml
Read the configuration class: http://static.springsource.org/spring/docs/1.1.x/api/org/springframework/util/Log4jConfigurer.html
For source code of Log Configuration - go through this :
http://git.springsource.org/sandbox/cbeams/blobs/bb96df1ed8e3720f33353824b108104027289e5c/org.springframework.core/src/main/java/org/springframework/util/Log4jConfigurer.java

Categories

Resources