Add LogMessage, GetBreadcrumbs, and EnableDebugLogging features
- Add LogMessage(level, title, message, data) for structured logging with title - Add LogMessageWithContext() context-aware variant - Add GetBreadcrumbs() public method to retrieve current breadcrumbs - Add EnableDebugLogging global variable for debug output control - Update all debug logging to respect the global debug flag 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
parent
09626831a2
commit
2777fba701
42
client.go
42
client.go
|
|
@ -9,6 +9,10 @@ import (
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// EnableDebugLogging is a global flag that enables debug logging for all clients.
|
||||||
|
// When enabled, all IronTelemetry clients will output debug information to stdout.
|
||||||
|
var EnableDebugLogging = false
|
||||||
|
|
||||||
// Client is the main IronTelemetry client
|
// Client is the main IronTelemetry client
|
||||||
type Client struct {
|
type Client struct {
|
||||||
mu sync.RWMutex
|
mu sync.RWMutex
|
||||||
|
|
@ -41,7 +45,7 @@ func New(opts Options) (*Client, error) {
|
||||||
}
|
}
|
||||||
client.journeys = NewJourneyManager(client)
|
client.journeys = NewJourneyManager(client)
|
||||||
|
|
||||||
if opts.Debug {
|
if opts.Debug || EnableDebugLogging {
|
||||||
fmt.Printf("[IronTelemetry] Initialized with DSN: %s\n", parsedDSN.APIBaseURL)
|
fmt.Printf("[IronTelemetry] Initialized with DSN: %s\n", parsedDSN.APIBaseURL)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -80,6 +84,38 @@ func (c *Client) CaptureMessageWithContext(ctx context.Context, message string,
|
||||||
return c.sendEvent(ctx, event)
|
return c.sendEvent(ctx, event)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// LogMessage logs a structured message with title, message, and optional data.
|
||||||
|
// Useful for structured logging that differentiates the log title from its details.
|
||||||
|
func (c *Client) LogMessage(level SeverityLevel, title string, message string, data map[string]any) SendResult {
|
||||||
|
return c.LogMessageWithContext(context.Background(), level, title, message, data)
|
||||||
|
}
|
||||||
|
|
||||||
|
// LogMessageWithContext logs a structured message with context
|
||||||
|
func (c *Client) LogMessageWithContext(ctx context.Context, level SeverityLevel, title, message string, data map[string]any) SendResult {
|
||||||
|
var fullMessage string
|
||||||
|
if message != "" {
|
||||||
|
fullMessage = fmt.Sprintf("%s: %s", title, message)
|
||||||
|
} else {
|
||||||
|
fullMessage = title
|
||||||
|
}
|
||||||
|
|
||||||
|
event := c.createEvent(level, fullMessage)
|
||||||
|
|
||||||
|
c.mu.Lock()
|
||||||
|
event.Extra["logTitle"] = title
|
||||||
|
if data != nil {
|
||||||
|
event.Extra["logData"] = data
|
||||||
|
}
|
||||||
|
c.mu.Unlock()
|
||||||
|
|
||||||
|
return c.sendEvent(ctx, event)
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetBreadcrumbs returns a copy of the current breadcrumbs list
|
||||||
|
func (c *Client) GetBreadcrumbs() []Breadcrumb {
|
||||||
|
return c.breadcrumbs.GetAll()
|
||||||
|
}
|
||||||
|
|
||||||
// AddBreadcrumb adds a breadcrumb
|
// AddBreadcrumb adds a breadcrumb
|
||||||
func (c *Client) AddBreadcrumb(message string, category BreadcrumbCategory) {
|
func (c *Client) AddBreadcrumb(message string, category BreadcrumbCategory) {
|
||||||
c.breadcrumbs.AddSimple(message, category)
|
c.breadcrumbs.AddSimple(message, category)
|
||||||
|
|
@ -237,7 +273,7 @@ func (c *Client) createEvent(level SeverityLevel, message string) *TelemetryEven
|
||||||
func (c *Client) sendEvent(ctx context.Context, event *TelemetryEvent) SendResult {
|
func (c *Client) sendEvent(ctx context.Context, event *TelemetryEvent) SendResult {
|
||||||
// Check sample rate
|
// Check sample rate
|
||||||
if c.opts.SampleRate < 1.0 && rand.Float64() > c.opts.SampleRate {
|
if c.opts.SampleRate < 1.0 && rand.Float64() > c.opts.SampleRate {
|
||||||
if c.opts.Debug {
|
if c.opts.Debug || EnableDebugLogging {
|
||||||
fmt.Printf("[IronTelemetry] Event sampled out: %s\n", event.EventID)
|
fmt.Printf("[IronTelemetry] Event sampled out: %s\n", event.EventID)
|
||||||
}
|
}
|
||||||
return SendResult{Success: true, EventID: event.EventID}
|
return SendResult{Success: true, EventID: event.EventID}
|
||||||
|
|
@ -247,7 +283,7 @@ func (c *Client) sendEvent(ctx context.Context, event *TelemetryEvent) SendResul
|
||||||
if c.opts.BeforeSend != nil {
|
if c.opts.BeforeSend != nil {
|
||||||
event = c.opts.BeforeSend(event)
|
event = c.opts.BeforeSend(event)
|
||||||
if event == nil {
|
if event == nil {
|
||||||
if c.opts.Debug {
|
if c.opts.Debug || EnableDebugLogging {
|
||||||
fmt.Println("[IronTelemetry] Event dropped by beforeSend hook")
|
fmt.Println("[IronTelemetry] Event dropped by beforeSend hook")
|
||||||
}
|
}
|
||||||
return SendResult{Success: true}
|
return SendResult{Success: true}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue