logging.fileConfig limitations?

M

Mani Ghasemlou

Hi all,

I have an application that writes out its logs in a subfolder of the
user's local data directory. Let's say our user's name is "ßéäöÜ2".

On my Windows XP machine, this logging path turns out to be:

C:\Documents and Settings\ßéäöÜ2\Local Settings\Application Data\MyApp\MyApp.log

My application has a "bootstrap" kind of application that sets up the
application's subfolder and writes out the log config file. Notice
that the user's name contains non-ASCII characters. So, I encode the
path in UTF8 format before writing the log config file. My resulting
log config is (the last line is the most important):

----- BEGIN
[formatters]
keys: normal

[handlers]
keys: rotatingfile

[loggers]
keys: root

[formatter_normal]
format: %(asctime)s %(levelname)s %(module)s: %(message)s

[logger_root]
level: DEBUG
handlers: rotatingfile

[handler_rotatingfile]
class: handlers.RotatingFileHandler
formatter: normal
args: ["C:/Documents and Settings/ßéäöÜ2/Local
Settings/Application Data/MyApp/MyApp.log", "a", 2*1024*1024, 5, None]
----- END

Now it turns out that the logging module can't find "C:/Documents and
Settings/ßéäöÜ2/Local Settings/Application Data/MyApp/MyApp.log"
specified in the "args" section, and rightfully so because this is an
encoded string. *There does not seem to be a way for me to specify the
encoding of the string so that the logging module resolves the proper
unicode path.* This is my key problem!

Is there some way to accomplish what I want?

One interesting observation: I did some poking around in the logging
module shipped with Python 2.5. I put in some code in the Python
logging module to force UTF8 decoding of the "filename" argument, the
result of which was interesting: it would end up logging in
"C:\Documents and Settings\ßéäöÜ2\Local Settings\Application
Data\MyApp\MyApp.txt" (not MyApp.log !). I've done a little debugging
and can't immediately figure out why this is happening.

Any help would be greatly appreciated!

Regards,
Mani
 
V

Vinay Sajip

Hi all,

C:\Documents and Settings\ßéäöÜ2\Local Settings\Application Data\MyApp\MyApp.log

Now it turns out that the logging module can't find "C:/Documents and
Settings/ßéäöÜ2/Local Settings/Application Data/MyApp/MyApp.log"
specified in the "args" section, and rightfully so because this is an
encoded string. *There does not seem to be a way for me to specify the
encoding of the string so that the logging module resolves the proper
unicode path.* This is my key problem!

Is there some way to accomplish what I want?

No need to poke about in the source. Here's my script:

#-- log_ufn.py --
import logging.config

logging.config.fileConfig("log_ufn.ini")
logging.error("Holy Cow, Batman!")

and here's my configuration file, adapted from yours:

#-- log_ufn.ini --
[formatters]
keys: normal

[handlers]
keys: rotatingfile

[loggers]
keys: root

[formatter_normal]
format: %(asctime)s %(levelname)s %(module)s: %(message)s

[logger_root]
level: DEBUG
handlers: rotatingfile

[handler_rotatingfile]
class: handlers.RotatingFileHandler
formatter: normal
args: (u"C:/Temp/\u00DF\u00E9\u00E4\u00F6\u00DC2/MyApp.log", "a",
2*1024*1024, 5, None)

Note that I specified a Unicode filename, with Unicode escapes for
ßéäöÜ2.

After I run the script (using ActivePython 2.5.2.2), I get the
following output:

# -- Contents of C:\temp\ßéäöÜ2\MyApp.log --
2009-06-18 17:07:30,493 ERROR log_ufn: Holy Cow, Batman!

Which seems to be what one would expect.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,994
Messages
2,570,222
Members
46,809
Latest member
moe77

Latest Threads

Top