fleet/server/logging/webhook.go
Victor Lyuboslavsky 77eb458658
Migrated logging and google calendar files to use slog (#40541)
<!-- Add the related story/sub-task/bug number, like Resolves #123, or
remove if NA -->
**Related issue:** Resolves #40540 

# Checklist for submitter
- [ ] Changes file added for user-visible changes in `changes/`,
`orbit/changes/` or `ee/fleetd-chrome/changes`.
  - Changes present in previous PR

## Testing

- [x] Added/updated automated tests
- [x] QA'd all new/changed functionality manually


<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->
## Summary by CodeRabbit

* **Refactor**
* Switched the application logging to Go's standard slog with
context-aware logging, improving structured logs and observability
across services (status, audit, result, integrations).
* Replaced legacy logging implementations and updated runtime wiring to
propagate contextual loggers for more consistent, searchable log output.

* **Tests**
  * Updated test suites to use the new slog discard/logger setup.
<!-- end of auto-generated comment: release notes by coderabbit.ai -->
2026-02-26 12:48:54 -06:00

53 lines
1.1 KiB
Go

package logging
import (
"context"
"encoding/json"
"errors"
"fmt"
"log/slog"
"time"
"github.com/fleetdm/fleet/v4/server"
)
type webhookLogWriter struct {
url string
logger *slog.Logger
}
func NewWebhookLogWriter(webhookURL string, logger *slog.Logger) (*webhookLogWriter, error) {
if webhookURL == "" {
return nil, errors.New("webhook URL missing")
}
return &webhookLogWriter{
url: webhookURL,
logger: logger,
}, nil
}
type webhookPayload struct {
Timestamp time.Time `json:"timestamp"`
Details []json.RawMessage `json:"details"`
}
func (w *webhookLogWriter) Write(ctx context.Context, logs []json.RawMessage) error {
payload := webhookPayload{
Timestamp: time.Now(),
Details: logs,
}
w.logger.DebugContext(ctx, "sending webhook request",
"url", server.MaskSecretURLParams(w.url),
)
if err := server.PostJSONWithTimeout(ctx, w.url, payload, w.logger); err != nil {
w.logger.ErrorContext(ctx, fmt.Sprintf("failed to send automation webhook to %s", server.MaskSecretURLParams(w.url)),
"err", server.MaskURLError(err).Error(),
)
}
return nil
}