1
mirror of git://git.acid.vegas/random.git synced 2024-12-04 13:36:39 +00:00

Added logga python logging examples for self reference

This commit is contained in:
Dionysus 2023-05-18 22:40:28 -04:00
parent 4597f24b57
commit dc9f20b3c8
Signed by: acidvegas
GPG Key ID: EF4B922DB85DC9DE

31
logga.py Normal file
View File

@ -0,0 +1,31 @@
#!/usr/bin/env python
# logging example - developed by acidvegas in python (https://acid.vegas/random)
import logging
import logging.handlers
import os
log_file=True # Set to False for console logging only
# Set up logging
sh = logging.StreamHandler()
sh.setFormatter(logging.Formatter('%(asctime)s | %(levelname)9s | %(message)s', '%I:%M %p'))
if log_file:
if not os.path.exists('logs'):
os.makedirs('logs')
fh = logging.handlers.RotatingFileHandler('logs/debug.log', maxBytes=250000, backupCount=7, encoding='utf-8')
fh.setFormatter(logging.Formatter('%(asctime)s | %(levelname)9s | %(filename)s.%(funcName)s.%(lineno)d | %(message)s', '%Y-%m-%d %I:%M %p'))
logging.basicConfig(level=logging.NOTSET, handlers=(sh,fh))
del fh
else:
logging.basicConfig(level=logging.NOTSET, handlers=(sh,))
finally:
del sh
# Logging examples
logging.debug('This message should go to the log file')
logging.info('So should this')
logging.critical('ok')
logging.warning('And this, too')
logging.error('And non-ASCII stuff, too, like Øresund and Malmö')
logging.shutdown()