5

i want to create separate logging file in python like info.log, debug.log, error.log i have a setting file(logging.conf) for logging as given below

[loggers]
keys=root,simpleExample

[handlers]
keys=consoleHandler

[formatters]
keys=simpleFormatter

[logger_root]
level=DEBUG
handlers=consoleHandler

[logger_simpleExample]
level=DEBUG
handlers=consoleHandler
qualname=simpleExample
propagate=0

[handler_consoleHandler]
class=StreamHandler
level=DEBUG
formatter=simpleFormatter
args=(sys.stdout,)

[formatter_simpleFormatter]
format=%(asctime)s - %(name)s - %(levelname)s - %(message)s
datefmt=

and i have created logging.py file as given below

import logging
import logging.config

logging.config.fileConfig('logging.conf')

# create logger
logger = logging.getLogger('simpleExample')

# 'application' code
logger.debug('debug message')
logger.info('info message')
logger.warn('warn message')
logger.error('error message')
logger.critical('critical message')

but when execute logging.py file i am getting following result in consol

2012-12-10 13:30:20,030 - simpleExample - DEBUG - debug message
2012-12-10 13:30:20,031 - simpleExample - INFO - info message
2012-12-10 13:30:20,032 - simpleExample - WARNING - warn message
2012-12-10 13:30:20,032 - simpleExample - ERROR - error message
2012-12-10 13:30:20,033 - simpleExample - CRITICAL - critical message

as i have told i want to create separate file in log.info, debug.info, error.info. thanks in advance

4
  • 1
    That would require a custom handler that has both a minimum and a maximum log level. Normally there is only a minimum level (which, internally, are numeric). Commented Dec 10, 2012 at 8:28
  • i don not have idea to create custom handler in logging.conf file, can you give any link or reference which will help me. Commented Dec 10, 2012 at 9:03
  • It won't be an easy task, I can only point you to the logging module documentation really. I'd rethink why you'd want to have separate files in the first place. Commented Dec 10, 2012 at 9:04
  • 1
    I would find it confusing to have the logging in separate files, myself. When diagnosing a problem I want to know the exact order messages were logged, and having the different levels separated would make that a lot more difficult. Commented Dec 10, 2012 at 9:17

1 Answer 1

7

You need to config multiple handler to output different level log to different files. For example, you want to INFO level log to info.log, you can define a fileHandler with INFO filer

class MyFilter(logging.Filter):
    def filter(self, rec):
        return rec.levelno == logging.INFO

class MyHandler(logging.FileHandler):
    def __init__(self, *arg, **kw):
        logging.FileHandler.__init__(self, *arg, **kw)
        self.addFilter(MyFilter())

And add it to logging namespace:

logging.MyHandler = MyHandler

so you can use it in your config file:

[handlers]
keys=consoleHandler,onlyinfoHandler
[handler_onlyinfoHandler]
class=MyHandler
level=DEBUG
formatter=simpleFormatter
args=('info.log','w')

You can continue to add others or use level as handler args.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.