Showing posts with label log4j. Show all posts
Showing posts with label log4j. Show all posts

29 July 2011

How to add and configure log4j.XML file in Spring 3

Solution works for Spring 3.0.5 and log4j  1.2.16 (but it should works with previous/future releases of both programs)

It is really easy task:

I assumed that you already has spring 3 stuff in place.
  1. You need add log4j to your libraries in your project
  2. Add information about log4j to web.xml file (It should be /MyCozyProject/src/main/webapp/WEB-INF/web.xml  
  3. Add information about location for log4j.xml  in context.xml
  4. Configure your log4j 
Information which you need add in .2 (It tells spring where to look for information about log4j)
<listener>
<listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
</listener>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

In 3. in context.xml (usually should be /src/main/webapp/META-INF/context.xml ) add information about where your log4j.xml file is
<Context displayName=" MyCozyProject ">
    <Parameter name="log4jConfigLocation" value="file:C:\myCozyProject\log4j.xml"/>
</Context>

In 4 You just configure your log4j to your needs (below is example of saving log to file)
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">

<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">

<appender name="tofile" class="org.apache.log4j.FileAppender">
  <param name="file" value="C:\\myCozyProject.log"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{ABSOLUTE} %5p %c{1}:%L - %m%n"/>
</layout>
</appender>

<root>
<priority value ="info" />
<appender-ref ref="tofile" />
</root>

</log4j:configuration>


 btw. If you want log4j.xml inside project, then you can use my example or check this blog :
http://th1rty7.blogspot.com/2009/03/configure-logging-in-spring-with-log4j.html

25 March 2011

log4j.properties log to file example

log4j.appender.save2file=org.apache.log4j.FileAppender
log4j.appender.save2file.layout=org.apache.log4j.PatternLayout
log4j.appender.save2file.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n
log4j.appender.save2file.File=c:\\newsEditor.log


in line  log4j.rootLogger=debug , save2file
debug you can replace to any log4j levels
 save2file is name of appender which you can change to any.

log4j.properties log to console example

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{ABSOLUTE} %5p %c{1}:%L - %m%n

log4j.rootLogger=debug , stdout

in line  log4j.rootLogger=debug , stdout
debug you can replace to any log4j levels
 stdout is name of appender which you can change to any.

how to add log4j.properties to classpath in Eclipse (For java aplication)?

Fancy log4j ?
 Good,because it is cool stuff....
 However ...but when you get started, then everything looks simply and go smoothly until you trying to run application , then  ... you see.

log4j:WARN No appenders could be found for logger (org.my.amazing.application).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.

 No good, then question is Set I everything correctly ?

Below you have receipe how to setup log4j correctly to be DAMN sure, that it will work.

1) add log4j.jar to lib (or any place where you store your libraries

2) and add log4j.properties to src folder in Java Resources

and btw. make sure that  configure log4j.properties correctly.

how to add log4j.properties to classpath in Netbeans (For java aplication)?

Fancy log4j ?
 Good,because it is cool stuff....
 However ...but when you get started, then everything looks simply and go smoothly until you trying to run application , then  ... you see.

log4j:WARN No appenders could be found for logger (org.my.amazing.application).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.

 No good, then question is Set I everything correctly ?

Below you have receipe how to setup log4j correctly to be DAMN sure, that it will work.

1) add log4j.jar to lib (or any place where you store your libraries

2) and add log4j.properties to <default package> in source Packages.

and btw. make sure that  configure log4j.properties correctly.