diff --git a/README.md b/README.md index 5b8eaab..bd8d7ee 100644 --- a/README.md +++ b/README.md @@ -27,13 +27,13 @@ The logging setup supports two main modes: To initialize logging in your application, call the `setup()` function. You can specify whether to enable structured logging or use the default human-readable format. ```python -from my_logging_module import setup +from flogging import flogging # Initialize logging -setup(level="INFO", structured=False) # Human-readable format +flogging.setup(level="INFO", structured=False) # Human-readable format # Enable structured logging for production -setup(level="INFO", structured=True) +flogging.setup(level="INFO", structured=True) ``` #### Parameters for `setup()` @@ -99,10 +99,10 @@ Example structured log output: In structured logging mode, you can attach additional context to each log message by calling `set_context()`. This context is logged alongside the usual fields, allowing you to track custom metadata. ```python -from my_logging_module import set_context +from flogging import flogging # Set custom context -set_context({"user_id": "12345", "transaction_id": "abcde"}) +flogging.set_context({"user_id": "12345", "transaction_id": "abcde"}) # The custom context will now appear in each structured log message ``` @@ -112,10 +112,11 @@ set_context({"user_id": "12345", "transaction_id": "abcde"}) When logging large messages (e.g., serialized data or files), the `log_multipart()` function compresses and splits the message into smaller chunks to prevent issues with log size limits. ```python -from my_logging_module import log_multipart +import logging +from flogging import flogging # Log a large message -log_multipart(logging.getLogger(), b"Large data to be logged") +flogging.log_multipart(logging.getLogger(), b"Large data to be logged") ``` This function will automatically split the message and log each chunk, ensuring the entire message is captured. @@ -140,9 +141,9 @@ You can further customize the format by modifying the `AwesomeFormatter` class, To enforce standards in your logging messages, such as preventing trailing dots in log messages, the module provides the `check_trailing_dot()` decorator. This can be applied to logging functions to raise an error if a message ends with a dot: ```python -from my_logging_module import check_trailing_dot +from flogging import flogging -@check_trailing_dot +@flogging.check_trailing_dot def log_message(record): # Your custom logging logic pass @@ -166,7 +167,7 @@ Here's a full example of how to use structured logging with command-line configu ```python import argparse import logging -from flogging import add_logging_args, set_context, setup +from flogging.flogging import add_logging_args, set_context, setup # Initialize logging setup(level="INFO", structured=False) # Human-readable format diff --git a/flogging/flogging.py b/flogging/flogging.py index 03c4538..e520278 100644 --- a/flogging/flogging.py +++ b/flogging/flogging.py @@ -189,6 +189,8 @@ class StructuredHandler(logging.Handler): """logging handler for structured logging.""" + default_fields = set(logging.LogRecord("", logging.NOTSET, "", 1, "msg", (), None).__dict__) + def __init__( self, level=logging.NOTSET, level_from_msg: Callable[[str], str | None] | None = None ): @@ -219,6 +221,7 @@ def emit(self, record: logging.LogRecord): # noqa: PLR0912 "thread": reduce_thread_id(record.thread), "name": record.name, } + obj.update((k, getattr(record, k)) for k in record.__dict__.keys() - self.default_fields) try: rank = os.environ["RANK"] except KeyError: diff --git a/flogging/tests/test_main.py b/flogging/tests/test_main.py index ef8b83b..db4b31a 100644 --- a/flogging/tests/test_main.py +++ b/flogging/tests/test_main.py @@ -1,7 +1,8 @@ import pytest -def test_main(): +@pytest.mark.parametrize("structured", [True, False]) +def test_main(structured): from flogging.flogging import setup as setup_logging - setup_logging(level="info", structured=False) + setup_logging(level="info", structured=structured) diff --git a/flogging/version.py b/flogging/version.py index 2f2de6a..13726a2 100644 --- a/flogging/version.py +++ b/flogging/version.py @@ -1,2 +1,2 @@ """Current version of the project. Do not modify manually.""" -__version__ = "0.0.25" +__version__ = "1.0.0" diff --git a/setup.py b/setup.py index e1b8540..31e10db 100644 --- a/setup.py +++ b/setup.py @@ -21,15 +21,15 @@ version=version.__version__, license="MIT", author="fragiletech", - author_email="", + author_email="gmarkhor@gmail.com", url="", - keywords=["Machine learning", "artificial intelligence"], + keywords=["logging"], tests_require=["pytest>=5.3.5", "hypothesis>=5.6.0"], extras_require={}, install_requires=["xxhash"], package_data={"": ["README.md"]}, classifiers=[ - "Development Status :: 3 - Alpha", + "Development Status :: 5 - Production/Stable", "Intended Audience :: Developers", "License :: OSI Approved :: MIT License", "Programming Language :: Python :: 3.10",