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

logging to two files

P: n/a
Hi

Have the following code:
import logging

logging.basicConfig(level = logging.DEBUG,
format = '[%(levelname)-8s %(asctime)s]
%(message)s',
filename = 'rfs.log',
filemode = 'w')

When using logging.(debug, info etc) stuff is logged to rfs.log.
How may I specify another log with different charateristics, such as a
different file

regards
Jul 19 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
Tor Erik Sønvisen wrote:
Hi

Have the following code:
import logging

logging.basicConfig(level = logging.DEBUG,
format = '[%(levelname)-8s %(asctime)s]
%(message)s',
filename = 'rfs.log',
filemode = 'w')

When using logging.(debug, info etc) stuff is logged to rfs.log.
How may I specify another log with different charateristics, such as a
different file

regards

I'm not sure if I understood your problem. However, just a tip for you:
is it possible to create your own handler object? (See section 6.29.5 in
the library reference).
You could setup a handler object that holds a list of other handler
objects and distribute all logging events to them. This way you should
be able to add/remove handlers at runtime.

Best,

Laci 2.0

--
__________________________________________________ _______________
Laszlo Nagy web: http://designasign.biz
IT Consultant mail: ga*****@geochemsource.com

Python forever!
Jul 19 '05 #2

P: n/a
Tor Erik Sønvisen wrote:
Hi

Have the following code:
import logging

logging.basicConfig(level = logging.DEBUG,
format = '[%(levelname)-8s %(asctime)s]
%(message)s',
filename = 'rfs.log',
filemode = 'w')

When using logging.(debug, info etc) stuff is logged to rfs.log.
How may I specify another log with different charateristics, such as a
different file

regards

You have to not use basicConfig if you want multiple handlers. Instead set
up the configuration explicitly.

Here is some code I used recently which you can use as a base. It isn't
logging to two files, instead it logs to a file and the console.

# Initialise logging
def setupLogging():
SCRIPT = os.path.splitext(os.path.basename(sys.argv[0]))[0]
SECTNAME = 'logging-'+SCRIPT
if not configuration.has_section(SECTNAME):
SECTNAME = 'logging'

options = configuration.options(SECTNAME)
if 'level' in options:
loglevel = configuration.get(SECTNAME, 'level')
else:
loglevel = 'NOTSET'

if 'console' in options:
consolelevel = configuration.get(SECTNAME, 'console')
else:
consolelevel = ''

if isinstance(loglevel, basestring):
loglevel = logging._levelNames[loglevel.upper()]

if 'format' in options:
logformat = configuration.get(SECTNAME, 'format')
else:
logformat = '%(asctime)s %(levelname)s %(message)s'

if 'filename' in options:
logfile = configuration.get(SECTNAME, 'filename')
else:
logfile='errors.log'

if 'filemode' in options:
logmode = configuration.get(SECTNAME, 'filemode')
else:
logmode = 'a'

if isinstance(consolelevel, basestring):
clevel = logging._levelNames[consolelevel.upper()]
else:
clevel = consolelevel

handler = logging.FileHandler(logfile, "a")
fmt = logging.Formatter(logformat, "%Y-%m-%d %H:%M:%S")
handler.setFormatter(fmt)
handler.setLevel(loglevel)
logging.root.addHandler(handler)

if consolelevel:
console = logging.StreamHandler()
formatter = logging.Formatter('%(levelname)-8s %(message)s')
console.setFormatter(formatter)
console.setLevel(clevel)
logging.root.addHandler(console)

logging.root.setLevel(min(loglevel, clevel))
The configuration file contains a section such as:

[logging]
level=warning
filename=output\%(script)s.log
console=info

Jul 19 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.