Skip to content

Instantly share code, notes, and snippets.

@tapanhp
Created February 12, 2020 09:32
Show Gist options
  • Save tapanhp/33d97c1f70f50c9865cf5f8d88373c92 to your computer and use it in GitHub Desktop.
Save tapanhp/33d97c1f70f50c9865cf5f8d88373c92 to your computer and use it in GitHub Desktop.
Python Logging module utilities
import datetime
import logging
import os
import re
import constants
log_separator = "\n===================================================================================================="
def setup_log_config(file_name, logs_path=None, log_format="%(asctime)s - %(message)s", log_level=logging.DEBUG):
logger = logging.getLogger(file_name)
logger.setLevel(log_level)
current_date = datetime.date.today().strftime("%B-%d-%Y")
if not logs_path:
log_location = os.path.join(constants.CRAWLER_LOGS_PATH, current_date)
else:
log_location = logs_path
if not os.path.exists(log_location):
if os.path.isabs(log_location):
os.makedirs(log_location)
else:
raise IOError("Invalid path for log files. Check if path is valid and absolute path to directory.")
file_name = os.path.join(log_location, file_name + ".log")
if not re.search(r'[^A-Za-z0-9_\-\\]', file_name):
raise IOError("Invalid file name for log files.")
handler = logging.FileHandler(file_name, 'a')
logger.addHandler(handler)
logger.handlers[0].setFormatter(logging.Formatter(log_format))
return logger
def log_current_time(msg, logger):
current_time = datetime.datetime.now()
logger.warning(msg + '\n\tStarts at : {}'.format(current_time))
return current_time
def log_time_difference(msg, pre_time, logger):
current_time = datetime.datetime.now()
td = current_time - pre_time
logger.warning(msg + "\n\tEnd at: {}".format(
current_time) + "\n\tTime to complete process: {}:{}".format(td.seconds // 60 % 60,
td.seconds) + log_separator)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment