How to package a logging.config file?

M

Matthew Wilson

I'm working on a package that uses the standard library logging module
along with a .cfg file.

In my code, I use
logging.config.fileConfig('/home/matt/mypackage/matt.cfg') to load in
the logging config file.

However, it seems really obvious to me that this won't work when I share
this package with others.

I can't figure out what path to use when I load my .cfg file.

Any ideas?

Matt
 
V

Vinay Sajip

I'm working on a package that uses the standard libraryloggingmodule
along with a .cfg file.

In my code, I uselogging.config.fileConfig('/home/matt/mypackage/matt.cfg') to load in
theloggingconfig file.

However, it seems really obvious to me that this won't work when I share
this package with others.

I can't figure out what path to use when I load my .cfg file.

Any ideas?

Matt

Is your package a library or an application? If it's a library, you
should avoid configuring logging using a config file - this is because
logging configuration is process-wide, and if multiple libraries use
fileConfig to configure their logging, you may get unexpected results.

If it's an application, then Larry's advice is good.

Regards,


Vinay Sajip
 
Y

Younger Wang

I had the similar question and my solution is:

default_config = os.path.join(os.getcwd(), "log.config")

def get_logger(name, config_file=None):
if config_file:
logging.config.fileConfig(config_file)
else:
logging.basicConfig(level=logging.INFO,
format='%(levelname)s %(module)s:%(lineno)d:
%(message)s')

return logging.getLogger(name)

I had to make this helper function because calling logging.getLogger
will fail in an exception without configuring logging module. I wish it
is not like that.

BR
Younger Wang
-----Original Message-----
From: [email protected]
[mailto:p[email protected]] On
Behalf Of Matthew Wilson
Sent: Monday, July 14, 2008 8:21 AM
To: (e-mail address removed)
Subject: How to package a logging.config file?

I'm working on a package that uses the standard library logging module
along with a .cfg file.

In my code, I use
logging.config.fileConfig('/home/matt/mypackage/matt.cfg') to load in
the logging config file.

However, it seems really obvious to me that this won't work when I share
this package with others.

I can't figure out what path to use when I load my .cfg file.

Any ideas?

Matt
 
M

Matthew Wilson

Is your package a library or an application? If it's a library, you
should avoid configuring logging using a config file - this is because
logging configuration is process-wide, and if multiple libraries use
fileConfig to configure their logging, you may get unexpected results.

I thought that the point of using logging.getLogger('foo.bar.baz') was
to allow each module/class/function to choose from the available
configurations.

So, I can define a really weird logger and still be a good citizen.

As long as I don't tweak the root logger, is it safe to use a config
file?

Matt
 
V

Vinay Sajip

I thought that the point of usinglogging.getLogger('foo.bar.baz') was
to allow each module/class/function to choose from the available
configurations.

So, I can define a really weird logger and still be a good citizen.

As long as I don't tweak the root logger, is it safe to use a config
file?

Matt

The reason why using fileConfig in libraries is problematic is that
fileConfig assumes it does the entire logging configuration. Whenever
fileConfig is called, any loggers which are not explicitly mentioned
in the config file (but are present in the logging system) are
disabled. (They are not actually removed, since there may be still-
running threads that have references to them.) You may think this is
odd, but it's only because fileConfig was never intended for
incremental configuration, only for a one-off configuration. So, a
fileConfig'd configuration is meant to completely replace the existing
configuration.

So - you can use hierarchical naming of loggers to avoid stepping on
other loggers - for example, prefixing with the company domain name if
there's a possibility of use outside the company (in the same way as
Java packages use e.g. com.sun.XXX for Sun's proprietary Java
packages, or com.ibm.YYY for IBM's proprietary packages). However, I'd
advise against using fileConfig in library code, as it is likely to
disable already-instantiated loggers in other library packages.

Best regards,

Vinay Sajip
 

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,779
Messages
2,569,606
Members
45,239
Latest member
Alex Young

Latest Threads

Top