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

-- 
http://mail.python.org/mailman/listinfo/python-list

Reply via email to