Documentation
¶
Overview ¶
Package lumber sends logs to Lumber, cloud logging with end-to-end encryption. Every log is encrypted with AES-256-GCM before it leaves the process, so Lumber's servers store only ciphertext plus each log's level.
client, err := lumber.New(os.Getenv("LUMBER_API_KEY"))
if err != nil {
log.Fatal(err)
}
defer client.Close(context.Background())
client.Info("Server started", "port", 8080)
slog.SetDefault(slog.New(client.Handler(nil)))
Logging never blocks: logs are queued and sent from a background goroutine. Close sends what's queued before the program exits.
Index ¶
- Constants
- type Client
- func (c *Client) Close(ctx context.Context) error
- func (c *Client) Debug(msg string, args ...any)
- func (c *Client) Error(msg string, args ...any)
- func (c *Client) Fatal(msg string, args ...any)
- func (c *Client) Flush(ctx context.Context) error
- func (c *Client) Handler(opts *HandlerOptions) slog.Handler
- func (c *Client) Info(msg string, args ...any)
- func (c *Client) Warn(msg string, args ...any)
- type HandlerOptions
- type Option
- func WithCaller(depth int) Option
- func WithEndpoint(url string) Option
- func WithEnvironment(env string) Option
- func WithHTTPClient(client *http.Client) Option
- func WithMaxQueue(n int) Option
- func WithTimeout(timeout time.Duration) Option
- func WithWarnings(warn func(message string)) Option
- func WithoutAutoContext() Option
Constants ¶
const DefaultEndpoint = "https://api.lumber.infinitetoken.com"
DefaultEndpoint is the Lumber API logs go to unless New is given WithEndpoint.
const LevelFatal = slog.Level(12)
LevelFatal is the slog level Lumber sends as FATAL: logger.Log(ctx, lumber.LevelFatal, "db down"). Any level from slog.LevelError up to it is ERROR.
const Version = "1.0.0"
Version is this package's version, sent in the User-Agent.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Client ¶
type Client struct {
// contains filtered or unexported fields
}
Client encrypts logs with an app's key and sends them to Lumber. It's safe for concurrent use.
func New ¶
New makes a client from an app's API key, `<token>.<encryptionKey>`, from the app's Secrets screen in Lumber. Only the token is sent to Lumber; the encryption key encrypts every log and never leaves this process.
func (*Client) Close ¶
Close sends what's queued, waiting until ctx is done at most, and stops. Logs after this are dropped.
func (*Client) Debug ¶
Debug logs at DEBUG. Its arguments are slog's: alternating keys and values, or slog.Attr. See Handler for the keys that mean something to Lumber.
func (*Client) Fatal ¶
Fatal logs at FATAL. Unlike log.Fatal it doesn't exit: call Close first if the program is about to.
func (*Client) Handler ¶
func (c *Client) Handler(opts *HandlerOptions) slog.Handler
Handler is a slog.Handler that sends records to Lumber:
logger := slog.New(client.Handler(nil))
logger.Info("Server started", "port", 8080)
Attributes go into the log's metadata, groups as nested objects, except three at the top level: request_id and trace_id (or requestId and traceId) are the log's own fields, and a "context" group or map goes into its context. An error value is sent as its message.
type HandlerOptions ¶
type HandlerOptions struct {
// Level is the lowest level sent. Nil means slog.LevelInfo.
Level slog.Leveler
// AddSource sends the file, line and function that logged in each log's context.
AddSource bool
}
HandlerOptions configure Client.Handler.
type Option ¶
type Option func(*options)
Option configures New.
func WithCaller ¶
WithCaller sends the file, line and function that logged with each of the client's own log calls, skipping depth more frames, one for each wrapper around the client. A Handler sends them with HandlerOptions.AddSource instead.
func WithEndpoint ¶
WithEndpoint sends logs to another Lumber API, for testing against another server.
func WithEnvironment ¶
WithEnvironment sends env (production, staging) with every log, as `env` in its context.
func WithHTTPClient ¶
WithHTTPClient sends logs with your own http.Client (a proxy, custom TLS). Its Timeout applies, not WithTimeout's.
func WithMaxQueue ¶
WithMaxQueue is how many logs can wait to send, while Lumber can't be reached, before new ones are dropped (10,000 unless set).
func WithTimeout ¶
WithTimeout is how long to wait for Lumber to answer one log (10 seconds unless set).
func WithWarnings ¶
WithWarnings receives the client's warnings (a log refused, Lumber unreachable) in place of the standard error output. Pass a function that does nothing to silence them.
func WithoutAutoContext ¶
func WithoutAutoContext() Option
WithoutAutoContext stops sending the process id and hostname with every log.