logging to two files

T

Tor Erik Sønvisen

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
 
L

Laszlo Zsolt Nagy

Tor said:
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: (e-mail address removed)

Python forever!
 
D

Duncan Booth

Tor said:
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
 

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,773
Messages
2,569,594
Members
45,114
Latest member
GlucoPremiumReview
Top