Python logging library for augmenting log records with additional information
Project description
logaugment
==========
Python logging library for augmenting log records with additional information.
This library supports Python 2.7.x and Python 3.1+.
If you want custom keys in your logged string:
.. code:: python
formatter = logging.Formatter("%(message)s: %(custom_key)s")
then this library allows you to set them easily:
.. code:: python
logaugment.set(logger, custom_key='custom_value')
logger.warn("My message")
# My message: custom_value
This allows you to safely make logging calls without getting exceptions
that those keys are missing. See below if you wish to override the value
for a particular logging call.
Installation
============
You can install the library with pip:
.. code:: bash
$ pip install logaugment
Overview
========
This library provides three functions:
.. code:: python
# Sets the custom keys and values for the logger.
logaugment.set(logger, custom_key='custom_value')
# Adds custom keys and values in addition to those already set.
logaugment.add(logger, custom_key='custom_value')
# Removes all custom keys and values for the logger.
logaugment.reset(logger)
You can use `logaugment.set` to specify custom values for a given logger.
You can use `logaugment.add` to keep the custom values that were already
set and add new ones. If you're redefining a custom key / value then this
value will be used instead of the earlier value. You should not repeatedly
call `logaugment.add` just to change the value; in that case it's better
to use `logaugment.set`.
You can use `logaugment.reset` to remove all additional values that
were added using the `logaugment` library.
Why?
====
If you need to add custom keys to your Python logging strings you need to pass
them in with each logging call. That is inconvenient so this library allows you
to add values just once and they're then available for all logging calls
afterwards.
Here is a full example:
.. code:: python
import logging
import logaugment
logger = logging.getLogger()
handler = logging.StreamHandler()
formatter = logging.Formatter("%(message)s: %(custom_key)s")
handler.setFormatter(formatter)
logger.addHandler(handler)
logaugment.add(logger, custom_key='custom_value')
logger.warn("My message")
# My message: custom_value
Examples
========
You can use keywords to specify additional values:
.. code:: python
logaugment.add(logger, custom_key='custom_value')
logger.warn("My message")
# My message: custom_value
You can also use a dictionary or subclasses of collections.Mapping
to specify the keys / values:
.. code:: python
logaugment.add(logger, {'custom_key': 'custom_value'})
logger.warn("My message")
# My message: custom_value
You can also use a function which returns a dictionary:
.. code:: python
def process_record(record):
return {'custom_key': record.levelname}
logaugment.add(logger, process_record)
logger.warn("My message")
# My message: WARNING
You can pass an `extra` dictionary in the call which overrides the
augmented data:
.. code:: python
logaugment.add(logger, {'custom_key': 'custom_value'})
logger.warn("My message", extra={'custom_key': 'extra_value'})
# My message: extra_value
Changelog
=========
0.1.3 (September 28, 2014)
----------------
* Added check to ensure at least one argument is provided.
0.1.2 (September 18, 2014)
--------------------------
* Added logaugment.set which calls reset() and add() in order.
* Make logaugment.add work for all collections.Mapping instances.
0.1.1 (September 8, 2014)
-------------------------
* Fixed issue where most recent value was not used
in subsequent logaugment.add calls.
* Added logaugment.reset to undo all additions using logaugment.
0.1 (August 30, 2014)
---------------------
* Initial release.
==========
Python logging library for augmenting log records with additional information.
This library supports Python 2.7.x and Python 3.1+.
If you want custom keys in your logged string:
.. code:: python
formatter = logging.Formatter("%(message)s: %(custom_key)s")
then this library allows you to set them easily:
.. code:: python
logaugment.set(logger, custom_key='custom_value')
logger.warn("My message")
# My message: custom_value
This allows you to safely make logging calls without getting exceptions
that those keys are missing. See below if you wish to override the value
for a particular logging call.
Installation
============
You can install the library with pip:
.. code:: bash
$ pip install logaugment
Overview
========
This library provides three functions:
.. code:: python
# Sets the custom keys and values for the logger.
logaugment.set(logger, custom_key='custom_value')
# Adds custom keys and values in addition to those already set.
logaugment.add(logger, custom_key='custom_value')
# Removes all custom keys and values for the logger.
logaugment.reset(logger)
You can use `logaugment.set` to specify custom values for a given logger.
You can use `logaugment.add` to keep the custom values that were already
set and add new ones. If you're redefining a custom key / value then this
value will be used instead of the earlier value. You should not repeatedly
call `logaugment.add` just to change the value; in that case it's better
to use `logaugment.set`.
You can use `logaugment.reset` to remove all additional values that
were added using the `logaugment` library.
Why?
====
If you need to add custom keys to your Python logging strings you need to pass
them in with each logging call. That is inconvenient so this library allows you
to add values just once and they're then available for all logging calls
afterwards.
Here is a full example:
.. code:: python
import logging
import logaugment
logger = logging.getLogger()
handler = logging.StreamHandler()
formatter = logging.Formatter("%(message)s: %(custom_key)s")
handler.setFormatter(formatter)
logger.addHandler(handler)
logaugment.add(logger, custom_key='custom_value')
logger.warn("My message")
# My message: custom_value
Examples
========
You can use keywords to specify additional values:
.. code:: python
logaugment.add(logger, custom_key='custom_value')
logger.warn("My message")
# My message: custom_value
You can also use a dictionary or subclasses of collections.Mapping
to specify the keys / values:
.. code:: python
logaugment.add(logger, {'custom_key': 'custom_value'})
logger.warn("My message")
# My message: custom_value
You can also use a function which returns a dictionary:
.. code:: python
def process_record(record):
return {'custom_key': record.levelname}
logaugment.add(logger, process_record)
logger.warn("My message")
# My message: WARNING
You can pass an `extra` dictionary in the call which overrides the
augmented data:
.. code:: python
logaugment.add(logger, {'custom_key': 'custom_value'})
logger.warn("My message", extra={'custom_key': 'extra_value'})
# My message: extra_value
Changelog
=========
0.1.3 (September 28, 2014)
----------------
* Added check to ensure at least one argument is provided.
0.1.2 (September 18, 2014)
--------------------------
* Added logaugment.set which calls reset() and add() in order.
* Make logaugment.add work for all collections.Mapping instances.
0.1.1 (September 8, 2014)
-------------------------
* Fixed issue where most recent value was not used
in subsequent logaugment.add calls.
* Added logaugment.reset to undo all additions using logaugment.
0.1 (August 30, 2014)
---------------------
* Initial release.
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
logaugment-0.1.3.tar.gz
(4.3 kB
view details)
File details
Details for the file logaugment-0.1.3.tar.gz
.
File metadata
- Download URL: logaugment-0.1.3.tar.gz
- Upload date:
- Size: 4.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | e7af7b8aae3470f8497c39bd96e5bf564e7b9fd0932552e3ec73e0aca66830f0 |
|
MD5 | 7d0dcff87b7daef38a45e190ff08105b |
|
BLAKE2b-256 | 3e5b99d569af5fcc188ffd34f2cac0925820cf22966891897ce69a4aa5196241 |