Skip to main content

Colored Google-style logging wrapper for Python.

Project description

Colored Glog for Python
===============

Credit to the upstream **glog for Python**.

Usage
-----

Simply change **import glog** to **import colored_glog as glog**, and all usages
should be the same with upstream `glog`:

glog for Python
===============

.. image:: https://travis-ci.org/benley/python-glog.svg?branch=master
:target: https://travis-ci.org/benley/python-glog

A simple Google-style logging wrapper for Python.

This library attempts to greatly simplify logging in Python applications.
Nobody wants to spend hours pouring over the PEP 282 logger documentation, and
almost nobody actually needs things like loggers that can be reconfigured over
the network. We just want to get on with writing our apps.

Styled somewhat after the twitter.common.log_ interface, which in turn was
modeled after Google's internal python logger, which was `never actually
released`_ to the wild, and which in turn was based on the `C++ glog library`_.

Core benefits
-------------

- You and your code don't need to care about how logging works. Unless you
want to, of course.

- No more complicated setup boilerplate!

- Your apps and scripts will all have a consistent log format, and the same
predictable behaviours.

This library configures the root logger, so nearly everything you import that
uses the standard Python logging module will play along nicely.

Behaviours
----------

- Messages are always written to stderr.

- Lines are prefixed with a google-style log prefix, of the form

``E0924 22:19:15.123456 19552 filename.py:87] Log message blah blah``

Splitting on spaces, the fields are:

1. The first character is the log level, followed by MMDD (month, day)
2. HH:MM:SS.microseconds
3. Process ID
4. basename\_of\_sourcefile.py:linenumber]
5. The body of the log message.

Example use
-----------

.. code:: python

import glog as log

log.setLevel("INFO") # Integer levels are also allowed.
log.info("It works.")
log.warn("Something not ideal")
log.error("Something went wrong")
log.fatal("AAAAAAAAAAAAAAA!")

If your app uses gflags_, it will automatically gain a ``--verbosity`` flag,
and you can skip calling ``log.setLevel``. Just import glog and start logging.

Check macros / assert helpers
-----------------------------

`Like the C++ version of glog`_, python-glog provides a set of check macros
[1]_ that help document and enforce invariants. These provide a detailed
message indicating what values caused the assertion to fail, along with a stack
trace identifying the code-path that caused the failure, hopefully making it
easier to reproduce the error. Failed checks raise the FailedCheckException.
You may find these more convenient and/or more familiar than standard Python
asserts, particularly if you are working in a mixed C++ and Python codebase.


.. code:: python

import glog as log
import math

def compute_something(a):
log.check_eq(type(a), float) # require floating point types
log.check_ge(a, 0) # require non-negative values
value = math.sqrt(a)
return value

if __name__ == '__main__':
compute_something(10)


Provided check functions:

.. code:: python

check(condition)
check_eq(obj1, obj2)
check_ne(obj1, obj2)
check_le(obj1, obj2)
check_ge(obj1, obj2)
check_lt(obj1, obj2)
check_gt(obj1, obj2)
check_notnone(obj1, obj2)


Happy logging!

.. _twitter.common.log: https://github.com/twitter/commons/tree/master/src/python/twitter/common/log

.. _never actually released: https://groups.google.com/d/msg/google-glog/a_JcyJ4p8MQ/Xu-vDPiuCCYJ

.. _C++ glog library: https://github.com/google/glog

.. _gflags: https://github.com/google/python-gflags

.. _Like the C++ version of glog: https://htmlpreview.github.io/?https://github.com/google/glog/master/doc/glog.html#check

.. [1] Technically these are functions, not macros. Python does not have
syntactic macros in the sense that C++ and most lisp-like languages do.


Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

colored-glog-1.3.0.tar.gz (5.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

colored_glog-1.3.0-py2.py3-none-any.whl (7.8 kB view details)

Uploaded Python 2Python 3

File details

Details for the file colored-glog-1.3.0.tar.gz.

File metadata

  • Download URL: colored-glog-1.3.0.tar.gz
  • Upload date:
  • Size: 5.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.11.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/41.0.0 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/2.7.16

File hashes

Hashes for colored-glog-1.3.0.tar.gz
Algorithm Hash digest
SHA256 d576e08a734d11b921c42455cd59c4385a0f63b3a8c2b8cc237d39eddf0a8a47
MD5 b766de6fc0115843abb8065e9ec6d274
BLAKE2b-256 c8e051044474d8329b3027c518b15bc7466e69f2fe51305ed53703e9ec043d02

See more details on using hashes here.

File details

Details for the file colored_glog-1.3.0-py2.py3-none-any.whl.

File metadata

  • Download URL: colored_glog-1.3.0-py2.py3-none-any.whl
  • Upload date:
  • Size: 7.8 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.11.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/41.0.0 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/2.7.16

File hashes

Hashes for colored_glog-1.3.0-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 687d49eba8d540a78e80c062a28de96b8e2c95ebfa3c52eb92731f14fcfd24a2
MD5 4f332852c7d717200f86365ccd4766f5
BLAKE2b-256 1b7a1613a1e00783d89859476aa15c0c34e006755b1217604e7c332a47163586

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page