summaryrefslogtreecommitdiffstats
path: root/lib/Python/Lib/colorlog/logging.py
diff options
context:
space:
mode:
authorGravatar Walter Purcaro <vuolter@users.noreply.github.com> 2015-04-13 16:05:04 +0200
committerGravatar Walter Purcaro <vuolter@users.noreply.github.com> 2015-04-13 16:05:04 +0200
commit99ec1c400d79ecc41ae1745e794e21e2e79d2add (patch)
treea20349bc3e5a1b20e0c8184f5aeb2f7c43a43194 /lib/Python/Lib/colorlog/logging.py
parentMerge branch 'stable' into 0.4.10 (diff)
downloadpyload-99ec1c400d79ecc41ae1745e794e21e2e79d2add.tar.xz
Move lib to lib/Python/Lib
Diffstat (limited to 'lib/Python/Lib/colorlog/logging.py')
-rw-r--r--lib/Python/Lib/colorlog/logging.py44
1 files changed, 44 insertions, 0 deletions
diff --git a/lib/Python/Lib/colorlog/logging.py b/lib/Python/Lib/colorlog/logging.py
new file mode 100644
index 000000000..13f0c4ffb
--- /dev/null
+++ b/lib/Python/Lib/colorlog/logging.py
@@ -0,0 +1,44 @@
+"""Wrappers around the logging module."""
+
+from __future__ import absolute_import
+
+import functools
+import logging
+
+from colorlog.colorlog import ColoredFormatter
+
+BASIC_FORMAT = "%(log_color)s%(levelname)s%(reset)s:%(name)s:%(message)s"
+
+
+def basicConfig(**kwargs):
+ """Call ``logging.basicConfig`` and override the formatter it creates."""
+ logging.basicConfig(**kwargs)
+ logging._acquireLock()
+ try:
+ stream = logging.root.handlers[0]
+ stream.setFormatter(
+ ColoredFormatter(
+ fmt=kwargs.get('format', BASIC_FORMAT),
+ datefmt=kwargs.get('datefmt', None)))
+ finally:
+ logging._releaseLock()
+
+
+def ensure_configured(func):
+ """Modify a function to call ``basicConfig`` first if no handlers exist."""
+ @functools.wraps(func)
+ def wrapper(*args, **kwargs):
+ if len(logging.root.handlers) == 0:
+ basicConfig()
+ return func(*args, **kwargs)
+ return wrapper
+
+root = logging.root
+getLogger = logging.getLogger
+debug = ensure_configured(logging.debug)
+info = ensure_configured(logging.info)
+warning = ensure_configured(logging.warning)
+error = ensure_configured(logging.error)
+critical = ensure_configured(logging.critical)
+log = ensure_configured(logging.log)
+exception = ensure_configured(logging.exception)