3

This is extension of this query.

The bug I found with the config file is by using the logger, at any point of time both ws_in_.log and ws_out_.log files are getting created even though they are under different handlers and meant for different processes. I mean, if I run IN process then respective IN logs are getting logged in ws_in.log file. But along with that an empty ws_out.log file is also getting created.

So is there any way of restricting the creation of log file for respective process.please help.

Thanks & Regards Pragyan

[loggers]
keys=root, ws_in_log, ws_out_log

[handlers]
keys=consoleHandler, ws_in_hand, ws_out_hand

[formatters]
keys=generic_form

[loggers]
keys=root, ws_in_log, ws_out_log

[handlers]
keys=consoleHandler, ws_in_hand, ws_out_hand

[formatters]
keys=generic_form

[logger_root]
handlers=consoleHandler
level=NOTSET

[logger_ws_in_log]
level=NOTSET
handlers=ws_in_hand
qualname=ws_in_log

[logger_ws_out_log]
level=NOTSET
handlers=ws_out_hand
qualname=ws_out_log

[handler_ws_in_hand]
class=logging.handlers.TimedRotatingFileHandler
level=NOTSET
formatter=generic_form
args=('/path/ws_in_.log', 'h', 1, 0, None, False, True)

[handler_ws_out_hand]
class=logging.handlers.TimedRotatingFileHandler
level=NOTSET
formatter=generic_form
args=('/path/em/ws_out_.log', 'h', 1, 0, None, False, True)

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

[formatter_generic_form]
format='%(asctime)s - %(levelname)s - %(message)s'
datefmt='%Y-%m-%d %H:%M:%S'
class=

I am using the above config file by the scrpit IN.py,

import logging.config

logging.config.fileConfig('x.ini')
logger=logging.getLogger('ws_in_log')
class Car(object):
    def __init__(self,brand,model,color):
        self.brand = brand
        self.model = model
        self.color = color

    def drive(self):
        self.condition = 'Used'
        print("in drive")
def drive(msg):
    logger.debug("in script function")
    logger.debug(msg)

Expected : only "ws_in_.log" file shoud be created if "IN" script get called.
Actual: Both "ws_in_.log" and "ws_out_.log" files are getting created if only "IN" script get called.

1 Answers1

0

The one way to fix this is to replace logging.handlers.TimedRotatingFileHandler with a version that only opens or creates the file when it receives a message to log to the file.

Another is to just accept the creation of the empty file.

The implementation of logging.handlers.TimedRotatingFileHandler creates the log file if it does not exist when its instance is created.

Dan D.
  • 73,243
  • 15
  • 104
  • 123
  • Its not only about "logging.handlers.TimedRotatingFileHandler" . If two handlers are of same type then the log file is created unwanted. Until unless the type handlers are different it works fine. So is there any way to restrict other handlers based on the keys or logger name? – Pragyan Paramita Das Jul 26 '19 at 10:19