Last active
August 29, 2015 14:21
-
-
Save CUXIDUMDUM/91c4482ef486aee20bb6 to your computer and use it in GitHub Desktop.
python logging
This file contains 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
#https://github.com/timwarnock/daemonize/blob/master/Log.py | |
#https://github.com/search?l=python&p=4&q=class+threading.Thread+multiprocessing+python+logging&ref=searchresults&type=Code&utf8=%E2%9C%93 | |
# vim: set tabstop=4 shiftwidth=4 autoindent smartindent: | |
''' | |
simple thread-safe logging adapter | |
Assigns log handlers to the logging module, you can use logging module methods directly or | |
create additional loggers that will inherit these handlers, e.g., | |
import Log, logging | |
logging.info('hello world') | |
import Log, logging | |
spam = logging.getLogger('spam') | |
spam.info('hello from spam') | |
import Log, logging as log | |
log.info('hello world') | |
''' | |
import logging | |
def init(LOG_FILE=False, LOG_CONSOLE=False, LOG_SYSLOG=False): | |
# remove all root handlers | |
for handler in logging.root.handlers: | |
logging.root.removeHandler(handler) | |
# Log to file | |
if LOG_FILE: | |
logging.basicConfig( | |
filename=LOG_FILE, | |
level=logging.INFO, | |
format='%(asctime)-15s %(levelname)s:%(filename)s:%(lineno)d -- %(message)s' | |
) | |
# Log to console | |
if LOG_CONSOLE: | |
console = logging.StreamHandler() | |
console.setLevel(logging.DEBUG) | |
console.setFormatter(logging.Formatter('%(levelname)s:%(filename)s:%(lineno)d -- %(message)s')) | |
logging.getLogger().addHandler(console) | |
# Log to syslog | |
if LOG_SYSLOG: | |
from logging.handlers import SysLogHandler | |
syslog = SysLogHandler(address='/dev/log') | |
syslog.setFormatter(logging.Formatter('%(asctime)-15s %(levelname)s:%(filename)s:%(lineno)d -- %(message)s')) | |
logging.getLogger().addHandler(syslog) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment