Skip to content

Instantly share code, notes, and snippets.

@e-mon
Created October 14, 2018 11:42
Show Gist options
  • Save e-mon/97f731cccb026272f083d2534fda6a5d to your computer and use it in GitHub Desktop.
Save e-mon/97f731cccb026272f083d2534fda6a5d to your computer and use it in GitHub Desktop.
import logging
import logging.handlers
import os
def get_logger(log_name, logdir=None, loglevel=logging.INFO):
_logger = logging.getLogger(log_name)
fmt = '%(asctime)s [%(levelname)s] %(message)s'
formatter = logging.Formatter(fmt)
if not _logger.hasHandlers():
if logdir is None:
handler = logging.StreamHandler()
else:
handler = logging.handlers.RotatingFileHandler(
filename=os.path.join(logdir, log_name),
maxBytes=2 * 1024 * 1024 * 1024,
backupCount=10)
handler.setFormatter(formatter)
_logger.addHandler(handler)
_logger.setLevel(loglevel)
return _logger
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment