log4py confusion
Rob Richardson
RDRichardson at rad-con.com
Thu Dec 13 14:20:54 EST 2012
The last line,
programSettings.Log(TempLogger),
should have been
programSettings.Log(Logger)
I get the same error no matter which logger I use, though, which I don't understand.
Also, I'm using Python 2.7.
Thanks again for your help!
RobR
From: Python-list [mailto:python-list-bounces+rob.richardson=rad-con.com at python.org] On Behalf Of Rob Richardson
Sent: Thursday, December 13, 2012 1:58 PM
To: python-list at python.org
Subject: log4py confusion
Greetings!
I have finally gotten fed up with all of the Python scripts used in my company that have every parameter hard-coded. I am rewriting one of them to read all of its parameters from an XML file. Then, we can change the XML file to control which database it is using, whether its running in debug mode or not, and various other things. One of the parameters I want the XML file to contain is the name of the script's log file.
I have a class named Settings that will contain all the parameters read from the XML file. But if something goes wrong, I want to log it. So, I want to have a Logger object for it to use. I use a hard-coded file name for that. Then, after reading the XML file, I want to create a second Logger object that will be used by the rest of the script.
When I try to use the second Logger object, I get an exception complaining that 'unicode' object has no attribute 'write'.
The script and the exception messages are below. Can anyone see what I'm doing wrong?
Can anyone point me to a web site documenting the log4py module?
Thank you very much.
RobR
import os
import sys
import string
import win32api
import win32event
import win32com
import win32com.client
import win32net
import pythoncom
import time
import log4py
import xml.dom.minidom as minidom
import argparse
#Set to 1 to run loop once for debugging, set to 0 for operation
#Debug = 1
class DataElement:
value = None
def __init__(self, element = None):
if element != None:
self.GetData(element)
def GetData(self, element):
nodes = element.childNodes
for node in nodes:
if node.nodeType == node.TEXT_NODE:
self.value = node.data
def GetDataFromRoot(self, rootElement, targetName, defaultValue):
elements = rootElement.getElementsByTagName(targetName)
if elements:
dataElement = DataElement(elements[0])
if (dataElement):
self.value = dataElement.value
else:
self.value = defaultValue
else:
self.Value = defaultValue
def __nonzero__(self):
return self.value != None
class Settings:
logFile = "TaskToCrane_somethingnew.log"
def __init__(self):
print "I don't know what XML file to use yet."
def __init__(self, fileName, tempLogger):
tempLogger.info( "Settings will come from " + fileName)
settingsDoc = minidom.parse(fileName)
rootElement = settingsDoc.documentElement
# print "Root element name: " + rootElement.tagName
programControlElement = rootElement.getElementsByTagName("ProgramControl")[0]
logFileElement = DataElement()
logFileElement.GetDataFromRoot(programControlElement, "LogFile", self.logFile)
if logFileElement:
self.logFile = logFileElement.value
else:
tempLogger.error("Failed to get log file name.")
def Log(self, logger):
logger.info("Log file: " + str(self.logFile))
if __name__ == "__main__" :
TempLogger = log4py.Logger().get_instance("main")
TempLogger.set_rotation(log4py.ROTATE_DAILY, 5)
TempLogger.set_loglevel(log4py.LOGLEVEL_DEBUG)
TempLogger.set_formatstring("%T - %L %M")
TempLogger.set_target("TaskToCrane_logging_base.log")
parser = argparse.ArgumentParser()
parser.add_argument("settings_file")
args = parser.parse_args()
programSettings = Settings(args.settings_file, TempLogger)
TempLogger.info("Setting actual log target to " + programSettings.logFile)
Logger = log4py.Logger().get_instance("main")
Logger.set_rotation(log4py.ROTATE_DAILY, 5)
Logger.set_loglevel(log4py.LOGLEVEL_DEBUG)
Logger.set_formatstring("%T - %L %M")
Logger.set_target(programSettings.logFile)
programSettings.Log(TempLogger)
Traceback (most recent call last):
File "D:\Python27\Lib\site-packages\pythonwin\pywin\framework\scriptutils.py", line 322, in RunScript
debugger.run(codeObject, __main__.__dict__, start_stepping=0)
File "D:\Python27\Lib\site-packages\pythonwin\pywin\debugger\__init__.py", line 60, in run
_GetCurrentDebugger().run(cmd, globals,locals, start_stepping)
File "D:\Python27\Lib\site-packages\pythonwin\pywin\debugger\debugger.py", line 655, in run
exec cmd in globals, locals
File "D:\Program Files\WinCaps\Scripts\TaskToCrane_logging.py", line 91, in <module>
programSettings.Log(TempLogger)
File "D:\Program Files\WinCaps\Scripts\TaskToCrane_logging.py", line 66, in Log
logger.info("Log file: " + str(self.logFile))
File "log4py.py", line 359, in info
self.__Logger_showmessage(message, MSG_INFO)
File "log4py.py", line 534, in __Logger_showmessage
target.write("%s\n" % line)
AttributeError: 'unicode' object h
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20121213/afacc52b/attachment.html>
More information about the Python-list
mailing list