By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,921 Members | 1,479 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,921 IT Pros & Developers. It's quick & easy.

How to package a logging.config file?

P: n/a
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
Jul 14 '08 #1
Share this Question
Share on Google+
4 Replies


P: n/a
Matthew Wilson wrote:
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
Normally you put the logging configuration file in the path where you put the
program that you will be sharing (quite often with no path at all).

-Larry
Jul 14 '08 #2

P: n/a
On Jul 14, 1:21 am, Matthew Wilson <m...@tplus1.comwrote:
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
Jul 14 '08 #3

P: n/a
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: py**********************************************@p ython.org
[mailto:py***************************************** *****@python.org] On
Behalf Of Matthew Wilson
Sent: Monday, July 14, 2008 8:21 AM
To: py*********@python.org
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
--
http://mail.python.org/mailman/listinfo/python-list
Jul 14 '08 #4

P: n/a
On Mon 14 Jul 2008 09:25:19 AM EDT, Vinay Sajip wrote:
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

Jul 15 '08 #5

This discussion thread is closed

Replies have been disabled for this discussion.