Skip to content

Instantly share code, notes, and snippets.

@TheBdouilleur
Last active March 22, 2020 14:10
Show Gist options
  • Save TheBdouilleur/4636a20512ab96a83c15560b7fcbb253 to your computer and use it in GitHub Desktop.
Save TheBdouilleur/4636a20512ab96a83c15560b7fcbb253 to your computer and use it in GitHub Desktop.
Simple logger
'''
Simple logging wrapper
@author: TheBdouilleur
@license: Free licensed
https://je.yj.fr/about.php
"c:\\Users\\pmhue\\Dropbox\\PythonScripts\\Typer\\main.log"
'''
from os import path
import datetime, time
refinedDate = datetime.date.today().strftime("%B %d, %Y")
refinedTime = datetime.datetime.now().time()
startTime = time.perf_counter()
startText = 3*'\n' +'------------------------------------------------\n' + str(refinedDate) + ', at ' + str(refinedTime) +', in logger.py:'
logPath = path.dirname(__file__)+'/main.log' #<-- absolute path to the log file
logPath = logPath.replace("/", "\\\\")
def log_start(appName):
log = open(logPath,"a")
log.write("\n"+startText+"\n")
log.close()
print('"{0}" logs can be found at: {1}'.format(appName, logPath))
def log_action(action):
actionTime = time.perf_counter() - startTime
log = open(logPath,"a")
log.write('\nAction: {0} at {1} seconds\n'.format(action, actionTime))
log.close()
def log_exception(e):
e = e
exceptionTime = time.perf_counter() - startTime
log = open(logPath,"a")
log.write('\nError {0} at {1} seconds\n'.format(str(e), exceptionTime))
log.close()
def log_end(appName):
runningTime = time.perf_counter() - startTime
log = open(logPath,"a")
log.write('\nApplication {0} ran in {1}\n'.format(appName, str(runningTime)))
log.close()
print('\n"{0}" ran in {1} seconds\n'.format(appName, str(runningTime)))
if __name__ == "__main__":
log_start('Example app')
log_action("Example action")
try:
print(5+'5') # Simple error to demonstrate program
except Exception as e:
log_exception(e)
log_end('Example app')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment