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.
4
  • Linked question is deleted. Please edit it out. Questions should stand on their own. Commented Jul 26, 2019 at 7:10
  • updated the link of the previous query. However, the information i have given is enough to mention the problem. Commented Jul 26, 2019 at 7:22
  • please also add the code how you instantiate and use the loggers Commented Jul 26, 2019 at 9:07
  • @blues, added the code where i am passing the key of logger Commented Jul 26, 2019 at 9:31

1 Answer 1

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.

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

1 Comment

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?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.