Skip to main content
AI Assist is now on Stack Overflow. Start a chat to get instant answers from across the network. Sign up to save and share your chats.
edited tags
Link
vvvvv
  • 32.9k
  • 19
  • 70
  • 103
added 74 characters in body
Source Link
probably at the beach
  • 15.3k
  • 17
  • 82
  • 123

I have a piece of code that is initializing a logger as below.

logger = logging.getLogger() hdlr = logging.FileHandler('logfile.log') formatter = logging.Formatter('%(asctime)s %(levelname)s %(message)s') hdlr.setFormatter(formatter) logger.addHandler(hdlr) logger.setLevel(logging.DEBUG) 

Unfortunately this code is being called multiple times, is there any way I can check to see if the handler already exists - I'd prefer to implement this without having to use a Singleton.

EDIT: Sorry, forgot to mention this is on python 2.5 - cheers, Richard

I have a piece of code that is initializing a logger as below.

logger = logging.getLogger() hdlr = logging.FileHandler('logfile.log') formatter = logging.Formatter('%(asctime)s %(levelname)s %(message)s') hdlr.setFormatter(formatter) logger.addHandler(hdlr) logger.setLevel(logging.DEBUG) 

Unfortunately this code is being called multiple times, is there any way I can check to see if the handler already exists - I'd prefer to implement this without having to use a Singleton.

I have a piece of code that is initializing a logger as below.

logger = logging.getLogger() hdlr = logging.FileHandler('logfile.log') formatter = logging.Formatter('%(asctime)s %(levelname)s %(message)s') hdlr.setFormatter(formatter) logger.addHandler(hdlr) logger.setLevel(logging.DEBUG) 

Unfortunately this code is being called multiple times, is there any way I can check to see if the handler already exists - I'd prefer to implement this without having to use a Singleton.

EDIT: Sorry, forgot to mention this is on python 2.5 - cheers, Richard

Source Link
probably at the beach
  • 15.3k
  • 17
  • 82
  • 123

python logging ensure a handler is added only once

I have a piece of code that is initializing a logger as below.

logger = logging.getLogger() hdlr = logging.FileHandler('logfile.log') formatter = logging.Formatter('%(asctime)s %(levelname)s %(message)s') hdlr.setFormatter(formatter) logger.addHandler(hdlr) logger.setLevel(logging.DEBUG) 

Unfortunately this code is being called multiple times, is there any way I can check to see if the handler already exists - I'd prefer to implement this without having to use a Singleton.