mirror of
git://git.acid.vegas/random.git
synced 2024-11-14 20:16:40 +00:00
Added logga python logging examples for self reference
This commit is contained in:
parent
4597f24b57
commit
dc9f20b3c8
31
logga.py
Normal file
31
logga.py
Normal 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()
|
Loading…
Reference in New Issue
Block a user