Skip to content

Instantly share code, notes, and snippets.

@hogjonny
Last active December 18, 2015 19:19
Show Gist options
  • Save hogjonny/5831957 to your computer and use it in GitHub Desktop.
Save hogjonny/5831957 to your computer and use it in GitHub Desktop.
... method/function for a logging class. Will return the message and information about the last function run: the function, the file called from, and the line number the function begins on.
###########################################################################
## Code Block. This Class sets up a logger for debugging.
# -------------------------------------------------------------------------
class SetupLogger():
def __init__(self):
# Set up fresh logging
self.name = name
self.log = logging.getLogger(self.name)
# Set the logging level
self.log.setLevel(logging.DEBUG)
# Setup the formatting
self.formatter = logging.Formatter('%(asctime)s - '
'%(levelname)s - '
'%(message)s')
#----------------------------------------------------------------------
def autolog(self, message):
'''Automatically log the current function details.'''
# Get the previous frame in the stack, otherwise it would
# be this function!!!
func = inspect.currentframe().f_back.f_code
# Dump the message + the name of this function to the log.
self.log.debug("{0}: {1} in {2}:{3}".format(message,
func.co_name,
func.co_filename,
func.co_firstlineno))
@hogjonny
Copy link
Author

Untested code.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment