Tor Erik Sønvisen wrote:
Hi
Have the following code:
import logging
logging.basicCo nfig(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.splitex t(os.path.basen ame(sys.argv[0]))[0]
SECTNAME = 'logging-'+SCRIPT
if not configuration.h as_section(SECT NAME):
SECTNAME = 'logging'
options = configuration.o ptions(SECTNAME )
if 'level' in options:
loglevel = configuration.g et(SECTNAME, 'level')
else:
loglevel = 'NOTSET'
if 'console' in options:
consolelevel = configuration.g et(SECTNAME, 'console')
else:
consolelevel = ''
if isinstance(logl evel, basestring):
loglevel = logging._levelN ames[loglevel.upper( )]
if 'format' in options:
logformat = configuration.g et(SECTNAME, 'format')
else:
logformat = '%(asctime)s %(levelname)s %(message)s'
if 'filename' in options:
logfile = configuration.g et(SECTNAME, 'filename')
else:
logfile='errors .log'
if 'filemode' in options:
logmode = configuration.g et(SECTNAME, 'filemode')
else:
logmode = 'a'
if isinstance(cons olelevel, basestring):
clevel = logging._levelN ames[consolelevel.up per()]
else:
clevel = consolelevel
handler = logging.FileHan dler(logfile, "a")
fmt = logging.Formatt er(logformat, "%Y-%m-%d %H:%M:%S")
handler.setForm atter(fmt)
handler.setLeve l(loglevel)
logging.root.ad dHandler(handle r)
if consolelevel:
console = logging.StreamH andler()
formatter = logging.Formatt er('%(levelname )-8s %(message)s')
console.setForm atter(formatter )
console.setLeve l(clevel)
logging.root.ad dHandler(consol e)
logging.root.se tLevel(min(logl evel, clevel))
The configuration file contains a section such as:
[logging]
level=warning
filename=output \%(script)s.log
console=info