[Python-modules-commits] [python-coloredlogs] 02/02: Import python-coloredlogs_6.0.orig.tar.gz
Gaurav Juvekar
gauravjuvekar-guest at moszumanska.debian.org
Sun Mar 12 20:19:58 UTC 2017
This is an automated email from the git hooks/post-receive script.
gauravjuvekar-guest pushed a commit to branch upstream
in repository python-coloredlogs.
commit b81cb9c09fa613e38b73cbdf3a10ce779d873a87
Author: Gaurav Juvekar <gauravjuvekar at gmail.com>
Date: Sun Mar 12 21:15:29 2017 +0100
Import python-coloredlogs_6.0.orig.tar.gz
---
LICENSE.txt | 2 +-
PKG-INFO | 17 ++++--
README.rst | 15 ++++--
coloredlogs.egg-info/PKG-INFO | 17 ++++--
coloredlogs/__init__.py | 117 ++++++++++++++++++++++++++++++++++--------
coloredlogs/syslog.py | 35 +++++++++----
setup.cfg | 1 -
7 files changed, 158 insertions(+), 46 deletions(-)
diff --git a/LICENSE.txt b/LICENSE.txt
index 3af15dd..71c0236 100644
--- a/LICENSE.txt
+++ b/LICENSE.txt
@@ -1,4 +1,4 @@
-Copyright (c) 2015 Peter Odding
+Copyright (c) 2017 Peter Odding
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
diff --git a/PKG-INFO b/PKG-INFO
index c294cb6..6044f09 100644
--- a/PKG-INFO
+++ b/PKG-INFO
@@ -1,6 +1,6 @@
Metadata-Version: 1.1
Name: coloredlogs
-Version: 5.2
+Version: 6.0
Summary: Colored terminal output for Python's logging module
Home-page: https://coloredlogs.readthedocs.io
Author: Peter Odding
@@ -60,14 +60,21 @@ Description: coloredlogs: Colored terminal output for Python's logging module
.. code-block:: python
+ import coloredlogs, logging
+
# Create a logger object.
- import logging
logger = logging.getLogger('your-module')
- # Initialize coloredlogs.
- import coloredlogs
+ # By default the install() function installs a handler on the root logger,
+ # this means that log messages from your code and log messages from the
+ # libraries that you use will all show up on the terminal.
coloredlogs.install(level='DEBUG')
+ # If you don't want to see log messages from libraries, you can pass a
+ # specific logger object to the install() function. In this case only log
+ # messages originating from that logger will show up on the terminal.
+ coloredlogs.install(level='DEBUG', logger=logger)
+
# Some examples.
logger.debug("this is a debugging message")
logger.info("this is an informational message")
@@ -113,7 +120,7 @@ Description: coloredlogs: Colored terminal output for Python's logging module
This software is licensed under the `MIT license`_.
- © 2015 Peter Odding.
+ © 2017 Peter Odding.
.. External references:
diff --git a/README.rst b/README.rst
index b44647a..36c9ee8 100644
--- a/README.rst
+++ b/README.rst
@@ -52,14 +52,21 @@ Here's an example of how easy it is to get started:
.. code-block:: python
+ import coloredlogs, logging
+
# Create a logger object.
- import logging
logger = logging.getLogger('your-module')
- # Initialize coloredlogs.
- import coloredlogs
+ # By default the install() function installs a handler on the root logger,
+ # this means that log messages from your code and log messages from the
+ # libraries that you use will all show up on the terminal.
coloredlogs.install(level='DEBUG')
+ # If you don't want to see log messages from libraries, you can pass a
+ # specific logger object to the install() function. In this case only log
+ # messages originating from that logger will show up on the terminal.
+ coloredlogs.install(level='DEBUG', logger=logger)
+
# Some examples.
logger.debug("this is a debugging message")
logger.info("this is an informational message")
@@ -105,7 +112,7 @@ License
This software is licensed under the `MIT license`_.
-© 2015 Peter Odding.
+© 2017 Peter Odding.
.. External references:
diff --git a/coloredlogs.egg-info/PKG-INFO b/coloredlogs.egg-info/PKG-INFO
index c294cb6..6044f09 100644
--- a/coloredlogs.egg-info/PKG-INFO
+++ b/coloredlogs.egg-info/PKG-INFO
@@ -1,6 +1,6 @@
Metadata-Version: 1.1
Name: coloredlogs
-Version: 5.2
+Version: 6.0
Summary: Colored terminal output for Python's logging module
Home-page: https://coloredlogs.readthedocs.io
Author: Peter Odding
@@ -60,14 +60,21 @@ Description: coloredlogs: Colored terminal output for Python's logging module
.. code-block:: python
+ import coloredlogs, logging
+
# Create a logger object.
- import logging
logger = logging.getLogger('your-module')
- # Initialize coloredlogs.
- import coloredlogs
+ # By default the install() function installs a handler on the root logger,
+ # this means that log messages from your code and log messages from the
+ # libraries that you use will all show up on the terminal.
coloredlogs.install(level='DEBUG')
+ # If you don't want to see log messages from libraries, you can pass a
+ # specific logger object to the install() function. In this case only log
+ # messages originating from that logger will show up on the terminal.
+ coloredlogs.install(level='DEBUG', logger=logger)
+
# Some examples.
logger.debug("this is a debugging message")
logger.info("this is an informational message")
@@ -113,7 +120,7 @@ Description: coloredlogs: Colored terminal output for Python's logging module
This software is licensed under the `MIT license`_.
- © 2015 Peter Odding.
+ © 2017 Peter Odding.
.. External references:
diff --git a/coloredlogs/__init__.py b/coloredlogs/__init__.py
index 3a5b077..af3827b 100644
--- a/coloredlogs/__init__.py
+++ b/coloredlogs/__init__.py
@@ -1,7 +1,7 @@
# Colored terminal output for Python's logging module.
#
# Author: Peter Odding <peter at peterodding.com>
-# Last Change: November 1, 2016
+# Last Change: March 10, 2017
# URL: https://coloredlogs.readthedocs.io
"""
@@ -121,6 +121,59 @@ following screen shot:
:align: center
:width: 80%
+.. _notes about log levels:
+
+Some notes about log levels
+===========================
+
+With regards to the handling of log levels, the :mod:`coloredlogs` package
+differs from Python's :mod:`logging` module in two aspects:
+
+1. While the :mod:`logging` module uses the default logging level
+ :data:`logging.WARNING`, the :mod:`coloredlogs` package has always used
+ :data:`logging.INFO` as its default log level.
+
+2. When logging to the terminal or system log is initialized by
+ :func:`install()` or :func:`.enable_system_logging()` the effective
+ level [#]_ of the selected logger [#]_ is compared against the requested
+ level [#]_ and if the effective level is more restrictive than the
+ requested level, the logger's level will be set to the requested level.
+ The reason for this is to work around a combination of design choices in
+ Python's :mod:`logging` module that can easily confuse people who aren't
+ already intimately familiar with it:
+
+ - All loggers are initialized with the level :data:`logging.NOTSET`.
+
+ - When a logger's level is set to :data:`logging.NOTSET` the
+ :func:`~logging.Logger.getEffectiveLevel()` method will
+ fall back to the level of the parent logger.
+
+ - The parent of all loggers is the root logger and the root logger has its
+ level set to :data:`logging.WARNING` by default (after importing the
+ :mod:`logging` module).
+
+ Effectively all user defined loggers inherit the default log level
+ :data:`logging.WARNING` from the root logger, which isn't very intuitive for
+ those who aren't already familiar with the hierarchical nature of the
+ :mod:`logging` module.
+
+ By avoiding this potentially confusing behavior (see `#14`_, `#18`_, `#21`_,
+ `#23`_ and `#24`_), while at the same time allowing the caller to specify a
+ logger object, my goal and hope is to provide sane defaults that can easily
+ be changed when the need arises.
+
+ .. [#] Refer to :func:`logging.Logger.getEffectiveLevel()` for details.
+ .. [#] The logger that is passed as an argument by the caller or the root
+ logger which is selected as a default when no logger is provided.
+ .. [#] The log level that is passed as an argument by the caller or the
+ default log level :data:`logging.INFO` when no level is provided.
+
+ .. _#14: https://github.com/xolox/python-coloredlogs/issues/14
+ .. _#18: https://github.com/xolox/python-coloredlogs/issues/18
+ .. _#21: https://github.com/xolox/python-coloredlogs/pull/21
+ .. _#23: https://github.com/xolox/python-coloredlogs/pull/23
+ .. _#24: https://github.com/xolox/python-coloredlogs/issues/24
+
Classes and functions
=====================
"""
@@ -151,7 +204,10 @@ if NEED_COLORAMA:
pass
# Semi-standard module versioning.
-__version__ = '5.2'
+__version__ = '6.0'
+
+DEFAULT_LOG_LEVEL = logging.INFO
+"""The default log level for :mod:`coloredlogs` (:data:`logging.INFO`)."""
DEFAULT_LOG_FORMAT = '%(asctime)s %(hostname)s %(name)s[%(process)d] %(levelname)s %(message)s'
"""The default log format for :class:`ColoredFormatter` objects (a string)."""
@@ -214,7 +270,7 @@ def install(level=None, **kw):
Enable colored terminal output for Python's :mod:`logging` module.
:param level: The default logging level (an integer or a string with a
- level name, defaults to :data:`logging.INFO`).
+ level name, defaults to :data:`DEFAULT_LOG_LEVEL`).
:param logger: The logger to which the stream handler should be attached (a
:class:`~logging.Logger` object, defaults to the root logger).
:param fmt: Set the logging format (a string like those accepted by
@@ -237,8 +293,11 @@ def install(level=None, **kw):
the previous configuration.
:param use_chroot: Refer to :class:`HostNameFilter`.
:param programname: Refer to :class:`ProgramNameFilter`.
- :param syslog: If :data:`True` then :func:`~coloredlogs.syslog.enable_system_logging()`
- will be called without arguments (defaults to :data:`False`).
+ :param syslog: If :data:`True` then :func:`.enable_system_logging()` will
+ be called without arguments (defaults to :data:`False`). The
+ `syslog` argument may also be a number or string, in this
+ case it is assumed to be a logging level which is passed on
+ to :func:`~coloredlogs.syslog.enable_system_logging()`.
The :func:`coloredlogs.install()` function is similar to
:func:`logging.basicConfig()`, both functions take a lot of optional
@@ -267,15 +326,20 @@ def install(level=None, **kw):
4. :func:`HostNameFilter.install()` and :func:`ProgramNameFilter.install()`
are called to enable the use of additional fields in the log format.
- 5. The formatter is added to the handler and the handler is added to the
- logger. The logger's level is set to :data:`logging.NOTSET` so that each
- handler gets to decide which records they filter. This makes it possible
- to have controllable verbosity on the terminal while logging at full
- verbosity to the system log or a file.
+ 5. If the logger's level is too restrictive it is relaxed (refer to `notes
+ about log levels`_ for details).
+
+ 6. The formatter is added to the handler and the handler is added to the
+ logger.
"""
logger = kw.get('logger') or logging.getLogger()
reconfigure = kw.get('reconfigure', True)
stream = kw.get('stream', sys.stderr)
+ # Get the log level from an argument, environment variable or default and
+ # convert the names of log levels to numbers to enable numeric comparison.
+ if level is None:
+ level = os.environ.get('COLOREDLOGS_LOG_LEVEL', DEFAULT_LOG_LEVEL)
+ level = level_to_number(level)
# Remove any existing stream handler that writes to stdout or stderr, even
# if the stream handler wasn't created by coloredlogs because multiple
# stream handlers (in the same hierarchy) writing to stdout or stderr would
@@ -287,9 +351,20 @@ def install(level=None, **kw):
# Make sure reconfiguration is allowed or not relevant.
if not (handler and not reconfigure):
# Make it easy to enable system logging.
- if kw.get('syslog', False):
- from coloredlogs import syslog
- syslog.enable_system_logging()
+ syslog_enabled = kw.get('syslog')
+ # Ignore False and None because None means the caller didn't opt in to
+ # system logging and False means the caller explicitly opted out of
+ # system logging.
+ if syslog_enabled not in (None, False):
+ from coloredlogs.syslog import enable_system_logging
+ if syslog_enabled is True:
+ # If the caller passed syslog=True then we leave the choice of
+ # default log level up to the coloredlogs.syslog module.
+ enable_system_logging()
+ else:
+ # Values other than (None, True, False) are assumed to
+ # represent a logging level for system logging.
+ enable_system_logging(level=syslog_enabled)
# Figure out whether we can use ANSI escape sequences.
use_colors = kw.get('isatty', None)
if use_colors or use_colors is None:
@@ -308,9 +383,7 @@ def install(level=None, **kw):
use_colors = terminal_supports_colors(stream)
# Create a stream handler.
handler = logging.StreamHandler(stream)
- if level is None:
- level = os.environ.get('COLOREDLOGS_LOG_LEVEL') or 'INFO'
- handler.setLevel(level_to_number(level))
+ handler.setLevel(level)
# Prepare the arguments to the formatter. The caller is
# allowed to customize `fmt' and/or `datefmt' as desired.
formatter_options = dict(fmt=kw.get('fmt'), datefmt=kw.get('datefmt'))
@@ -352,8 +425,10 @@ def install(level=None, **kw):
# Create a (possibly colored) formatter.
formatter_type = ColoredFormatter if use_colors else logging.Formatter
handler.setFormatter(formatter_type(**formatter_options))
+ # Adjust the level of the selected logger?
+ if logger.getEffectiveLevel() > level:
+ logger.setLevel(level)
# Install the stream handler.
- logger.setLevel(logging.NOTSET)
logger.addHandler(handler)
@@ -389,7 +464,7 @@ def is_verbose():
:returns: ``True`` if the root handler is verbose, ``False`` if not.
"""
- return get_level() < logging.INFO
+ return get_level() < DEFAULT_LOG_LEVEL
def get_level():
@@ -397,10 +472,10 @@ def get_level():
Get the logging level of the root handler.
:returns: The logging level of the root handler (an integer) or
- :data:`logging.INFO` (if no root handler exists).
+ :data:`DEFAULT_LOG_LEVEL` (if no root handler exists).
"""
handler, logger = find_handler(logging.getLogger(), match_stream_handler)
- return handler.level if handler else logging.INFO
+ return handler.level if handler else DEFAULT_LOG_LEVEL
def set_level(level):
@@ -465,7 +540,7 @@ def level_to_number(value):
value = defined_levels[value.upper()]
except KeyError:
# Don't fail on unsupported log levels.
- value = logging.INFO
+ value = DEFAULT_LOG_LEVEL
return value
diff --git a/coloredlogs/syslog.py b/coloredlogs/syslog.py
index 8faa1e7..21342ee 100644
--- a/coloredlogs/syslog.py
+++ b/coloredlogs/syslog.py
@@ -1,7 +1,7 @@
# Easy to use system logging for Python's logging module.
#
# Author: Peter Odding <peter at peterodding.com>
-# Last Change: November 14, 2015
+# Last Change: March 10, 2017
# URL: https://coloredlogs.readthedocs.io
"""
@@ -33,7 +33,13 @@ import socket
import sys
# Modules included in our package.
-from coloredlogs import ProgramNameFilter, find_program_name, replace_handler
+from coloredlogs import (
+ DEFAULT_LOG_LEVEL,
+ ProgramNameFilter,
+ find_program_name,
+ level_to_number,
+ replace_handler,
+)
LOG_DEVICE_MACOSX = '/var/run/syslog'
"""The pathname of the log device on Mac OS X (a string)."""
@@ -73,7 +79,6 @@ class SystemLogging(object):
"""
self.args = args
self.kw = kw
- self.silent = kw.pop('silent', False)
self.handler = None
def __enter__(self):
@@ -107,6 +112,9 @@ def enable_system_logging(programname=None, fmt=None, logger=None, reconfigure=T
:data:`DEFAULT_LOG_FORMAT`).
:param logger: The logger to which the :class:`~logging.handlers.SysLogHandler`
should be connected (defaults to the root logger).
+ :param level: The logging level for the :class:`~logging.handlers.SysLogHandler`
+ (defaults to :data:`.DEFAULT_LOG_LEVEL`). This value is coerced
+ using :func:`~coloredlogs.level_to_number()`.
:param reconfigure: If :data:`True` (the default) multiple calls to
:func:`enable_system_logging()` will each override
the previous configuration.
@@ -116,11 +124,15 @@ def enable_system_logging(programname=None, fmt=None, logger=None, reconfigure=T
is :data:`False` the existing handler object is returned. If the
connection to the system logging daemon fails :data:`None` is
returned.
+
+ .. note:: When the logger's effective level is too restrictive it is
+ relaxed (refer to `notes about log levels`_ for details).
"""
- # Remove the keyword arguments that we can handle.
+ # Provide defaults for omitted arguments.
programname = programname or find_program_name()
logger = logger or logging.getLogger()
fmt = fmt or DEFAULT_LOG_FORMAT
+ level = level_to_number(kw.get('level', DEFAULT_LOG_LEVEL))
# Check whether system logging is already enabled.
match_syslog_handler = lambda handler: isinstance(handler, logging.handlers.SysLogHandler)
handler, logger = replace_handler(logger, match_syslog_handler, reconfigure)
@@ -135,6 +147,9 @@ def enable_system_logging(programname=None, fmt=None, logger=None, reconfigure=T
# Connect the formatter, handler and logger.
handler.setFormatter(logging.Formatter(fmt))
logger.addHandler(handler)
+ # Adjust the level of the selected logger?
+ if logger.getEffectiveLevel() > level:
+ logger.setLevel(level)
return handler
@@ -146,8 +161,10 @@ def connect_to_syslog(address=None, facility=None, level=None):
daemon (a string or tuple, defaults to the result of
:func:`find_syslog_address()`).
:param facility: Refer to :class:`~logging.handlers.SysLogHandler`.
+ Defaults to ``LOG_USER``.
:param level: The logging level for the :class:`~logging.handlers.SysLogHandler`
- (defaults to :data:`logging.DEBUG` meaning nothing is filtered).
+ (defaults to :data:`.DEFAULT_LOG_LEVEL`). This value is coerced
+ using :func:`~coloredlogs.level_to_number()`.
:returns: A :class:`~logging.handlers.SysLogHandler` object or :data:`None` (if the
system logging daemon is unavailable).
@@ -163,17 +180,17 @@ def connect_to_syslog(address=None, facility=None, level=None):
default (which is UDP).
- If socket types are not supported Python's (2.6) defaults are used to
- connect to the given `address`.
+ connect to the selected `address`.
"""
if not address:
address = find_syslog_address()
if facility is None:
facility = logging.handlers.SysLogHandler.LOG_USER
if level is None:
- level = logging.DEBUG
+ level = DEFAULT_LOG_LEVEL
for socktype in socket.SOCK_RAW, socket.SOCK_STREAM, None:
kw = dict(facility=facility, address=address)
- if socktype:
+ if socktype is not None:
kw['socktype'] = socktype
try:
handler = logging.handlers.SysLogHandler(**kw)
@@ -184,7 +201,7 @@ def connect_to_syslog(address=None, facility=None, level=None):
# unavailable.
pass
else:
- handler.setLevel(level)
+ handler.setLevel(level_to_number(level))
return handler
diff --git a/setup.cfg b/setup.cfg
index 6c71b61..1e3eb36 100644
--- a/setup.cfg
+++ b/setup.cfg
@@ -4,5 +4,4 @@ universal = 1
[egg_info]
tag_build =
tag_date = 0
-tag_svn_revision = 0
--
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/python-modules/packages/python-coloredlogs.git
More information about the Python-modules-commits
mailing list