Skip to content

Instantly share code, notes, and snippets.

@xeor
Last active November 19, 2015 11:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xeor/a6a506aabee0deec5d35 to your computer and use it in GitHub Desktop.
Save xeor/a6a506aabee0deec5d35 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import sys
import logging
logger = logging.getLogger(__name__)
logger.addHandler(logging.NullHandler()) # Defaulting to null output in case Caculator class is imported
logging.basicConfig(filename='/error.log', filemode='a', level=logging.ERROR, format='%(asctime)s - %(levelname)s - %(message)s')
class ClassName(object):
def __init__(self):
"""
>>> 2 * 2
4
>>> 1 + 1
2
"""
logger.debug('Loading class')
if __name__ == '__main__':
log_handler = logging.StreamHandler()
# Looks like: 2015-11-16 13:07:24,481 - filename.__init__:12 - DEBUG - Logg message
log_handler.setFormatter(logging.Formatter('%(asctime)s - %(module)s.%(funcName)s:%(lineno)d - %(levelname)s - %(message)s'))
logger.addHandler(log_handler) # Display info when running script manually
logger.setLevel(logging.DEBUG) # Set to logging.DEBUG for debug
cn = ClassName()
mode = sys.argv[1]
if mode == 'test':
logger.info('Running tests, no output means everything is fine!')
import doctest
doctest.testmod()
if mode == 'fail':
# Generic use of logging exceptions
try:
cn.fail()
except Exception, e:
# We shoulnt die, but log..
logging.exception(e)
sys.exit(0)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment