Skip to content

Commit

Permalink
0.1.1: The reserved size for the logger names auto-adjust to the long…
Browse files Browse the repository at this point in the history
…est active logger
  • Loading branch information
amazy committed Apr 5, 2024
1 parent 1dddee0 commit 7ba153d
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 12 deletions.
22 changes: 12 additions & 10 deletions demo.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,28 +4,30 @@

configure_logging()

logger = logging.getLogger("DEMO")

with LogContext("1ST"):
logging.info("my first message")
logger.info("my first message")
with LogContext("2ND"):
logging.info("my second message")
logger.info("my second message")


def worker(worker_id):
with LogContext(f"Worker {worker_id}"):
logging.info("my top level worker message")
logger.info("my top level worker message")

with LogContext("next-context"):
logging.warning("my warning")
logger.warning("my warning")

for i in range(0, 2):
thread = threading.Thread(target=worker, args=(i+1, ))
thread.start()


###### this shall output
# 2024-04-05 15:05:28,162 - root - INFO - 1ST | my first message
# 2024-04-05 15:05:28,162 - root - INFO - 1ST | 2ND | my second message
# 2024-04-05 15:05:28,163 - root - INFO - Worker 1 | my top level worker message
# 2024-04-05 15:05:28,163 - root - WARNING - Worker 1 | next-context | my warning
# 2024-04-05 15:05:28,163 - root - INFO - Worker 2 | my top level worker message
# 2024-04-05 15:05:28,163 - root - WARNING - Worker 2 | next-context | my warning
# 2024-04-05 16:41:09,429 - DEMO - INFO - 1ST | my first message
# 2024-04-05 16:41:09,430 - DEMO - INFO - 1ST | 2ND | my second message
# 2024-04-05 16:41:09,433 - DEMO - INFO - Worker 1 | my top level worker message
# 2024-04-05 16:41:09,433 - DEMO - WARNING - Worker 1 | next-context | my warning
# 2024-04-05 16:41:09,435 - DEMO - INFO - Worker 2 | my top level worker message
# 2024-04-05 16:41:09,436 - DEMO - WARNING - Worker 2 | next-context | my warning
4 changes: 3 additions & 1 deletion ot_logging_helpers/custom_formatter.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ def format(self, record):
# Add the context information to the log message
record.msg = f"{context_str}{record.msg}"

record.name = record.name[:10].ljust(10)
logger_names_max_length = max(len('root'), max([len(logger.name) for logger in logging.Logger.manager.loggerDict.values() if isinstance(logger, logging.Logger)]))

record.name = record.name[:logger_names_max_length].ljust(logger_names_max_length)
record.levelname = record.levelname[:8].ljust(8)

return super().format(record)
6 changes: 6 additions & 0 deletions release-notes.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
v 0.1.1
=========

- The reserved size for the logger names auto-adjust to the longest active logger.


v 0.1.0
=========

Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
# For a discussion on single-sourcing the version across setup.py and the
# project code, see
# https://packaging.python.org/guides/single-sourcing-package-version/
version='0.1.0', # Required
version='0.1.1', # Required

# This is a one-line description or tagline of what your project does. This
# corresponds to the "Summary" metadata field:
Expand Down

0 comments on commit 7ba153d

Please sign in to comment.