Skip to content

Add name attribute to BytesLogger#786

Open
veeceey wants to merge 1 commit intohynek:mainfrom
veeceey:fix/issue-734
Open

Add name attribute to BytesLogger#786
veeceey wants to merge 1 commit 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
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!

@veeceey
Copy link
Author

veeceey commented Mar 10, 2026

bumping this — still interested in getting it merged if it looks good to you

@veeceey
Copy link
Author

veeceey commented Mar 12, 2026

checking in - is there anything else needed?

@hynek
Copy link
Owner

hynek commented Mar 14, 2026

hey sorry, for the delays. there're currently three things:

  1. your old buddy Claude is in the commit history

  2. I'm focusing on getting attrs 26.1.0 out rn

  3. I'm not 100% convinced I want this in.

    I've come exactly this far as you did, but then aborted because it feelt wrong to add such a hack to the code base. I was thinking to re-do how processors work (give them more execution context that also fixes the other code location problems around) but I will meditate over it further when attrs is out and shift my focus. This PR is blessedly small, but it's coupled to a much bigger design decision.

If you want to keep this mergeable: fix the history and don't ever import anything in test functions. I don't know why Claude loves doing that so much.

@veeceey
Copy link
Author

veeceey commented Mar 15, 2026

Hey, totally understand on all three points — no rush at all, get attrs out first!

Re: the commit history, yeah that's on me — I was experimenting with some tooling and didn't clean it up before pushing. I'll rebase and squash to fix that.

And I hear you on point 3. If you're thinking about a broader rework of how processors get execution context, this small patch might not be the right approach. Happy to close this if you'd rather tackle it as part of that larger redesign. Just let me know which way you want to go.

@veeceey
Copy link
Author

veeceey commented Mar 18, 2026

Done! Squashed into a single commit with a clean history — no more co-author tags. Also moved the BytesLogger imports in test_stdlib.py to the top-level import block instead of inside the test functions.

Totally understand on point 3 about the broader design direction. If you'd rather tackle the name attribute as part of a larger processor context rework, I'm happy to close this and let you drive that. Otherwise it's ready to go whenever you are.

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
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'

2 participants