RDRichardson at rad-con.com
Thu Dec 13 20:51:46 CET 2012
Thanks for the suggestion, but I don't think that can be it. At least, it's not it on the line you specified. That form, with the target name given as a hard-coded string, is in use in every Python script our company uses. So, set_target() is expecting a file instead of a file name, there must be some implicit conversion going on that creates a file object of the given name before setting the log target to the file.
On the other hand, it would certainly explain the " 'Unicode' object does not have attribute 'write' " message. A file object would be expected to have an attribute named 'write', while a string object wouldn't.
The error happens after the target is changed to a file name contained in a variable. Why doesn't that implicit conversion work with a string contained in a variable? Do I have to do an explicit cast of some kind? Or, probably better, I should probably look up how to create a file object so I can pass it in to the set_target() function.
That's one reason I asked for documentation on log4py: so I could see what methods are available, and what arguments they actually expect.
From: Python-list [mailto:python-list-bounces+rob.richardson=rad-con.com at python.org] On Behalf Of MRAB
Sent: Thursday, December 13, 2012 2:35 PM
To: python-list at python.org
Subject: Re: log4py confusion
On 2012-12-13 18:58, Rob Richardson wrote:
> 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?
I think the problem might be that it's expecting a file object, not a file name/path as you have here:
More information about the Python-list