0

I am trying to log the actions in a function and I have written the following function to log responses to different files based on the type of response i.e. info,error,debug and warning.

logging.basicConfig(filename='indivi_service.log',
                    format='%(asctime)s - %(name)s - %(levelname)s - %(message)s' )
def setup_logger(logger_name, log_file, level=logging.DEBUG):
    l = logging.getLogger(logger_name)
    formatter = logging.Formatter()
    fileHandler = logging.FileHandler(log_file)
    fileHandler.setFormatter(formatter)
    handler = TimedRotatingFileHandler(logger_name,
                               when="d",
                               interval=1,
                               backupCount=100)


l.setLevel(level)
l.addHandler(fileHandler)
l.addHandler(handler)


setup_logger('debug', r'debug')
setup_logger('error', r'error')
setup_logger('warning', r'warning')
setup_logger('info', r'info')
debug = logging.getLogger('debug')
error = logging.getLogger('error')
warning = logging.getLogger('warning')
info = logging.getLogger('info')


class Info(APIHandler):
    @gen.coroutine
    def post(self):
        req = json.loads(self.request.body)
        resp, content = client(item_id=req['item_id'])
        debug.debug(content)
        info.info(hello world)
        warning.warn('warning message')
        error.error('error message')

The problem that I am facing is that a response is printed twice each time I call a function.

for example: info.log

hello world hello world

Can anyone tell me why is it happening like. This is the case with all the log files.

Thanks

Tony Roczz
  • 2,366
  • 6
  • 32
  • 59

1 Answers1

0

try:

import logging
import logging.handlers

logging.basicConfig(filename='indivi_service.log',
                    format='%(asctime)s - %(name)s - %(levelname)s - %(message)s' )
def setup_logger(logger_name, log_file, level=logging.DEBUG):
    l = logging.getLogger(logger_name)
    formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
    handler = logging.handlers.TimedRotatingFileHandler(str(log_file)+'.log', when="d", interval=1, backupCount=100)
    handler.setFormatter(formatter)

    l.setLevel(level)
    l.addHandler(handler)


setup_logger('debug', r'debug')
setup_logger('error', r'error')
setup_logger('warning', r'warning')
setup_logger('info', r'info')
debug = logging.getLogger('debug')
error = logging.getLogger('error')
warning = logging.getLogger('warning')
info = logging.getLogger('info')


if __name__ == "__main__":
    info.info('hello world')
    error.info('hello world')

after run this script, file info.log has one 'hello world' and error.log also has only one 'hello world'.

Fujiao Liu
  • 2,195
  • 2
  • 24
  • 28
  • Thanks. Actually my requirement is to do a `TimedRotatingFileHandler` and store the different response to separate files like error.log, info.log and debug.log, Is there a way i can do this without having to print in 2 log files. (I want to have just one file with `TimedRotatingFileHandler` and `FileHandler` functionality.) Is there a way to do something like that with just 1 handler instead of the above used 2 handlers – Tony Roczz Oct 30 '15 at 08:56
  • just remove the FileHandler and only add TimedRotatingFileHandler . – Fujiao Liu Oct 30 '15 at 08:59
  • By removing FileHandler, I could only print everything in the same file, I want the error to be saved in error.log and warning to be saved in warning.log and info's in info.log – Tony Roczz Oct 30 '15 at 09:04
  • update anwser , error to error.log and warning to warning.log – Fujiao Liu Oct 30 '15 at 09:11