Skip to content

Conversation

giortzisg
Copy link
Contributor

@giortzisg giortzisg commented Jun 27, 2025

Closes #1042. See issue for more details.

Description

Modifying existing API to make usage clearer. Introducing chaining of attributes:

  • Multiple levels Trace, Debug, Info, Warn, Error, Panic, Fatal that return a LogEntry
  • Attribute API on each LogEntry, to set String, Int, ...
  • WithCtx func to inject context to entry
  • Emit & Emitf functions to create a message and send the log

Example:

loggerWithVersion := sentry.NewLogger(ctx)

// Setting  attributes (these persist across log calls)
loggerWithVersion.SetAttributes(
    attribute.String("permanent.version", "1.0.0"),
)

// Add attributes to log entry
loggerWithVersion.Info().WithCtx(ctx).
    String("key.string", "value").
    Int("key.int", 42).
    Bool("key.bool", true).
    // don't forget to call Emit to send the logs to Sentry
    Emitf("Message with parameters %d and %d", 1, 2)

logger := sentry.NewLogger(ctx)

// you can also use Attributes for setting many attributes in the log entry (these are temporary)
logger.Info().
    Attributes(
	attribute.String("key.temp.string", "value"),
	attribute.Int("key.temp.int", 42),
    ).Emit("doesn't contain permanent.version")

Copy link

codecov bot commented Jun 27, 2025

Codecov Report

Attention: Patch coverage is 90.00000% with 24 lines in your changes missing coverage. Please review.

Project coverage is 86.87%. Comparing base (7b375b0) to head (259b489).
Report is 2 commits behind head on master.

Files with missing lines Patch % Lines
log.go 89.91% 12 Missing ⚠️
logrus/logrusentry.go 89.09% 6 Missing ⚠️
slog/sentryslog.go 90.38% 5 Missing ⚠️
slog/converter.go 92.85% 1 Missing ⚠️
Additional details and impacted files
@@           Coverage Diff            @@
##           master    #1046    +/-   ##
========================================
  Coverage   86.86%   86.87%            
========================================
  Files          55       55            
  Lines        5840     5950   +110     
========================================
+ Hits         5073     5169    +96     
- Misses        624      638    +14     
  Partials      143      143            

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@giortzisg giortzisg marked this pull request as ready for review June 30, 2025 09:52
@giortzisg giortzisg requested review from cleptric and AbhiPrasad June 30, 2025 09:52
cursor[bot]

This comment was marked as outdated.

cursor[bot]

This comment was marked as outdated.

cursor[bot]

This comment was marked as outdated.

Copy link
Member

@lcian lcian left a comment

Choose a reason for hiding this comment

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

Left some comments

Copy link
Member

@lcian lcian left a comment

Choose a reason for hiding this comment

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

Nice improvements, LGTM!

cursor[bot]

This comment was marked as outdated.

Copy link
Member

@AbhiPrasad AbhiPrasad left a comment

Choose a reason for hiding this comment

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

Good change!

cursor[bot]

This comment was marked as outdated.

cursor[bot]

This comment was marked as outdated.

log.go Outdated
}
if user.Email != "" {
attrs["user.email"] = Attribute{Value: user.Email, Type: "string"}
attrs["user.email"] = Attribute{Value: user.Email, Type: AttributeString}
}
}
}
if spanID.String() != "0000000000000000" {
Copy link
Member

Choose a reason for hiding this comment

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

How can this even happen?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

If there is no propagation context, then the spanID would be empty. So just emitting it in that case.

cursor[bot]

This comment was marked as outdated.

@cleptric cleptric added the Improvement Issue type label Jul 7, 2025
cursor[bot]

This comment was marked as outdated.

@cleptric cleptric merged commit 274de7b into master Jul 21, 2025
18 checks passed
@cleptric cleptric deleted the log-api branch July 21, 2025 13:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Improvement Issue type
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Modify sentryLogger API signature
4 participants