Skip to content

Instantly share code, notes, and snippets.

@123Daoxyz
Last active August 29, 2015 14:03
Show Gist options
  • Save 123Daoxyz/632534a654c1cd955657 to your computer and use it in GitHub Desktop.
Save 123Daoxyz/632534a654c1cd955657 to your computer and use it in GitHub Desktop.
logging replacement for python std one
# get from goagent
class SimpleLogging(object):
CRITICAL = 50
FATAL = CRITICAL
ERROR = 40
WARNING = 30
WARN = WARNING
INFO = 20
DEBUG = 10
NOTSET = 0
def __init__(self, *args, **kwargs):
self.level = SimpleLogging.INFO
if self.level > SimpleLogging.DEBUG:
self.debug = self.dummy
self.__write = sys.stdout.write
@classmethod
def getLogger(cls, *args, **kwargs):
return cls(*args, **kwargs)
def basicConfig(self, *args, **kwargs):
self.level = kwargs.get('level', SimpleLogging.INFO)
if self.level > SimpleLogging.DEBUG:
self.debug = self.dummy
def log(self, level, fmt, *args, **kwargs):
self.__write('%s - - [%s] %s\n' % (level, time.ctime()[4:-5], fmt%args))
def dummy(self, *args, **kwargs):
pass
def debug(self, fmt, *args, **kwargs):
self.log('DEBUG', fmt, *args, **kwargs)
def info(self, fmt, *args, **kwargs):
self.log('INFO', fmt, *args)
def warning(self, fmt, *args, **kwargs):
self.log('WARNING', fmt, *args, **kwargs)
def warn(self, fmt, *args, **kwargs):
self.log('WARNING', fmt, *args, **kwargs)
def error(self, fmt, *args, **kwargs):
self.log('ERROR', fmt, *args, **kwargs)
def exception(self, fmt, *args, **kwargs):
self.log('ERROR', fmt, *args, **kwargs)
traceback.print_exc(file=sys.stderr)
def critical(self, fmt, *args, **kwargs):
self.log('CRITICAL', fmt, *args, **kwargs)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment