Skip to main content
added 298 characters in body
Source Link
unutbu
  • 886.2k
  • 197
  • 1.9k
  • 1.7k

How about simply wrap the handler code in a function:

import os def myLogger(name): logger = logging.getLogger(name) logger.setLevel(logging.DEBUG) handler = logging.FileHandler(os.path.join('/some/path/', name + '.log'), 'w') logger.addHandler(handler) return logger log_hm = myLogger('healthmonitor') log_hm.info("Testing Log") # Should log to /some/path/healthmonitor.log 

To prevent creating duplicate handlers, care needs to be taken to ensure that myLogger(name) is only called once per name. Usually that means putting myLogger(name) inside

if __name__ == '__main__': log_hm = myLogger('healthmonitor') 

of the main script.

How about simply wrap the handler code in a function:

import os def myLogger(name): logger = logging.getLogger(name) logger.setLevel(logging.DEBUG) handler = logging.FileHandler(os.path.join('/some/path/', name + '.log'), 'w') logger.addHandler(handler) return logger log_hm = myLogger('healthmonitor') log_hm.info("Testing Log") # Should log to /some/path/healthmonitor.log 

How about simply wrap the handler code in a function:

import os def myLogger(name): logger = logging.getLogger(name) logger.setLevel(logging.DEBUG) handler = logging.FileHandler(os.path.join('/some/path/', name + '.log'), 'w') logger.addHandler(handler) return logger log_hm = myLogger('healthmonitor') log_hm.info("Testing Log") # Should log to /some/path/healthmonitor.log 

To prevent creating duplicate handlers, care needs to be taken to ensure that myLogger(name) is only called once per name. Usually that means putting myLogger(name) inside

if __name__ == '__main__': log_hm = myLogger('healthmonitor') 

of the main script.

modified code as per pep8 rules
Source Link
Chillar Anand
  • 29.8k
  • 10
  • 126
  • 144

How about simply wrap the handler code in a function:

import os def myLogger(name): logger=logginglogger = logging.getLogger(name) logger.setLevel(logging.DEBUG) handler=logginghandler = logging.FileHandler(os.path.join('/some/path/',name+' name + '.log'), 'w') logger.addHandler(handler) return logger log_hm = myLogger('healthmonitor') log_hm.info("Testing Log") # Should log to /some/path/healthmonitor.log 

How about simply wrap the handler code in a function:

import os def myLogger(name): logger=logging.getLogger(name) logger.setLevel(logging.DEBUG) handler=logging.FileHandler(os.path.join('/some/path/',name+'.log'),'w') logger.addHandler(handler) return logger log_hm = myLogger('healthmonitor') log_hm.info("Testing Log") # Should log to /some/path/healthmonitor.log 

How about simply wrap the handler code in a function:

import os def myLogger(name): logger = logging.getLogger(name) logger.setLevel(logging.DEBUG) handler = logging.FileHandler(os.path.join('/some/path/', name + '.log'), 'w') logger.addHandler(handler) return logger log_hm = myLogger('healthmonitor') log_hm.info("Testing Log") # Should log to /some/path/healthmonitor.log 
Source Link
unutbu
  • 886.2k
  • 197
  • 1.9k
  • 1.7k

How about simply wrap the handler code in a function:

import os def myLogger(name): logger=logging.getLogger(name) logger.setLevel(logging.DEBUG) handler=logging.FileHandler(os.path.join('/some/path/',name+'.log'),'w') logger.addHandler(handler) return logger log_hm = myLogger('healthmonitor') log_hm.info("Testing Log") # Should log to /some/path/healthmonitor.log