Reputation: 113
I created two separate logs with two different names (windows). One is a rotating log and the other not. How can I prevent the data that I write to the rotating log from logging to the non-rotating log?
I am not very log savvy.
import logging
from logging.handlers import RotatingFileHandler
def main():
def normalLogging(fn1):
global normalLogger
#Create and configure logger
logging.basicConfig(filename=fn1, format='%(asctime)s %(message)s', filemode='w')
#Creating an object
normalLogger=logging.getLogger()
#Setting the threshold of logger to DEBUG
normalLogger.setLevel(logging.DEBUG)
def rotatingLog(fn2):
global rotatingLogger
# start the roll-screen logger too
rotatingLogger = logging.getLogger(fn2)
rotatingLogger.setLevel(logging.DEBUG)
# add a rotating logger
handlerScreen = RotatingFileHandler(fn2, maxBytes=1000, backupCount=3)
rotatingLogger.addHandler(handlerScreen)
def normalTest():
#Test messages to the rotating log
normalLogger.debug("normalLogger.debug - Harmless debug Message")
normalLogger.info("normalLogger.info - Just an information")
normalLogger.warning("normalLogger.warning - Its a Warning")
normalLogger.error("normalLogger.error - Did you try to divide by zero")
normalLogger.critical("normalLogger.critical - Internet is down")
def rotatorTest():
for i in range(1, 100):
#Test messages to the rotating log
rotatingLogger.debug("rotatingLogger.debug %s - Harmless debug Message" % i)
rotatingLogger.info("rotatingLogger.info %s - Just an information" % i)
rotatingLogger.warning("rotatingLogger.warning %s - Its a Warning" % i)
rotatingLogger.error("rotatingLogger.error %s - Did you try to divide by zero" % i)
rotatingLogger.critical("rotatingLogger.critical %s - Internet is down" % i)
# fn2 = "rotatorLog"
rotatingLog("rotatorLog")
rotatorTest()
# fn1 = "normalLog"
normalLogging("normalLog")
normalTest()
rotatorTest()
if __name__ == '__main__':
main()
The rotating log maintains it's own unique data, but the normal log has data from the rotating log. I expected that the data would be unique to each log since I write to them separately, but that's not the case.
Upvotes: 0
Views: 65
Reputation: 5185
All you need to do is rotatingLogger.propagate = False
to stop it's logs being sent to the root logger which is used for normalLogger. basicConfig configures the root logger and logging.getLogger without a name returns the root logger.
Upvotes: 1
Reputation: 142651
First: use name for normal logger - ie. logging.getLogger(fn1)
Second: use FileHandler
instead of basicConfig
for normal logger.
import logging
from logging.handlers import RotatingFileHandler
from logging import FileHandler
def main():
def normalLogging(fn1):
global normalLogger
global normalLogger
normalLogger = logging.getLogger(fn1)
normalLogger.setLevel(logging.DEBUG)
handlerScreen = FileHandler(fn1)
handlerScreen.setFormatter(logging.Formatter('%(asctime)s %(message)s'))
normalLogger.addHandler(handlerScreen)
def rotatingLog(fn2):
global rotatingLogger
rotatingLogger = logging.getLogger(fn2)
rotatingLogger.setLevel(logging.DEBUG)
handlerScreen = RotatingFileHandler(fn2, maxBytes=1000, backupCount=3)
handlerScreen.setFormatter(logging.Formatter('%(asctime)s %(message)s'))
rotatingLogger.addHandler(handlerScreen)
# ... rest ...
As I remember basicConfig
creates logger which later is used by all loggers so they use the same settings.
Upvotes: 0