Created
May 24, 2015 00:38
-
-
Save dlbewley/793efe246fd13829628a to your computer and use it in GitHub Desktop.
python setLogLevel function to setup logging; with OS X detection
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
import os | |
import platform | |
import logging, logging.handlers | |
def setLogLevel(loglevel='debug'): | |
numeric_loglevel = getattr(logging, loglevel.upper(), None) | |
if not isinstance(numeric_loglevel, int): | |
raise ValueError('Invalid log level: "%s"\n Try: "debug", "info", "warning", "critical".' % loglevel) | |
logging.basicConfig(level=numeric_loglevel, format='%(asctime)s %(name)s %(levelname)s %(message)s') | |
program = os.path.basename(__file__) | |
logger = logging.getLogger(program) | |
syslog_address = '/dev/log' | |
if platform.system() == 'Darwin': | |
syslog_address = '/var/run/syslog' | |
log_handler = logging.handlers.SysLogHandler(address = syslog_address) | |
logger.addHandler(log_handler) | |
return logger | |
logger = setLogLevel() | |
logger.info('Hello Log') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment