Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 11 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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()`
Expand Down Expand Up @@ -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
```
Expand All @@ -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.
Expand All @@ -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
Expand All @@ -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
Expand Down
3 changes: 3 additions & 0 deletions flogging/flogging.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
):
Expand Down Expand Up @@ -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:
Expand Down
5 changes: 3 additions & 2 deletions flogging/tests/test_main.py
Original file line number Diff line number Diff line change
@@ -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)
2 changes: 1 addition & 1 deletion flogging/version.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
"""Current version of the project. Do not modify manually."""
__version__ = "0.0.25"
__version__ = "1.0.0"
6 changes: 3 additions & 3 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
Loading