Documentation
¶
Overview ¶
Package logsentry provides Sentry integration for golog structured logging. It implements the golog.Writer and golog.WriterConfig interfaces to bridge golog's logging capabilities with Sentry's error tracking and monitoring system.
Index ¶
- Variables
- func ContextWithoutLogging(parent context.Context) context.Context
- func IsContextWithoutLogging(ctx context.Context) bool
- type Writer
- func (w *Writer) BeginMessage(config golog.Config, timestamp time.Time, level golog.Level, ...)
- func (w *Writer) CommitMessage()
- func (w *Writer) String() string
- func (w *Writer) WriteBool(val bool)
- func (w *Writer) WriteError(val error)
- func (w *Writer) WriteFloat(val float64)
- func (w *Writer) WriteInt(val int64)
- func (w *Writer) WriteJSON(val []byte)
- func (w *Writer) WriteKey(key string)
- func (w *Writer) WriteNil()
- func (w *Writer) WriteSliceEnd()
- func (w *Writer) WriteSliceKey(key string)
- func (w *Writer) WriteString(val string)
- func (w *Writer) WriteUUID(val [16]byte)
- func (w *Writer) WriteUint(val uint64)
- type WriterConfig
Constants ¶
This section is empty.
Variables ¶
var ( // UnknownLevel is the Sentry level used when a golog.Level cannot be mapped // to a known Sentry level. This typically happens with custom log levels // that don't match the standard golog levels (TRACE, DEBUG, INFO, WARN, ERROR, FATAL). // Defaults to sentry.LevelError to ensure unknown levels are treated as errors. UnknownLevel = sentry.LevelError // FlushTimeout specifies how long to wait when flushing Sentry events // before giving up. This is used when the application is shutting down // to ensure pending events are sent to Sentry before termination. // Defaults to 3 seconds, which should be sufficient for most network conditions. FlushTimeout time.Duration = 3 * time.Second )
Functions ¶
func ContextWithoutLogging ¶
ContextWithoutLogging returns a new context derived from parent that disables Sentry logging for all log levels. When a logger uses this context, no log messages will be sent to Sentry regardless of their level.
This is useful for: - Testing scenarios where you don't want to pollute Sentry with test data - Background operations where Sentry logging is not desired - Sensitive operations where logging to external services should be avoided
The function is idempotent - if the parent context already has Sentry logging disabled, the same context is returned without modification.
Example:
ctx := logsentry.ContextWithoutLogging(context.Background())
logger.WithContext(ctx).Error("This won't go to Sentry").Log()
func IsContextWithoutLogging ¶
IsContextWithoutLogging checks whether the given context has Sentry logging disabled. It returns true if the context was created by ContextWithoutLogging or if it contains the same context key that disables Sentry logging.
This function is used internally by the WriterConfig to determine whether to skip sending log messages to Sentry for a given context.
Returns false if ctx is nil or if Sentry logging is not disabled.
Example:
ctx := logsentry.ContextWithoutLogging(context.Background())
if logsentry.IsContextWithoutLogging(ctx) {
// Sentry logging is disabled for this context
}
Types ¶
type Writer ¶
type Writer struct {
// contains filtered or unexported fields
}
Writer implements golog.Writer and handles the actual writing of log messages to Sentry. It accumulates log data during the logging process and sends it as a Sentry event when CommitMessage() is called.
Writer instances are designed to be reused through object pooling to minimize memory allocations and improve performance. Each Writer accumulates:
- Message text and timestamp
- Sentry level (mapped from golog level)
- Key-value pairs as Sentry event extra data
- Optional stack trace information
The Writer automatically maps golog levels to Sentry levels:
TRACE/DEBUG -> DEBUG, INFO -> INFO, WARN -> WARNING, ERROR -> ERROR, FATAL -> FATAL
Example usage through golog:
logger.Error("Database error").Str("query", sql).Err(err).Log()
// Creates Sentry event with level=ERROR, message="Database error",
// and extra data: {"query": sql, "error": err.Error()}
func (*Writer) BeginMessage ¶
func (*Writer) CommitMessage ¶
func (w *Writer) CommitMessage()
CommitMessage implements golog.Writer and finalizes the log message by sending it to Sentry as an event. This method is called at the end of each log operation after all data has been written.
The method creates a Sentry event with:
- The accumulated message text
- The mapped Sentry level
- The original timestamp
- All key-value pairs as extra data (from both config.extra and values)
- Optional stack trace (if enabled in Sentry options)
- A fingerprint based on the message for grouping
After sending the event, the Writer is reset and returned to the object pool for reuse, ensuring efficient memory management.
func (*Writer) WriteError ¶
func (*Writer) WriteFloat ¶
func (*Writer) WriteSliceEnd ¶
func (w *Writer) WriteSliceEnd()
func (*Writer) WriteSliceKey ¶
func (*Writer) WriteString ¶
type WriterConfig ¶
type WriterConfig struct {
// contains filtered or unexported fields
}
WriterConfig implements golog.WriterConfig and serves as a factory for creating Writer instances that send log messages to Sentry.
It maintains configuration for Sentry integration including the Sentry hub, message formatting, level filtering, and object pooling for efficient memory usage. WriterConfig instances are typically created once and reused across multiple log operations.
The config determines:
- Which Sentry project receives events (via hub)
- How messages are formatted (via format)
- Which log levels are sent to Sentry (via filter)
- Whether values appear in message text (via valsAsMsg)
- Additional metadata included with every event (via extra)
Example usage:
config := logsentry.NewWriterConfig(
sentry.CurrentHub(),
golog.NewDefaultFormat(),
golog.ErrorLevel().FilterOutBelow(),
false,
map[string]any{"service": "my-app"},
)
func NewWriterConfig ¶
func NewWriterConfig(hub *sentry.Hub, format *golog.Format, filter golog.LevelFilter, valsAsMsg bool, extra map[string]any) *WriterConfig
NewWriterConfig returns a new WriterConfig for a sentry.Hub. Any values passed as extra will be added to every log messsage.
func (*WriterConfig) FlushUnderlying ¶
func (c *WriterConfig) FlushUnderlying()