File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Other Open Source Projects and the fly likes Separate log file Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Products » Other Open Source Projects
Bookmark "Separate log file " Watch "Separate log file " New topic
Author

Separate log file

kayanaat sidiqui
Ranch Hand

Joined: Sep 04, 2008
Posts: 122
How to create a separate log file that contains [size=18]only[/size] info level messages?
Please help me out.

Martijn Verburg
author
Bartender

Joined: Jun 24, 2003
Posts: 3274
    
    5

You need to create a separate appender in your log4j configuration file and set the threshold on that to INFO.


Cheers, Martijn - Blog,
Twitter, PCGen, Ikasan, My The Well-Grounded Java Developer book!,
My start-up.
kayanaat sidiqui
Ranch Hand

Joined: Sep 04, 2008
Posts: 122
Hi Martijn Verburg,
Thanks for your quick reply.

I did the same as you said in your post, but i setting theshod as info level i am getting info, warn, error level messages also.

My concern is to keep ONLY INFO LEVEL MESSAGES in a separate file.

If i am missing somethiong then please correct me.

Thank you.
Martijn Verburg
author
Bartender

Joined: Jun 24, 2003
Posts: 3274
    
    5

Oh I see, I found this example on the net which does something similar:

kayanaat sidiqui
Ranch Hand

Joined: Sep 04, 2008
Posts: 122
Hi Martijn Verburg,
Thanks for your nice response.

I used this code snippet in log4j.properties-

log4j.rootLogger=WARN, CONSOLE, WARN_FILE , ERROR_FILE
log4j.logger.com.elasticpath=INFO

log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender
log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout
log4j.appender.CONSOLE.layout.ConversionPattern=%-5p %d{ISO8601} %l - %m%n

# specify the location of the log file relative to the web application root
# or use an absolute path as show below.
log4j.appender.WARN_FILE=org.apache.log4j.RollingFileAppender
log4j.appender.WARN_FILE.File=log/ImportExport-WARN.log
log4j.appender.WARN_FILE.MaxFileSize=10MB
log4j.appender.WARN_FILE.MaxBackupIndex=1
log4j.appender.WARN_FILE.layout=org.apache.log4j.TTCCLayout
log4j.appender.WARN_FILE.filter=org.apache.log4j.varia.LevelRangeFilter
log4j.appender.WARN_FILE.filter.LogLevelMin=WARN
log4j.appender.WARN_FILE.filter.LogLevelMax=WARN

log4j.appender.WARN_FILE.filter.AcceptOnMatch=TRUE
log4j.appender.WARN_FILE.layout.DateFormat=ISO8601

But i am getting this error-
log4j:WARN Failed to set property [filter] to value "org.apache.log4j.varia.LevelRangeFilter".
Please help me.
Martijn Verburg
author
Bartender

Joined: Jun 24, 2003
Posts: 3274
    
    5

I don't think filters are supported in log4j.properties files. You need to use the log4j.xml style configuration file.
kayanaat sidiqui
Ranch Hand

Joined: Sep 04, 2008
Posts: 122
I tried with the following log4j.xml--

<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">

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

<appender name="CONSOLE" class="org.apache.log4j.ConsoleAppender">
<layout class="org.apache.log4j.PatternLayout Source code of org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%-5p %d{ISO8601} %l - %m%n" />
</layout>
</appender>

<appender name="WARN_FILE" class="org.apache.log4j.RollingFileAppender Source code of org.apache.log4j.RollingFileAppender">
<param name="File" value="log/ImportExport-WARN.log"/>
<param name="MaxFileSize" value="10MB"/>
<param name="MaxBackupIndex" value="5"/>
<layout class="org.apache.log4j.TTCCLayout Source code of org.apache.log4j.TTCCLayout ">
<param name="ConversionPattern" value="%-5p %d{ISO8601} %l - %m%n" />
<param name="DateFormat" value="ISO8601" />
</layout>
<filter class="org.apache.log4j.varia.LevelRangeFilter Source code of org.apache.log4j.varia.LevelRangeFilter">
<param name="LevelMin" value="warn" />
<param name="LevelMax" value="warn" />
<param name="AcceptOnMatch" value="true" />
</filter>
</appender>

<appender name="ERROR_FILE" class="org.apache.log4j.RollingFileAppender Source code of org.apache.log4j.RollingFileAppender">
<param name="File" value="log/ImportExport-ERROR.log"/>
<param name="MaxFileSize" value="10MB"/>
<param name="MaxBackupIndex" value="5"/>
<layout class="org.apache.log4j.TTCCLayout Source code of org.apache.log4j.TTCCLayout ">
<param name="ConversionPattern" value="%-5p %d{ISO8601} %l - %m%n" />
<param name="DateFormat" value="ISO8601" />
</layout>
<filter class="org.apache.log4j.varia.LevelRangeFilter Source code of org.apache.log4j.varia.LevelRangeFilter">
<param name="LevelMin" value="error" />
<param name="LevelMax" value="error" />
<param name="AcceptOnMatch" value="true" />
</filter>
</appender>

<root>
<level value="debug" />
<appender-ref ref="CONSOLE" />
<appender-ref ref="WARN_FILE" />
<appender-ref ref="ERROR_FILE" />
</root>

</log4j:configuration>

also, i put the log4j.dtd in the same folder.

but getting these error-

log4j:ERROR Could not create the Layout. Reported error follows.
java.lang.ClassNotFoundException: org.apache.log4j.PatternLayout Source code of org.apache.log4j.PatternLayout
at java.net.URLClassLoader$1.run(URLClassLoader.java:200)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:268)
at java.lang.ClassLoader.loadClass(ClassLoader.java:251)
at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:319)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:164)
at org.apache.log4j.helpers.Loader.loadClass(Loader.java:178)
at org.apache.log4j.xml.DOMConfigurator.parseLayout(DOMConfigurator.java:455)
at org.apache.log4j.xml.DOMConfigurator.parseAppender(DOMConfigurator.java:196)
at org.apache.log4j.xml.DOMConfigurator.findAppenderByName(DOMConfigurator.java:150)
at org.apache.log4j.xml.DOMConfigurator.findAppenderByReference(DOMConfigurator.java:163)
at org.apache.log4j.xml.DOMConfigurator.parseChildrenOfLoggerElement(DOMConfigurator.java:425)
at org.apache.log4j.xml.DOMConfigurator.parseRoot(DOMConfigurator.java:394)
at org.apache.log4j.xml.DOMConfigurator.parse(DOMConfigurator.java:829)
at org.apache.log4j.xml.DOMConfigurator.doConfigure(DOMConfigurator.java:712)
at org.apache.log4j.xml.DOMConfigurator.doConfigure(DOMConfigurator.java:618)
at org.apache.log4j.helpers.OptionConverter.selectAndConfigure(OptionConverter.java:468)
at org.apache.log4j.LogManager.<clinit>(LogManager.java:122)
at org.apache.log4j.Logger.getLogger(Logger.java:117)
at com.symantec.importexport.processor.impl.SymProductProcessor.<clinit>(SymProductProcessor.java:57)
log4j:ERROR Could not create an Appender. Reported error follows.
java.lang.ClassNotFoundException: org.apache.log4j.RollingFileAppender Source code of org.apache.log4j.RollingFileAppender
at java.net.URLClassLoader$1.run(URLClassLoader.java:200)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:268)
at java.lang.ClassLoader.loadClass(ClassLoader.java:251)
at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:319)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:164)
at org.apache.log4j.helpers.Loader.loadClass(Loader.java:178)
at org.apache.log4j.xml.DOMConfigurator.parseAppender(DOMConfigurator.java:174)
at org.apache.log4j.xml.DOMConfigurator.findAppenderByName(DOMConfigurator.java:150)
at org.apache.log4j.xml.DOMConfigurator.findAppenderByReference(DOMConfigurator.java:163)
at org.apache.log4j.xml.DOMConfigurator.parseChildrenOfLoggerElement(DOMConfigurator.java:425)
at org.apache.log4j.xml.DOMConfigurator.parseRoot(DOMConfigurator.java:394)
at org.apache.log4j.xml.DOMConfigurator.parse(DOMConfigurator.java:829)
at org.apache.log4j.xml.DOMConfigurator.doConfigure(DOMConfigurator.java:712)
at org.apache.log4j.xml.DOMConfigurator.doConfigure(DOMConfigurator.java:618)
at org.apache.log4j.helpers.OptionConverter.selectAndConfigure(OptionConverter.java:468)
at org.apache.log4j.LogManager.<clinit>(LogManager.java:122)
at org.apache.log4j.Logger.getLogger(Logger.java:117)
at com.symantec.importexport.processor.impl.SymProductProcessor.<clinit>(SymProductProcessor.java:57)
log4j:ERROR Could not parse url [file:/D:/6f/depot/Framework/SymEp/Branches/Train1_Docking/importexport/bin/main/log4j.xml].
java.lang.NullPointerException
at java.util.Hashtable.put(Hashtable.java:396)
at org.apache.log4j.xml.DOMConfigurator.findAppenderByName(DOMConfigurator.java:151)
at org.apache.log4j.xml.DOMConfigurator.findAppenderByReference(DOMConfigurator.java:163)
at org.apache.log4j.xml.DOMConfigurator.parseChildrenOfLoggerElement(DOMConfigurator.java:425)
at org.apache.log4j.xml.DOMConfigurator.parseRoot(DOMConfigurator.java:394)
at org.apache.log4j.xml.DOMConfigurator.parse(DOMConfigurator.java:829)
at org.apache.log4j.xml.DOMConfigurator.doConfigure(DOMConfigurator.java:712)
at org.apache.log4j.xml.DOMConfigurator.doConfigure(DOMConfigurator.java:618)
at org.apache.log4j.helpers.OptionConverter.selectAndConfigure(OptionConverter.java:468)
at org.apache.log4j.LogManager.<clinit>(LogManager.java:122)
at org.apache.log4j.Logger.getLogger(Logger.java:117)
at com.symantec.importexport.processor.impl.SymProductProcessor.<clinit>(SymProductProcessor.java:57)
log4j:ERROR No layout set for the appender named [CONSOLE].

can you see it?
Martijn Verburg
author
Bartender

Joined: Jun 24, 2003
Posts: 3274
    
    5

Yep, you've got the string "Source code of org.apache.log4j.PatternLayout" accidentally copied into your configuration
kayanaat sidiqui
Ranch Hand

Joined: Sep 04, 2008
Posts: 122
Thank you Martijn Verburg.
Now I am able to run my code.

Well I am trying another thing.
What i want, whenever a particular code runs, each time a new log file
must generate and the name of file must contain timestamp of the file generation.

can you please help me.
Martijn Verburg
author
Bartender

Joined: Jun 24, 2003
Posts: 3274
    
    5

Not sure how you would do that, I'm afraid, anyone else?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Separate log file