Spring and logging properties file - java

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

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.

log4j to write log to console and also to file

I use Selenium WebDriver and log4j in my Java project. I set log4j.properties as follows:
# Define the root logger with appender file
log4j.rootLogger = DEBUG, stdout, FILE
# Define the file appender
log4j.appender.FILE=org.apache.log4j.FileAppender
# Set the name of the file
log4j.appender.FILE.File=C:/_privat/myproj/log/log.out
# 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
# Redirect log messages to console
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p [%-20c{1}:%3L] %m%n
log4j.appender.stdout.Threshold=INFO
During execution I see only these lines instead of full log.
Starting ChromeDriver 75.0.3770.8 (681f24ea911fe754973dda2fdc6d2a2e159dd300-refs/branch-heads/3770#{#40}) on port 3320
Only local connections are allowed.
Please protect ports used by ChromeDriver and related test frameworks to prevent access by malicious code.
jún. 14, 2019 8:26:53 DE org.openqa.selenium.remote.ProtocolHandshake createSession
INFO: Detected dialect: W3C
When execution is stopped I get all the log messages also to console, which was written to file. I need to see log also in console during execution.

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.

Apache Commons Configuration -- Cannot load/use config files

I've been trying to configure a Java application (specifically my integration tests) with absolutely no success. I originally had a single XMLConfiguration which I could load and use without issue. My directory setup looks like:
src
test
java
mypackage
resources
Before, I had a file myconfig.xml in the resources directory. I was loading it it like so:
private static XMLConfiguration configuration = null;
public static void configure() {
try {
configuration = new XMLConfiguration("myconfig.xml");
}
catch (ConfigurationException e) {
System.err.println(e);
}
}
Now I'm trying to make it so that users can configure the application themselves by writing a configuration file in /etc or /home or whatever, so I made a new file in the same location called config-test.xml:
<?xml version="1.0" encoding="ISO-8859-1"?>
<configuration>
<!-- Attempt to load configuration provided on the system -->
<xml fileName="/etc/myconf/myconfig.xml" config-optional="true" />
<!-- Load default configuration from the classpath -->
<xml fileName="myconfig.xml" />
</configuration>
The file /etc/myconf/myconfig.xml does not exist, but that's the whole point of making it optional. I've tried a few different approaches to loading my configuration, including how the documentation does it: http://commons.apache.org/configuration/userguide/howto_configurationbuilder.html. What I have currently is the following:
private static CombinedConfiguration configuration = null;
public static void configure() {
try {
DefaultConfigurationBuilder builder =
new DefaultConfigurationBuilder("config-test.xml");
configuration = builder.getConfiguration(true);
System.err.println("Yay");
}
catch (ConfigurationException e) {
System.err.println("Herp");
}
catch (Exception e) {
System.err.println("Derp");
}
}
When I run my integration tests, I see no Yay, I see no Herp and I see no Derp. If I place a println before I try to load things, it does print so configure is being called. The output I get from commons configuration amounts to:
-------------------------------------------------------
T E S T S
-------------------------------------------------------
Running TestSuite
13:36:57.752 [main] DEBUG o.a.c.c.ConfigurationUtils - ConfigurationUtils.locate(): base is null, name is config-test.xml
13:36:57.753 [main] DEBUG o.a.c.c.DefaultFileSystem - Could not locate file config-test.xml at null: no protocol: config-test.xml
13:36:57.756 [main] DEBUG o.a.c.c.ConfigurationUtils - Loading configuration from the context classpath (config-test.xml)
13:36:57.766 [main] DEBUG o.a.c.c.DefaultConfigurationBuilder - Creating configuration null with name null
13:36:57.795 [main] DEBUG o.a.c.c.ConfigurationUtils - ConfigurationUtils.locate(): base is null, name is config-test.xml
13:36:57.795 [main] DEBUG o.a.c.c.DefaultFileSystem - Could not locate file config-test.xml at null: no protocol: config-test.xml
13:36:57.796 [main] DEBUG o.a.c.c.ConfigurationUtils - Loading configuration from the context classpath (config-test.xml)
13:36:57.800 [main] DEBUG o.a.c.c.DefaultConfigurationBuilder - Creating configuration null with name null
Tests run: 56, Failures: 2, Errors: 0, Skipped: 16, Time elapsed: 2.589 sec <<< FAILURE!
Results :
Failed tests: initialize(mypackage.ImportTestIT): org/apache/commons/beanutils/PropertyUtils
initialize(mypackage.TransferTestIT): org/apache/commons/beanutils/PropertyUtils
Tests run: 56, Failures: 2, Errors: 0, Skipped: 16
Those initialize methods are just calling configure and then trying to get data from the configuration that is loaded. I've been working with this for a couple hours now and could really use another pair of eyes. Any ideas? Is there anything I could have changed that I'm not mentioning that would affect this? Thanks.
Edit #1 (1:58pm): After making sure beanutils were on the classpath, I now get a brutal explosion of stack trace that starts with
Running TestSuite
14:00:16.088 [main] DEBUG o.a.c.c.ConfigurationUtils - ConfigurationUtils.locate(): base is null, name is config-test.xml
14:00:16.089 [main] DEBUG o.a.c.c.DefaultFileSystem - Could not locate file config-test.xml at null: no protocol: config-test.xml
14:00:16.093 [main] DEBUG o.a.c.c.ConfigurationUtils - Loading configuration from the context classpath (config-test.xml)
14:00:16.103 [main] DEBUG o.a.c.c.DefaultConfigurationBuilder - Creating configuration null with name null
14:00:16.178 [main] DEBUG o.a.c.c.ConfigurationUtils - ConfigurationUtils.locate(): base is file:///home/pgarrity/projects/myproject/target/test-classes/config-test.xml, name is /etc/myconf/myproject/myconfig.xml
14:00:16.179 [main] DEBUG o.a.c.c.DefaultFileSystem - Could not locate file /etc/myconf/myproject/myconfig.xml at file:///home/pgarrity/projects/myproject/target/test-classes/config-test.xml: /etc/myconf/myproject/myconfig.xml (No such file or directory)
14:00:16.181 [main] DEBUG o.a.c.c.DefaultConfigurationBuilder - Load failed for optional configuration xml: Cannot locate configuration source /etc/myconf/myproject/myconfig.xml
14:00:16.185 [main] WARN o.a.c.c.DefaultConfigurationBuilder - Internal error
org.apache.commons.configuration.ConfigurationException: Cannot locate configuration source /etc/myconf/myproject/myconfig.xml
at org.apache.commons.configuration.AbstractFileConfiguration.load(AbstractFileConfiguration.java:259) ~[commons-configuration-1.8.jar:1.8]
at org.apache.commons.configuration.AbstractFileConfiguration.load(AbstractFileConfiguration.java:238) ~[commons-configuration-1.8.jar:1.8]
And so on and so on... but I do see 'Yay' now. Now what I don't get is that it doesn't seem to be looking for my non-optional configuration source, or assuming it's in a location I never told it to look.
Edit #2:
I looked through my output a bit more and buried in the complaints from commons I found that it seemed to load the correct file eventually. I think I may have it working? Maybe my path to the properties change when I load it like this... I've got some stuff to try. Anyway, the problem that I asked about has been fixed. Thanks for the help.
You seem to be missing commons-beanutils on your classpath. Make sure that jar is there.
Pfft. A multi-page document talks about how to load a configuration file. But did they forget to implement detection of file URLs in absolute Windows paths such as c:\Users\USER\FOO\bar.xml? Commons 1.9 will show this scrupulous message,
DEBUG DefaultFileSystem - Could not locate file C:\Users\USER\FOO\bar.xml at null: unknown protocol: c
https://github.com/apache/commons-configuration/blob/CONFIGURATION_1_9/src/main/java/org/apache/commons/configuration/DefaultFileSystem.java#L281

How to configure commons-logging for JUnit testing of Hibernate

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)

Categories

Resources