Last active
December 18, 2015 19:19
-
-
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
########################################################################### | |
## 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)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Untested code.