#format wiki #language en #pragma section-numbers off = log4j Example = OtherExamples <> ---- This example require several things. * log4j on the classpath * log4j.properties (below) in the same directory as the example * example.xml (below) in the same directory as the example * the example below * And of course Jyhton 2.2 This was tested with Jython 2.2 and Java JVM 1.5 and 1.4.2 ==== log4j Example ==== This is a simple example to show how easy it is to use log4j in your own scripts. The source is well documented but if you have any questions or want to more info use your favorite search engine and type in log4j. {{{#!python from org.apache.log4j import * class logtest: def __init__(self): log.info("start of Logtest") log.debug('just before file read') try: log.warn('file read proceding to processing') xmlStringData = open('example.xml').read() except: #yes, more could have been done here but this is just an example log.error('file read FAILURE') log.info('file read proceding to processing') # since this is just an example processing would go here. # #you can also log variables log.warn('its just an example, OK?') pi = 3.141592681 msg = 'do you like?' + str(pi) log.info(msg) log.debug('lets try to parse the string') if '[CDATA' in xmlStringData: log.warn('No CDATA section.') #say good bye and close the log file. log.info('That all. The End. Good Bye') log.shutdown() if __name__ == '__main__': # loggingTest is just a string that identifies this log. log = Logger.getLogger("loggingTest") #use the config data in the properties file PropertyConfigurator.configure('log4j.properties') log.info('This is the start of the log file') logit = logtest() print '\n\nif you change the log level in the properties' print "file you'll get varing amouts of log data." }}} ==== log4j.properties ==== This file is required by the code above. it need to be in the same directory as the example however It can be anywhere as log as you provide a full path to the file. it configures how log4j operates. If it is not found it defaults to a default logging level. Since this is for example purposes the file below is larger then really needed. {{{ #define loging level and output log4j.rootLogger=debug, stdout, LOGFILE #log4j.rootLogger=info, LOGFILE # this 2 lines tie the apache logging into log4j #log4j.logger.org.apache.axis.SOAPPart=DEBUG #log4j.logger.httpclient.wire.header=info #log4j.logger.org.apache.commons.httpclient=DEBUG # where is the logging going. # This is for std out and defines the log output format log4j.appender.stdout=org.apache.log4j.ConsoleAppender log4j.appender.stdout.layout=org.apache.log4j.PatternLayout log4j.appender.stdout.layout.ConversionPattern=%d{HH:mm:ss,SSS} | %p | [%c] %m%n %t #log it to a file as well. and define a filename, max file size and number of backups log4j.appender.LOGFILE=org.apache.log4j.RollingFileAppender log4j.appender.LOGFILE.File=jythonTest.log log4j.appender.LOGFILE.MaxFileSize=100KB # Keep one backup file log4j.appender.LOGFILE.MaxBackupIndex=1 log4j.appender.LOGFILE.layout=org.apache.log4j.PatternLayout # Pattern for logfile - only diff is that date is added log4j.appender.LOGFILE.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} | %p | [%c] %m%n # Other Examples: only time, loglog level, loggerName #log4j.appender.LOGFILE.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss},%p,%c %m%n #above plus filename, linenumber, Class Name, method name #log4j.appender.LOGFILE.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss},%p,%c,%F,%L,%C{1},%M %m%n }}} ==== Example xml file ==== This is here for completeness. Any text file could be use with the example above by changing the 'open' line in the above line. {{{ W I TU Cathrine Knight 34-5424-77 10/12/1938
4780 Centerville Saint Paul, MN 55127
}}}