Skip to content

Comments

Add name attribute to BytesLogger#786

Open
veeceey wants to merge 2 commits intohynek:mainfrom
veeceey:fix/issue-734
Open

Add name attribute to BytesLogger#786
veeceey wants to merge 2 commits intohynek:mainfrom
veeceey:fix/issue-734

Conversation

@veeceey
Copy link

@veeceey veeceey commented Feb 8, 2026

Summary

  • Fixes AttributeError: 'BytesLogger' object has no attribute 'name' when using add_logger_name processor with BytesLoggerFactory (closes AttributeError: 'BytesLogger' object has no attribute 'name' #734)
  • Adds a name slot to BytesLogger and accepts it as a keyword-only argument in __init__
  • Updates BytesLoggerFactory.__call__ to forward the first positional argument (the logger name from get_logger()) to BytesLogger

Details

As reported in #734, using structlog.stdlib.add_logger_name with BytesLoggerFactory crashes because BytesLogger has no name attribute. The add_logger_name processor accesses logger.name when no _record is present in the event dict.

This implements the fix suggested by @hynek in #734 (comment) with one additional safety guard: BytesLoggerFactory.__call__ uses args[0] if args else None to avoid an IndexError when no positional arguments are passed to the factory.

Test plan

  • Added TestBytesLogger::test_name_attribute -- verifies BytesLogger accepts a name kwarg
  • Added TestBytesLogger::test_name_defaults_to_none -- verifies default is None
  • Added TestBytesLoggerFactory::test_passes_name_from_args -- verifies factory forwards name
  • Added TestBytesLoggerFactory::test_name_defaults_to_none -- verifies no-arg case
  • Added TestBytesLoggerFactory::test_extra_args_ignored -- verifies extra args beyond first are ignored
  • Added TestAddLoggerName::test_logger_name_added_with_bytes_logger -- end-to-end test with named BytesLogger
  • Added TestAddLoggerName::test_logger_name_none_with_unnamed_bytes_logger -- end-to-end test with unnamed BytesLogger
  • All existing tests pass (125 in test_output.py, 139 in test_stdlib.py)

veeceey and others added 2 commits February 13, 2026 19:38
BytesLogger lacked a `name` attribute, causing `add_logger_name` to
raise AttributeError when used with BytesLoggerFactory. The first
positional argument passed to BytesLoggerFactory (the logger name from
`get_logger()`) is now forwarded to BytesLogger as its `name` attribute.

Closes hynek#734

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@veeceey
Copy link
Author

veeceey commented Feb 19, 2026

Hi @hynek, friendly ping on this PR. It's been open for about 10 days now without any review activity. Would appreciate any feedback when you have a moment -- happy to adjust if anything needs changing. Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

AttributeError: 'BytesLogger' object has no attribute 'name'

1 participant