Reputation: 1792
I have a Django web application that runs a bunch of helper scripts. Each of these scripts writes its own log. I am noticing that the scripts are writing to each others logs. These scripts are launched by separate users/sessions. Whats going on?
Here is the implementation of the logger within ScriptA
import logging
logging.basicConfig(format='%(asctime)s %(message)s',filename='/var/logs/scriptA.log',level=logging.DEBUG)
logging.info("INFO: test log from scriptA"))
same for scriptB but say someone runs scriptB, it writes to scriptA.log instead of scriptB.log
it seems logging is created a shared global module. How can I stop this
EDIT: most solutions here are for 2 separate logs within the same class. my issue is separate scripts/classes writing to each others logs
Upvotes: 0
Views: 120
Reputation: 167
Yes, the logging module for Python is a global module. I ran to the same issue before. I used this code to separate the logs for each module/class.
import logging
import sys
class SampleCass:
def __init__(self):
self.set_logger()
self.log = logging.getLogger(__name__)
@run_one
def set_logger():
# this separates the logging from different modules
logger = logging.getLogger(__name__)
console_handler = logging.StreamHandler(sys.stdout)
logger.setLevel(logging.DEBUG)
formatter = logging.Formatter('[%(asctime)s %(levelname)s ' +
'%(name)s]: %(message)s',
'%Y-%m-%d %H:%M:%S')
console_handler.setFormatter(formatter)
# add the handler to the class logger
logger.addHandler(console_handler)
def run_once(func):
"""
A decorator to run function only once
:type func: __builtin__.function
:return:
"""
def wrapper(*args, **kwargs):
"""
:param args:
:param kwargs:
:return:
"""
if not wrapper.has_run:
wrapper.has_run = True
return func(*args, **kwargs)
wrapper.has_run = False
return wrapper
Now you can use self.log for your class and it will not interfere with the logging of other modules. run_one function ensures that when you import the function, it will only run once and only once. Hope it helps you.
Upvotes: 1