Skip to content

Instantly share code, notes, and snippets.

@dlbewley
Created May 24, 2015 00:38
Show Gist options
  • Save dlbewley/793efe246fd13829628a to your computer and use it in GitHub Desktop.
Save dlbewley/793efe246fd13829628a to your computer and use it in GitHub Desktop.
python setLogLevel function to setup logging; with OS X detection
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