logkit-py
Logging helpers.
Installation
poetry add outcome-logkit
Usage
logkit is a wrapper around structlog that configures it with the following:
- Sets log level based on
APP_ENVenvironment variable - Automatically outputs Stackdriver-compliant JSON to stdout when running in a GCP environment (AppEngine, CloudRun, GKE, etc.)
- Intercepts all messages sent to the standard library loggers and processes them transparently
- Configures structlog to provide async-safe context values
Initialization
logkit needs to be initialized before being used. This initialization configures structlog and sets up the intercept for the standard logging library.
Note It's important to do this as early as possible in the program to ensure that no other imports start logging messages before the intercept has been configured. You can use # isort:skip to ensure isort doesn't reorder the import.
# Important that this happens before any other imports
from outcome.logkit import init_logging # isort:skip
init_logging() # isort:skip
Log Level
You can provide a level parameter to init_logging to define the default log-level. You can use the built-in log levels from the logging module (e.g. logging.INFO). If you don't provide a level, it will automatically be set based on the env.is_prod() method from the outcome-utils package.
import logging
init_logging(level=logging.INFO)
Custom Processors
You can provide an array of your own structlog processors to init_logging. They will be merged into the processors provided by logkit.
init_logging(processors=[my_custom_processor])
Logging
To log with logkit, you can either use the standard library logging, or use the structlog interface. Both can be used to pass structured data to the log entries. Using the structlog interface is marginally faster, since all the messages sent to the standard logging library are sent to structlog anyway.
import logging
from outcome.logkit import get_logger
# Using the standard library
logger = logging.getLogger(__name__)
logger.info('my_message', user_id='1')
# Using the structlog interface
structured_logger = get_logger(__name__)
structured_logger.info('my_message', user_id='1')
Async-safe context vars
You can set "global" variables that are async safe using outcome.logkit.context.
import logging
from outcome.logkit import get_logger, context
context.add(user_id='1')
structured_logger = get_logger(__name__)
structured_logger.info('my_message') # user_id=1 will be added to this log event
context.remove('user_id')
Testing
If you want to capture logs during your tests, you can use configure_structlog and log_output fixtures.
@pytest.mark.usefixtures('configure_structlog')
def test_log_output(log_ouput):
assert log_output.entries == []
# do something
assert log_output.entries == [...]
You can also define the captured log level or add custom processors thanks to these handy fixtures:
@pytest.fixture
def log_level():
return logging.DEBUG
@pytest.fixture
def log_processors(log_output):
my_custom_processor = foo
return [my_custom_processor, log_output]
@pytest.mark.usefixtures('configure_structlog')
def test_log_output(log_ouput):
assert log_output.entries == []
# do something
assert log_output.entries == [...]
Development
Remember to run ./bootstrap.sh when you clone the repository.
Metadata
Release files for outcome-logkit 1.2.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| outcome-logkit-1.2.0.tar.gz | 11.4 kB | Without provenance | Details for outcome-logkit-1.2.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| outcome_logkit-1.2.0-py3-none-any.whl | Python 3 | none | any | Details for outcome_logkit-1.2.0-py3-none-any.whl |
Total release size: 23.7 kB
Release files / outcome-logkit-1.2.0.tar.gz
| Download URL | outcome-logkit-1.2.0.tar.gz |
|---|---|
| Size | 11.4 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
a7ae2777f355eda448a9c8bed98ed345d94f930568cf7ed9f5fe8b9456a18211
|
|
BLAKE2b-256 checksum How to use checksums |
aa9d48c7abd8ca54f91529eaf9dfaf4db5c7aaf7e0db0f8fdea1fce51428b5df
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/1.1.5 CPython/3.8.6 Darwin/19.6.0
|
Release files / outcome_logkit-1.2.0-py3-none-any.whl
| Download URL | outcome_logkit-1.2.0-py3-none-any.whl |
|---|---|
| Size | 12.4 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
70074ace5160dc525bb20a175ae50dbfbd51d11c77e8c8c195264de6e2dbf833
|
|
BLAKE2b-256 checksum How to use checksums |
0d953a5057c8beaabf257d1b8676115a534a8a9f0e704130ed5df55b90495a22
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/1.1.5 CPython/3.8.6 Darwin/19.6.0
|
Security for outcome-logkit 1.2.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.