Differences between revisions 8 and 9
Revision 8 as of 2006-08-04 17:25:04
Size: 4231
Editor: JoshJuneau
Revision 9 as of 2008-11-15 09:16:01
Size: 4233
Editor: localhost
Comment: converted to 1.6 markup
Deletions are marked like this. Additions are marked like this.
Line 82: Line 82:
Check it out at: [https://simple-log.dev.java.net/] Check it out at: [[https://simple-log.dev.java.net/]]

Add Logging to Jython Using Log4J

Submitted by: Josh Juneau

Are you still using the Jython print command to show your errors? How about in a production environment, are you using any formal logging? If not, you should be doing so...and the Apache log4j API makes it easy to do so. Many Java developers have grown to love the log4j API and it is utilized throughout much of the community. That is great news for Jython developers since we've got direct access to Java libraries!

Setting Up Your Environment

The most difficult part about using log4j with Jython is the setup. You must ensure that the log4j.jar archive resides somewhere within your Jython PATH (usually this entails setting the CLASSPATH to include necessary files in Windows environment). You then set up a properties file for use with log4j. Within the properties file, you can include appender information, where logs should reside, and much more.

Example properties file:

log4j.rootLogger=debug, stdout, R


# Pattern to output the caller's file name and line number.
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] (%F:%L) - %m%n


# Keep one backup file

log4j.appender.R.layout.ConversionPattern=%p %t %c - %m%n 

You are now ready to use log4j in your Jython application. As you can see, if you've ever used log4j with Java, it is pretty much the same.

Using log4j in a Jython Application

Once again, using log4j within a Jython application is very similar to it's usage in the Java world.

First, you must import the log4j packages:

from org.apache.log4j import * 

Second, you obtain a new logger for your class or module and set up a PropertyConfigurator:

 Logger logger = Logger.getLogger("myClass")
  # Assume that the log4j properties resides within a folder named "utilities"
  PropertyConfigurator.configure(sys.path[0] + "/utilities/log4j.properties")

Lastly, use log4j:

  # Example module within the class:
   def submitDocument(self, event):
           # Assume we perform some SQL here              
       except SQLException, ex:
           self.logger.error("docPanel#submitDocument ERROR: %s" % (ex)) 

Your logging will now take place within the file you specified in the properties file for log4j.appender.R.File.

Using log4j in Jython Scripts

Many may ask, why in the world would you be interested in logging information about your scripts? Most of the time a script is executed interactively via the command line. However, there are plenty of instances where it makes sense to have the system invoke a script for you. As you probably know, this technique is used quite often within an environment to run nightly tasks, or even daily tasks which are automatically invoked on a scheduled basis. For these cases, it can be extremely useful to log errors or information using log4j. Some may even wish to create a separate automated task to email these log files after the tasks complete.

The overall implementation is the same as above, the most important thing to remember is that you must have the log4j.jar archive and properties file within your Jython path. Once this is ready to go you can use log4j in your script.

 from org.apache.log4j import *
 logger = Logger.getLogger("scriptname")
 logger.info("Test the logging")

Future Possibilities

Log4J is nice, but there are other logging frameworks which could be extremely useful in Jython as well. As a matter of fact, there is a new logging "anti-framework" which is called SimpleLog 2.0 and it looks helpful. I haven't yet reviewed SimpleLog 2.0, but I plan to do so in the near future!

Check it out at: https://simple-log.dev.java.net/

JythonMonthly/Articles/August2006/1 (last edited 2008-11-15 09:16:01 by localhost)