Skip to content

Instantly share code, notes, and snippets.

@bmacauley
Created March 28, 2013 13:37
Show Gist options
  • Save bmacauley/5263168 to your computer and use it in GitHub Desktop.
Save bmacauley/5263168 to your computer and use it in GitHub Desktop.
logging
#set up logging
logger = logging.getLogger('main')
logger.setLevel(logging.DEBUG)
# Use file output for production logging:
filename, fileextension = os.path.splitext(sys.argv[0])
# log file name <script name>.log
log_file = '{0}.log'.format(filename)
filelog = logging.FileHandler(log_file, 'a')
filelog.setLevel(logging.INFO)
# Use console for development logging:
conlog = logging.StreamHandler()
conlog.setLevel(logging.DEBUG)
# Specify log formatting:
#formatter = logging.Formatter('%(asctime)s - %(levelname)s - %(module)s - %(lineno)s - %(message)s')
formatter = logging.Formatter('%(asctime)s - %(module)s - %(lineno)s - %(levelname)s - %(message)s')
conlog.setFormatter(formatter)
filelog.setFormatter(formatter)
# add console and filelog to logger
logger.addHandler(conlog)
logger.addHandler(filelog)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment