Documentation
¶
Index ¶
- Variables
- type Logdash
- type Logger
- func (l *Logger) Close() error
- func (l *Logger) Debug(args ...any)
- func (l *Logger) DebugF(format string, args ...any)
- func (l *Logger) Error(args ...any)
- func (l *Logger) ErrorF(format string, args ...any)
- func (l *Logger) HTTP(args ...any)
- func (l *Logger) HTTPF(format string, args ...any)
- func (l *Logger) Info(args ...any)
- func (l *Logger) InfoF(format string, args ...any)
- func (l *Logger) Log(args ...any)
- func (l *Logger) LogF(format string, args ...any)
- func (l *Logger) Shutdown(ctx context.Context) error
- func (l *Logger) Silly(args ...any)
- func (l *Logger) SillyF(format string, args ...any)
- func (l *Logger) Verbose(args ...any)
- func (l *Logger) VerboseF(format string, args ...any)
- func (l *Logger) Warn(args ...any)
- func (l *Logger) WarnF(format string, args ...any)
- type Metrics
- type Option
- func WithAPIKey(apiKey string) Option
- func WithBufferSize(size int) Option
- func WithHTTPRetries(retries int) Option
- func WithHTTPRetryMax(max time.Duration) Option
- func WithHTTPRetryMin(min time.Duration) Option
- func WithHTTPTimeout(timeout time.Duration) Option
- func WithHost(host string) Option
- func WithOverflowPolicy(policy OverflowPolicy) Option
- func WithVerbose() Option
- type OverflowPolicy
- type SlogTextHandler
Constants ¶
This section is empty.
Variables ¶
var (
// DefaultBufferSize is the default size of the buffer for the async queue.
DefaultBufferSize = 128
)
var ErrAlreadyClosed = errors.New("already closed or shutting down")
Functions ¶
This section is empty.
Types ¶
type Logdash ¶
type Logdash struct { // Logger is the logger used to log messages to the Logdash server. // // If no API key is provided, the Logdash will not send any logs to the server. // Logging to the console is always enabled. Logger *Logger // Metrics is the metrics object used to track metrics. // // If no API key is provided, the Logdash will not send any metrics to the server. Metrics Metrics // contains filtered or unexported fields }
Logdash is the main object exposing the Logdash API.
func New ¶
New creates a new Logdash instance with the given options.
By default, the Logdash will use the Logdash API at https://api.logdash.io.
If no API key is provided, the Logdash will not send any logs or metrics to the server. Logging to the console is always enabled.
The default buffer size is 128 (see: DefaultBufferSize).
The default overflow policy is OverflowPolicyDrop, to avoid blocking the logging thread. For preserving logs in case of overflow, use WithOverflowPolicy to set OverflowPolicyBlock.
The default HTTP settings are:
- timeout: 5 seconds (see: WithHTTPTimeout).
- retries: 3 (see: WithHTTPRetries).
- retry minimum interval: 1 second (see: WithHTTPRetryMin).
- retry maximum interval: 30 seconds (see: WithHTTPRetryMax).
type Logger ¶
type Logger struct {
// contains filtered or unexported fields
}
Logger is a struct that provides logging functionality.
This is created internally as a part of the Logdash object and accessed via the [Logdash.Logger] field.
type Metrics ¶
type Metrics interface { // Set sets a metric to an absolute value. Set(name string, value float64) // Mutate changes a metric by a relative value. Mutate(name string, value float64) // contains filtered or unexported methods }
Metrics defines the interface for metrics functionality.
This is created internally as a part of the Logdash object and accessed via the [Logdash.Metrics] field.
type Option ¶
type Option func(*options)
Option is a function that configures a Logdash instance.
func WithAPIKey ¶
WithAPIKey sets the API key for the Logdash server.
func WithBufferSize ¶
WithBufferSize sets the size of the buffer for the async queue.
func WithHTTPRetries ¶
WithHTTPRetries sets the number of retries for HTTP requests.
func WithHTTPRetryMax ¶
WithHTTPRetryMax sets the maximum duration for HTTP retries.
func WithHTTPRetryMin ¶
WithHTTPRetryMin sets the minimum duration for HTTP retries.
func WithHTTPTimeout ¶
WithHTTPTimeout sets the timeout for HTTP requests.
func WithOverflowPolicy ¶
func WithOverflowPolicy(policy OverflowPolicy) Option
WithOverflowPolicy sets how to handle log overflow.
func WithVerbose ¶
func WithVerbose() Option
WithVerbose enables verbose logging.
This is useful for debugging, showing internal logs and changes in the metrics.
type OverflowPolicy ¶
type OverflowPolicy int
OverflowPolicy defines how to handle log overflow.
const ( // OverflowPolicyDrop drops new logs when the internal buffer is full. // // This is the default behavior. OverflowPolicyDrop OverflowPolicy = iota // OverflowPolicyBlock blocks when the internal buffer is full. // // This is useful when you want to preserve logs even when the internal buffer is full. OverflowPolicyBlock )
type SlogTextHandler ¶
type SlogTextHandler struct {
// contains filtered or unexported fields
}
SlogTextHandler is a slog.Handler that logs to Logdash.
It mimics slog.TextHandler behavior, but logs to Logdash instead of io.Writer.
slog.HandlerOptions are fully supported.
Basic mapping between slog.Level and [logdash.logLevel] is:
- slog.LevelDebug (-4) → logdash.Debug
- slog.LevelInfo (0) → logdash.Info
- slog.LevelWarn (4) → logdash.Warn
- slog.LevelError (8) → logdash.Error
Since slog.Level is an integer type, the mapping handles any intermediate or custom level values:
- Levels < slog.LevelDebug (-4) → logdash.Silly
- Levels ≥ slog.LevelDebug (-4) and < slog.LevelInfo (0) → logdash.Debug
- Levels ≥ slog.LevelInfo (0) and < slog.LevelWarn (4) → logdash.Info
- Levels ≥ slog.LevelWarn (4) and < slog.LevelError (8) → logdash.Warn
- Levels ≥ slog.LevelError (8) → logdash.Error
If you want to log with a custom level, you can use slog.Level directly.
func NewSlogTextHandler ¶
func NewSlogTextHandler(logger *Logger, opts slog.HandlerOptions) *SlogTextHandler
NewSlogTextHandler creates a new SlogTextHandler with the given Logger and slog.HandlerOptions.