Helpful code for logging in Python
Project description
powerflex-logging-utilities
Helpful code for logging in Python by PowerFlex.
Sample usage
Initializing Loggers
Setup JSON logging to stdout and to a file:
import logging
from typing import Optional
from powerflex_logging_utilities import (
JsonFormatter,
TraceLogger,
init_loggers,
)
LOG_LEVEL: str = "DEBUG"
FILE_LOG_LEVEL: Optional[str] = "TRACE"
LOG_FILE: Optional[str] = "./logs/trace.log"
MAX_LOG_FILE_MB = 10
MAX_TOTAL_LOG_FILE_MB = 10000
logger = TraceLogger("main")
third_party_loggers = ["asyncio", "backoff", "py.warnings"]
loggers = (logger, *[logging.getLogger(name) for name in third_party_loggers])
logging.captureWarnings(True)
init_loggers.init_loggers(
loggers,
log_level=LOG_LEVEL,
file_level=FILE_LOG_LEVEL,
filename=LOG_FILE,
max_bytes=1000 * 1000 * MAX_LOG_FILE_MB,
backup_count=MAX_TOTAL_LOG_FILE_MB // MAX_LOG_FILE_MB,
formatter=JsonFormatter,
info_logger=logger,
)
To use: logger.info("hello")
.
Using several other utilities
import logging
from powerflex_logging_utilities import (
forbid_toplevel_logging,
log_slow_callbacks,
)
logger = logging.getLogger(__name__)
# Log slow async callbacks with two log levels
log_slow_callbacks.log_slow_callbacks(logger)
# Forbid functions such as logging.info since they implicitly use the root logger
forbid_toplevel_logging.forbid_logging_with_logging_toplevel()
Using the JSON formatter
import logging
import sys
from powerflex_logging_utilities import JsonFormatter
log_handler = logging.StreamHandler(stream=sys.stdout)
log_handler.setLevel("DEBUG")
log_handler.setFormatter(JsonFormatter())
logger = logging.getLogger(__name__)
logger.addHandler(log_handler)
logger.setLevel("DEBUG")
logger.info("hello world", extra={
"data": ["log structured data", ":D"],
1: "handles non string key",
})
{
"message": "hello world",
"name": "__main__",
"module": "<ipython-input-10-b016ce80d46f>",
"lineno": 1,
"funcName": "<cell line: 1>",
"filename": "<ipython-input-10-b016ce80d46f>",
"asctime": "2022-05-12 01:04:16,824",
"data": [
"log structured data",
":D"
],
"severity": "INFO",
"1": "handles non string key"
}
Using pipenv
- Run
make setup-with-pipenv
to install all dependencies. Make sure you have the version of Python specified in.tool-versions
or simply change this file to your Python version (must be 3.8+). - Run
pipenv shell
or run the followingmake
commands withpipenv run make ...
. You could also aliaspmake
topipenv run make
for convenience.
Tests
There is 100% code coverage.
make test-unit
To test in several versions of Python, run:
tox
Testing the code in this README
make test-readme
Checking code quality
The Github Actions will run all of the following checks on the code.
Code formatting
make format-fix
Linting
make lint
Type checking
make type-check-strict
Releasing to pypi
- Make sure you have committed all code you wish to release.
- Make sure all code checks have passed.
- Set the version in
./src/powerflex_logging_utilities/VERSION
Please attempt to follow semantic versioning. - Run
make bump-version
- Run
make release
Project details
Release history Release notifications | RSS feed
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
Close
Hashes for powerflex-logging-utilities-1.1.0.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | d9e28f71aed52215391cbfe70ad7e6cf4b4b8452f90fbcb6f1e5aed0b29356f7 |
|
MD5 | cb5b84b67664a1f58a33fb2a8c3529df |
|
BLAKE2b-256 | a9bc034d27e7424cd66576cfe0d4b5adb0a8bc361527aec010c79610f30bfcd8 |
Close
Hashes for powerflex_logging_utilities-1.1.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 2e9e04e0756906fc91893ac76b3ce428f31c803bf247ac5dfb97fc4367f14687 |
|
MD5 | e41585e6b800effa63734984deb06358 |
|
BLAKE2b-256 | ab819030cf03d001361d89605742fb7456e96fd7a937502da9d246932103ab81 |