Skip to content

Instantly share code, notes, and snippets.

@gargolito
Last active January 3, 2019 19:41
Show Gist options
  • Save gargolito/0ee986334ceb6d5f5e711487fd35c198 to your computer and use it in GitHub Desktop.
Save gargolito/0ee986334ceb6d5f5e711487fd35c198 to your computer and use it in GitHub Desktop.
python setup logger
"""Function setup as many loggers as you want"""
formatter = logging.Formatter('%(asctime)s - %(levelname)s - %(module)s - line %(lineno)d - %(message)s')
def setup_logger(name, log_file, level=logging.DEBUG):
handler = logging.FileHandler(log_file)
handler.setFormatter(formatter)
logger = logging.getLogger(name)
logger.setLevel(level)
logger.addHandler(handler)
return logger
log_root_path = '/tmp/'
if not os.path.exists(log_root_path):
os.makedirs(log_root_path)
script_log_path = log_root_path + '/script_name'
if not os.path.exists(script_log_path):
os.makedirs(script_log_path)
# identifier = str(uuid.uuid4())
# identifier = str(int(time.time())
script_log_name = script_log_path + "/my_script-" + identifier + ".log"
failover_script_log = setup_logger('my_script', script_log_name)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment