Logging utilities to help you over-communicate
Project description
logquacious
Logquacious is a set of simple logging utilities to help you over-communicate. (Logorrhea would’ve been a good name, if it didn’t sound so terrible.)
Good application logging is easy to overlook, until you have to debug an error in production. Logquacious aims to make logging as easy as possible.
Quick start
To get started, you’ll need set up logging for your application. For this example, we’ll use a really simple configuration:
import logging
logging.basicConfig(format='[%(levelname)s] %(message)s', level=logging.DEBUG)
The main interface to logquacious is the LogManager, which can be used for normal logging:
import logquacious
log = logquacious.LogManager(__name__)
log.debug('Nothing to see here.')
Due to our simplified logging format defined earlier, that would output:
[DEBUG] Nothing to see here.
That isn’t a very interesting example. In addition to basic logging, LogManager has a context attribute for use as a context manager:
with log.context.debug('greetings'):
print('Hello!')
[DEBUG] Start greetings
Hello!
[DEBUG] Finish greetings
The same attribute can be used as a decorator, as well:
@log.context.info
def divide(numerator, denominator):
if denominator == 0:
log.warn('Attempted division by zero. Returning None')
return None
return numerator / denominator
divide(1, 0)
[INFO] Start divide
[WARNING] Attempted division by zero. Returning None
[INFO] Finish divide
There’s also a special context manager for suppressing errors and logging:
with log.and_suppress(ValueError, msg="It's ok, mistakes happen"):
raise ValueError('Test error')
[ERROR] It's ok, mistakes happen
Traceback (most recent call last):
File "/Users/tyu/code/logquacious/logquacious/log_manager.py", line 103, in and_suppress
yield
File "scripts/example.py", line 26, in <module>
raise ValueError('Test error')
ValueError: Test error
Configuration
The message templates used by LogManager.context can be configured to your liking by passing a context_templates argument to LogManager:
log = logquacious.LogManager(__name__, context_templates={
'context.start': '=============== Enter {label} ===============',
'context.finish': '=============== Exit {label} ===============',
})
with log.context.debug('greetings'):
print('Hello!')
[DEBUG] =============== Enter greetings ===============
Hello!
[DEBUG] =============== Exit greetings ===============
The general format for context_templates keys is:
[CONTEXT_TYPE.]('start'|'finish')[.LOG_LEVEL_NAME]
where square-brackes marks optional fields.
CONTEXT_TYPE can be any of the following:
function: Template used when called as a decorator.
context: Template used when called as a context manager.
LOG_LEVEL_NAME can be any of the following logging levels:
DEBUG
INFO
WARNING
ERROR
CRITICAL
For example, consider the cascade graph for function.start.DEBUG, which looks like:
function.start.DEBUG / \ start.DEBUG function.start \ / start
The cascade is performed using a breadth-first search. If function.start.DEBUG is not defined, check start.DEBUG then check function.start BEFORE checking start.
Credits
This package was created with Cookiecutter and the audreyr/cookiecutter-pypackage project template.
History
0.1.0 (2018-09-06)
First release on PyPI.
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
Built Distribution
Hashes for logquacious-0.1.0-py2.py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 00bc4beb4ec981204b7967d31399962fdf30ff4817df4b0708f8dd20bcbc1a91 |
|
MD5 | 2ce82d63a2e9094b414003bae9e73a13 |
|
BLAKE2b-256 | 54f399d5e1ab52453cb0fb7fce7c575d0f1b25511cc6b78e2918b3ac930d8ae8 |