Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

colorful glog. #11

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.rst
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
glog for Python
Color Glog for Python
===============

.. image:: https://travis-ci.org/benley/python-glog.svg?branch=master
Expand Down
62 changes: 47 additions & 15 deletions glog.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
"""A simple Google-style logging wrapper."""

import logging
import colorlog
import time
import traceback
import os

import time
import gflags as flags

FLAGS = flags.FLAGS
Expand All @@ -18,7 +19,7 @@ def format_message(record):
return record_message


class GlogFormatter(logging.Formatter):
class GlogFormatter(colorlog.ColoredFormatter):
LEVEL_MAP = {
logging.FATAL: 'F', # FATAL is alias of CRITICAL
logging.ERROR: 'E',
Expand All @@ -28,7 +29,7 @@ class GlogFormatter(logging.Formatter):
}

def __init__(self):
logging.Formatter.__init__(self)
colorlog.ColoredFormatter.__init__(self, '%(log_color)s%(message)s%(reset)s')

def format(self, record):
try:
Expand All @@ -45,7 +46,8 @@ def format(self, record):
record.lineno,
format_message(record))
record.getMessage = lambda: record_message
return logging.Formatter.format(self, record)
return colorlog.ColoredFormatter.format(self, record)


logger = logging.getLogger()
handler = logging.StreamHandler()
Expand All @@ -56,8 +58,22 @@ def setLevel(newlevel):
logger.debug('Log level set to %s', newlevel)


def setDirectory(directory):
if not os.path.exists(directory):
os.makedirs(directory)

localtime = time.localtime()
filepath = directory + "/%04d%02d%02d-%02d%02d%02d.log" % (
localtime.tm_year, localtime.tm_mon, localtime.tm_mday, localtime.tm_hour, localtime.tm_min, localtime.tm_sec)
fhandler = logging.FileHandler(filepath)
logger.addHandler(fhandler)
logger.debug('Log path set to %s' % filepath)


def init():
setLevel(FLAGS.verbosity)
setDirectory(FLAGS.directory)


debug = logging.debug
info = logging.info
Expand Down Expand Up @@ -86,16 +102,16 @@ def init():
_level_letters = [name[0] for name in _level_names.values()]

GLOG_PREFIX_REGEX = (
r"""
(?x) ^
(?P<severity>[%s])
(?P<month>\d\d)(?P<day>\d\d)\s
(?P<hour>\d\d):(?P<minute>\d\d):(?P<second>\d\d)
\.(?P<microsecond>\d{6})\s+
(?P<process_id>-?\d+)\s
(?P<filename>[a-zA-Z<_][\w._<>-]+):(?P<line>\d+)
\]\s
""") % ''.join(_level_letters)
r"""
(?x) ^
(?P<severity>[%s])
(?P<month>\d\d)(?P<day>\d\d)\s
(?P<hour>\d\d):(?P<minute>\d\d):(?P<second>\d\d)
\.(?P<microsecond>\d{6})\s+
(?P<process_id>-?\d+)\s
(?P<filename>[a-zA-Z<_][\w._<>-]+):(?P<line>\d+)
\]\s
""") % ''.join(_level_letters)
"""Regex you can use to parse glog line prefixes."""

handler.setFormatter(GlogFormatter())
Expand All @@ -111,6 +127,7 @@ def Parse(self, arg):
flags.BooleanFlag.Parse(self, arg)
logging.captureWarnings(self.value)


flags.DEFINE_flag(CaptureWarningsFlag())


Expand All @@ -123,13 +140,14 @@ def Parse(self, arg):
# Look up the name for this level (DEBUG, INFO, etc) if it exists
try:
level = logging._levelNames.get(intarg, intarg)
except AttributeError: # This was renamed somewhere b/w 2.7 and 3.4
except AttributeError: # This was renamed somewhere b/w 2.7 and 3.4
level = logging._levelToName.get(intarg, intarg)
except ValueError:
level = arg
setLevel(level)
return level


flags.DEFINE(
parser=VerbosityParser(),
serializer=flags.ArgumentSerializer(),
Expand All @@ -138,6 +156,20 @@ def Parse(self, arg):
help='Logging verbosity')


class DirectoryParser(flags.ArgumentParser):
def Parse(self, arg):
setDirectory(arg)
return arg


flags.DEFINE(
parser=DirectoryParser(),
serializer=flags.ArgumentSerializer(),
name='logdir',
default=None,
help='The directory of log output.')


# Define functions emulating C++ glog check-macros
# https://htmlpreview.github.io/?https://github.com/google/glog/master/doc/glog.html#check

Expand Down
3 changes: 2 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import os
import setuptools

VERSION = '0.3.1'
VERSION = '0.3.2'

README = open(os.path.join(os.path.dirname(__file__), 'README.rst')).read()

Expand All @@ -14,6 +14,7 @@
url='https://github.com/benley/python-glog',
install_requires=[
'python-gflags>=3.1',
'colorlog',
'six', # glog doesn't need six, but gflags 3.1 does and its distutils
# "requires" line apparently accomplishes nothing, so ...
],
Expand Down