Skip to content

Add an InMemoryLogHandler#390

Merged
FranzBusch merged 10 commits intoapple:mainfrom
hamzahrmalik:in_memory_log_handler
Dec 2, 2025
Merged

Add an InMemoryLogHandler#390
FranzBusch merged 10 commits intoapple:mainfrom
hamzahrmalik:in_memory_log_handler

Conversation

@hamzahrmalik
Copy link
Contributor

Add an InMemoryLogHandler

Motivation:

Library maintainers should be able to test that their libraries log what they expect

Modifications:

Create a new InMemoryLogging product, which contains an InMemoryLogHandler

This log handler can be used to make a logger, pass the logger into some function, and then assert that logs were emitted.

See the tests for example usage

Future:

For now i have left this very bare-bones. We could in future add convenience functions. For example

  • a function to assert that a particular log message was logged
  • a static function to create a logger with this handler

@hamzahrmalik hamzahrmalik force-pushed the in_memory_log_handler branch 2 times, most recently from bd5f9c0 to 15c267c Compare December 1, 2025 10:29
@hamzahrmalik hamzahrmalik force-pushed the in_memory_log_handler branch from 15c267c to a57256c Compare December 1, 2025 10:38
@ktoso ktoso self-requested a review December 1, 2025 12:02
@ktoso ktoso added the 🆕 semver/minor Adds new public API. label Dec 1, 2025
level: level,
message: message,
metadata: metadata
)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

for completeness, wouldn't we want to also store the source/line? Maybe bad idea to assert on them but perhaps can be useful in debugging in some weird situations, not up to us to judge that I think 🤔

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could do. But then equating Entry's becomes hard, unelss you know what source to expect...

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We'd skip it from equatable and hashable impls -- lets see if anyone cares about it during review; in any case this is LGTM as well as is

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm happy to not store file/line for now in this handler.

Copy link
Member

@ktoso ktoso left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's a nice non-controversial addition and follows suit what we did in tracing https://github.com/apple/swift-distributed-tracing/tree/main/Sources/InMemoryTracing -- LGTM 👍

I also agree with keeping any assertions out of this, it's just an in memory handler, not specifically for testing.

Copy link
Member

@FranzBusch FranzBusch left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we do a small proposal for this since we have a proposal process setup now? Otherwise LGTM!

level: level,
message: message,
metadata: metadata
)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm happy to not store file/line for now in this handler.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we're not doing the proposal, no need to add the proposal .md either.

Copy link
Member

@FranzBusch FranzBusch left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just three doc nits

import Logging
import Synchronization

/// A custom log handler which doesn't actually emit logs, but just collects them into memory.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
/// A custom log handler which doesn't actually emit logs, but just collects them into memory.
/// A custom log handler which collects them into memory.

Comment on lines 19 to 20
/// You can then retrieve a list of what was logged and run assertions on it.
/// This handler is intended to be used in tests.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this log handler has more applications than just tests. Can we make this doc comment a bit more general purpose?

@FranzBusch FranzBusch merged commit ca53814 into apple:main Dec 2, 2025
56 checks passed
@samuelmurray
Copy link
Contributor

Doesn’t this close #242?

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

Labels

🆕 semver/minor Adds new public API.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants