Skip to content

Commit

Permalink
3.4.5 - Add limit to ListLogger
Browse files Browse the repository at this point in the history
  • Loading branch information
vkottler committed Nov 1, 2024
1 parent 94302ac commit c11edcd
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 12 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/python-package.yml
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ jobs:
- run: |
mk python-release owner=vkottler \
repo=vcorelib version=3.4.4
repo=vcorelib version=3.4.5
if: |
matrix.python-version == '3.12'
&& matrix.system == 'ubuntu-latest'
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
=====================================
generator=datazen
version=3.1.4
hash=7ea5d02e721034df4d7213302d0c348b
hash=c13507ae97a348826959dff3baa92a94
=====================================
-->

# vcorelib ([3.4.4](https://pypi.org/project/vcorelib/))
# vcorelib ([3.4.5](https://pypi.org/project/vcorelib/))

[![python](https://img.shields.io/pypi/pyversions/vcorelib.svg)](https://pypi.org/project/vcorelib/)
![Build Status](https://github.com/vkottler/vcorelib/workflows/Python%20Package/badge.svg)
Expand Down
2 changes: 1 addition & 1 deletion local/variables/package.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
---
major: 3
minor: 4
patch: 4
patch: 5
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta:__legacy__"

[project]
name = "vcorelib"
version = "3.4.4"
version = "3.4.5"
description = "A collection of core Python utilities."
readme = "README.md"
requires-python = ">=3.12"
Expand Down
5 changes: 5 additions & 0 deletions tests/logging/test_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,8 @@ def test_list_logger_basic():

assert handler
assert handler.drain_str()

for idx in range(handler.max_size * 2):
logger.info("test %d", idx)

assert handler.drain_str()
4 changes: 2 additions & 2 deletions vcorelib/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# =====================================
# generator=datazen
# version=3.1.4
# hash=361158fa88de7e08204b5667614ce30d
# hash=85793ec0c221df2bb9a205d6eba2bf29
# =====================================

"""
Expand All @@ -10,7 +10,7 @@

DESCRIPTION = "A collection of core Python utilities."
PKG_NAME = "vcorelib"
VERSION = "3.4.4"
VERSION = "3.4.5"

# vcorelib-specific content.
DEFAULT_INDENT = 2
Expand Down
30 changes: 25 additions & 5 deletions vcorelib/logging/list.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

# built-in
import logging
from typing import Iterator

# third-party
from vcorelib.logging.args import DEFAULT_TIME_FORMAT
Expand All @@ -12,6 +13,9 @@
class ListLogger(logging.Handler):
"""An interface facilitating sending log messages to browser tabs."""

max_size: int = 2**10
dropped: int

log_messages: list[logging.LogRecord]

def drain(self) -> list[logging.LogRecord]:
Expand All @@ -21,18 +25,28 @@ def drain(self) -> list[logging.LogRecord]:
self.log_messages = []
return result

def drain_str(self) -> list[str]:
"""Drain formatted messages."""
def drain_str_iter(self) -> Iterator[str]:
"""Iterate over string messages."""

result = []
for record in self.drain():
result.append(
yield (
self.format(record)
# Respect 'external' logs that don't warrant full formatting.
if not getattr(record, "external", False)
else record.getMessage()
)
return result

# Create a message for drops.
if self.dropped:
yield (
f"(logger dropped {self.dropped} messages, "
f"max_size={self.max_size})"
)
self.dropped = 0

def drain_str(self) -> list[str]:
"""Drain formatted messages."""
return list(self.drain_str_iter())

def __bool__(self) -> bool:
"""Evaluate this instance as boolean."""
Expand All @@ -41,6 +55,11 @@ def __bool__(self) -> bool:
def emit(self, record: logging.LogRecord) -> None:
"""Send the log message."""

# Could do something with the lost messages at some point.
while len(self.log_messages) >= self.max_size:
self.log_messages.pop(0)
self.dropped += 1

self.log_messages.append(record)

@staticmethod
Expand All @@ -49,5 +68,6 @@ def create(fmt: str = DEFAULT_TIME_FORMAT) -> "ListLogger":

logger = ListLogger()
logger.log_messages = []
logger.dropped = 0
logger.setFormatter(logging.Formatter(fmt))
return logger

0 comments on commit c11edcd

Please sign in to comment.