Skip to content

Add withLog() for logging alongside metrics - #28

Merged
jszobody merged 2 commits into
masterfrom
add-with-log
Feb 20, 2026
Merged

Add withLog() for logging alongside metrics#28
jszobody merged 2 commits into
masterfrom
add-with-log

Conversation

@jszobody

Copy link
Copy Markdown
Member

Summary

  • Adds Metric::withLog(string $message, string $level = 'info') to log a message when a metric is flushed
  • The metric's extra includes 'message' => $logMessage
  • The log context includes ['event' => $metricName] merged with the metric's extra
  • Logging happens at flush time via flushMetricLogs() so all extra data is finalized
  • Fixes PostHog driver to batch metrics like other drivers instead of sending immediately in add(), which broke fluent chaining (Metrics::create('event')->setExtra([...]))

Usage

Metrics::create("my_event")
    ->withLog("Something happened", "info")
    ->setExtra(['key' => 'value']);

Test plan

  • withLog() stores message and level, defaults to info
  • getExtra() includes message key when log is set
  • Log fires at flush time with correct level, message, and context
  • All 81 tests pass

Adds Metric::withLog() to log a message when a metric is flushed.
The log message is included in the metric's extra as 'message', and
the log context includes the metric's extra plus an 'event' key.

Also fixes PostHog to batch metrics like other drivers instead of
sending immediately in add(), which broke fluent chaining like
Metrics::create('event')->setExtra([...]).
@jszobody
jszobody merged commit e8b49d1 into master Feb 20, 2026
1 check passed
@jszobody
jszobody deleted the add-with-log branch February 20, 2026 22:22
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.

1 participant