Re: Help needed on propertyConfigurator
viswa wrote: > > Hi all, > I am looking in to the log4j manual.In the following example where > is he specifing the configuration file. Even in many examples i saw >PropertyConfigurator.configure(args[0]). > But i haven't seen the file name and location of it. Can anybody tell > Where is he specifing the location of the file. > > import com.foo.Bar; > > import org.apache.log4j.Category; > import org.apache.log4j.PropertyConfigurator; > > public class MyApp { > >static Category cat = Category.getInstance(MyApp.class.getName()); > >public static void main(String[] args) { > > > // BasicConfigurator replaced with PropertyConfigurator. > PropertyConfigurator.configure(args[0]); > > cat.info("Entering application."); > Bar bar = new Bar(); > bar.doIt(); > cat.info("Exiting application."); >} > } > args[0] refers to the first command line parameter given to your application. So if your application is 'MyAapp' and configuration info is in 'MyApp.config' then you would bring up the app with java MyApp MyApp.config Cheers Claus - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: Escape sequences
"Klovance, Joe" wrote: > > I need to modify log4j to so that I can use a delimiter between the fields > and be able to replace any occurance of the delimeter in any of the paterns > with an escape sequence. Where in the code would I do that? > > Joe Klovance > If you would only have the first demand, namely putting a delimiter between the fields, you would just use a new pattern, e.g., "%-4r|%t|%a|%h|%-5p|%c%m%n" if you want a "|" as delimiter. The easiest way is to choose a delimiter which is garuanteed not to be in any message. If you really need to parse the message string, a (good?) place for doing this is in the forcedLog(...) methods of class Category. But then you have to subclass from Category and overwrite the forcedLog method like protected void forcedLog(String fqcn, Priority priority, Object message, Throwable t) { // apply your patches here: change for separators, escape them, etc patchMessage( message ); // <--- new callAppenders(new LoggingEvent(fqcn, this, priority, message, t)); } But then you must tell CategoryFactory to produce instances of your new category class. Regarding extending log4j in this way you should have a look at http://jakarta.apache.org/log4j/docs/deepExtension.html Have a closer look at the last sentence in this document: "There is one a word of caution concerning the use of configurators that may create Category instances (such as PropertyConfigurator and DOMConfigurator). Since these configurators do not know about our extensions, any Category instances they create will not be AppServerCategory instances. To prevent this problem, any AppServerCategory that one might want to be configured through a configurator should be instantiated before the configure method is invoked. In this way, the configurator will configure the AppServerCategory that already exists rather than creating an instance of its super class. " Baybe it's not enough just to subclass from Category? Claus Wagner - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: Are we sure log4j is flushing the output for a FileWriter?
Scott Ellsworth wrote: > > Hi, all. > > I have a project in which we have used log4j extensively. Our program logs > a message on start, so there should be something in the log file pretty > much instantly, but it appears that if the program is halted suddenly, the > log file will be completely empty. > > I have tried halting it by closing the Java launcher DOS window, and I note > that I then get an empty log file. This is true even if the program has > been running for five or ten minutes. I am using a plain old FileAppender, > which inherits from WriterAppender, and thus ImmediateFlush should force > the writes to take place within seconds at most. > > If I do not kill the process, then the writes take place either when we > have a few k in the log buffer, or when the process terminates > gracefully. This is normal behavior for buffered output, but I even tried > explicitly setting ImmediateFlush to see if that made a difference. > > NB - there is quite a bit of time where the program sits idle, so I know > that the initial message is not getting swallowed by threading or something > similarly vile. > > I have tried both 1.1.3 and 1.1.2. > > So, can anyone confirm that FileWriter is honoring the immediate flush > requests under Windows and JDK 1.3.1? > > The config file is below. > > log4j.rootCategory=DEBUG, A_log > > log4j.appender.A_log=org.apache.log4j.FileAppender > log4j.appender.A_log.Threshold=DEBUG > log4j.appender.A_log.File=pov.log > log4j.appender.A_log.ImmediateFlush=true > log4j.appender.A_log.Append=false > log4j.appender.A_log.layout=org.apache.log4j.PatternLayout > log4j.appender.A_log.layout.ConversionPattern=%d (%r) %-5p %-30c{2} - %m\n > > log4j.category.com.isisph.bioinfo.general.POV=DEBUG > Scott Ellsworth > [EMAIL PROTECTED] > Scott: I observed a similar behaviour but with a different appender (a custom RMI appender). It had to deregister with rmiregistry on shutdown. A managed this for proper exiting of the application. However, this didn't work when, for example, the application got killed via Ctrl-C or something similar. My solution to this was adding a shudown hook to my application. Registering the shutdown hook was like // allocate thread which runs when the Java vm gets killed. allocateCleanupThread(); // register that thread with the runtime Runtime runtime = java.lang.Runtime.getRuntime(); runtime.addShutdownHook( cleanupThread ); Where's the best place to put this? The main() or the constructor of the class holding the resource? I have an attribute as follows: CleanupThread cleanupThread; which is defined as follows (as an inner class): class CleanupThread extends Thread { public void run() { cleanupResources(); } } The method allocateCleanupThread() may look as follows: public void allocateCleanupThread () { if( cleanupThread == null ) { cleanupThread = new CleanupThread(); //System.out.println( "cleanupThread has been created." ); } } As said above, I used it to deregister from rmiregistry on sudden Java virtual machine shutdown via Ctrl-C. But maybe you can use such a thing also to flush streams or do other resource cleanups. HTH Regards Claus Wagner - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: How to change the type of logging without restarting the application
Sajid Shaikh wrote: > > Is there a way to change the type of logging without restarting the > application. We are using a property file to specify the categories and > priorities when the application starts. If we change this property file is > there a way that log4j will automatically pick up the new property settings > without restarting the application ? > > Thanks for help in advance. > Sajid. Sajid: PropertyConfigurator.configureAndWatch(...) does the trick. Cheers Claus - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: Log a localized and parameterized message
Veerappan Saravanan wrote: > > Category has a method called l7dlog to help with parameterization and > localization. But it doesn't quite do what I would like it to do. > > If I have a resource bundle that has the following entry > > ERROR_101=some error in the system > > then if I call > > l7dlog(Priority.ERROR, ERROR_101, null); > > the output would be > > "some error in the system" > > instead I would like it to print the error code along with the message, > something like > > "ERROR_101: some error in the system" > > I know of two solutions to this... > 1. change the resource bundle entry to > ERROR_101=ERROR_101:some error in the system > but, this is erorr prone, since developers could always enter the wrong id > in the message. > > 2. Hack the Category class' l7dlog method to do customize it. > In this case, every upgrade to log4j, I need to change my Category class, > which could be a pain. > > Are there any other solutions to this? > > Help appreciated. > > Thanks > Van > There is another possibility to achieve this (although it is not the proper place to do it): override protected void forcedLog(), which is called from l7dlog, and manipulate your message string therein: /** This method creates a new logging event and logs the event without further checks. */ protected void forcedLog(String fqcn, Priority priority, Object message, Throwable t) { // message is "some error in the system" // have a getKey() method // squeeze the key in front of the original message // or make a new string --> new_message callAppenders(new LoggingEvent(fqcn, this, priority, new_message, t)); } HTH Claus - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: Log a localized and parameterized message
Veerappan Saravanan wrote: > > Category has a method called l7dlog to help with parameterization and > localization. But it doesn't quite do what I would like it to do. > > If I have a resource bundle that has the following entry > > ERROR_101=some error in the system > > then if I call > > l7dlog(Priority.ERROR, ERROR_101, null); > > the output would be > > "some error in the system" > > instead I would like it to print the error code along with the message, > something like > > "ERROR_101: some error in the system" > > I know of two solutions to this... > 1. change the resource bundle entry to > ERROR_101=ERROR_101:some error in the system > but, this is erorr prone, since developers could always enter the wrong id > in the message. > > 2. Hack the Category class' l7dlog method to do customize it. > In this case, every upgrade to log4j, I need to change my Category class, > which could be a pain. > > Are there any other solutions to this? > > Help appreciated. > > Thanks > Van > Hi Van and list, I need this, too. Ceki: is it possible, e.g., to outsource the message building part of the l7dlog methods into a separate method? Something similar to public String buildMessage () { String msg = getResourceBundleString(key); // if message corresponding to 'key' could not be found in the // resource bundle, then default to 'key'. if(msg == null) { msg = key; } return msg; } Users are then able to override and perform some more exotic string handling. Regards Claus - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: different appenders for different levels of severity
Ceki Gülcü wrote: > > Claus, > > They are there. Please see under >$LOG4J_HOME/src/java/org/apache/log4j/xml/test/test11.xml > > Regards, Ceki > Ceki: Ooh, I should have found them before sending the mail Anyway, I assembled a config file like this, which is not working: --- # # set root category priority to DEBUG, # it supports only the console per default log4j.rootCategory=DEBUG, consoleApp # # we will use two appenders (destinations): the console (default) # and 2 log files # consoleApp is set to be a ConsoleAppender, fileApps go to a file log4j.appender.consoleApp=org.apache.log4j.ConsoleAppender log4j.appender.debugfileApp=org.apache.log4j.RollingFileAppender # name of the logfile log4j.appender.debugfileApp.File=debug.log # each log file will have a maximum size of 100 kB log4j.appender.debugfileApp.MaxFileSize=100KB # keep 5 backup files log4j.appender.debugfileApp.MaxBackupIndex=5 log4j.appender.warnfileApp=org.apache.log4j.RollingFileAppender # name of the logfile log4j.appender.warnfileApp.File=warn.log # each log file will have a maximum size of 100 kB log4j.appender.warnfileApp.MaxFileSize=100KB # keep 5 backup files log4j.appender.warnfileApp.MaxBackupIndex=5 # # consoleApp uses PatternLayout, fileApps too. # consoleApp uses the short timestamp: milliseconds since startup log4j.appender.consoleApp.layout=org.apache.log4j.PatternLayout log4j.appender.consoleApp.layout.ConversionPattern=%-4r [%t] %-5p %c %x - %m%n # print the date in ISO 8601 format for the debug file log4j.appender.debugfileApp.layout=org.apache.log4j.PatternLayout log4j.appender.debugfileApp.layout.ConversionPattern=%d [%t] %-5p %c - %m%n # print the date in ISO 8601 format for the warn file log4j.appender.warnfileApp.layout=org.apache.log4j.PatternLayout log4j.appender.warnfileApp.layout.ConversionPattern=%d [%t] %-5p %c - %m%n # print only messages of priority DEBUG only in the package com.foo log4j.appender.debugfileApp.filter=org.apache.log4j.varia.PriorityMatchFilter log4j.appender.debugfileApp.filter.PriorityToMatch=DEBUG log4j.appender.debugFileApp.filter.AcceptOnMatch=true log4j.category.com.foo=DEBUG, debugfileApp # let log4j log itself log4j.debug=true --- The log4j output on parsing the config file is log4j: Parsing for [root] with value=[DEBUG, consoleApp]. log4j: Priority token is [DEBUG]. log4j: Category root set to DEBUG log4j: Parsing appender named "consoleApp". log4j: Parsing layout options for "consoleApp". log4j: Setting property [conversionPattern] to [%-4r [%t] %-5p %c %x - %m%n]. log4j: End of parsing for "consoleApp". log4j: Parsed "consoleApp" options. log4j: Parsing for [com.foo] with value=[DEBUG, debugfileApp]. log4j: Priority token is [DEBUG]. log4j: Category com.foo set to DEBUG log4j: Parsing appender named "debugfileApp". log4j: Parsing layout options for "debugfileApp". log4j: Setting property [conversionPattern] to [%d [%t] %-5p %c - %m%n]. log4j: End of parsing for "debugfileApp". log4j: Setting property [maxFileSize] to [100KB]. log4j: Setting property [maxBackupIndex] to [5]. log4j:WARN Failed to set property filter to value "org.apache.log4j.varia.PriorityMatchFilter". No setter for property log4j: Setting property [file] to [debug.log]. log4j: Parsed "debugfileApp" options. log4j: Handling log4j.additivity.com.foo=[null] log4j: Finished configuring. File "debug.log' has been created but log4j loggs both levels DEBUG and WARN into it. If I disable the package related line #log4j.category.com.foo=DEBUG, debugfileApp there are no warnings on startup (of course) but no logfile either (of course), since I didn't connect a category with an appender. The syntax of the filter settings seem to be ok. What's wrong with the above config file? Any help is welcome! Regards Claus - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
different appenders for different levels of severity
Hi, I want to use the same category in the same class to log to different appenders. All logs with severity _only_ DEBUG should go to a file debug.log, and all logs with severity _only_ WARN should go to file warn.log, and so on. The FAQ says that attaching a PriorityMatchFilter to an appender should do the trick. However, the example configuration files test11.xml and test12.xml from the javadoc was not found by my browser. Can anybody give me a hint of how to log exact priority matches into defined appenders? Any code snipped of a (non-XML) configuration file is welcome! Thanks in advance Claus Wagner - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
isEnabledFor() not working?
Hi list, two questions: 1) there is only isDebugEnabled() and isInfoEnabled() present. Is this meant to be like so? Are WARN, ERROR, and FATAL not meant to be checked prior to the corresponding logging method call? 2) when I then try to isEnabledFor( Priority.ERROR ), javac is not able to resolve symbol Priority. After importing org.apache.log4j.Priority, javac is not able to resolve isEnabledFor(), though org.apache.log4j.Category has been imported! Any advice is appreciated! Regards Claus Wagner - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]