zaptail

package module
v0.1.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 12, 2026 License: MIT Imports: 8 Imported by: 0

README

Zaptail

A Go middleware for Uber's Zap logger that sends logs to BetterStack Logtail.

Installation

go get github.com/BrunoKrugel/zaptail

Usage

package main

import (
	"time"

	"github.com/BrunoKrugel/zaptail"
	"go.uber.org/zap"
	"go.uber.org/zap/zapcore"
)

func main() {
	encoderConfig := zap.NewProductionEncoderConfig()
	encoder := zapcore.NewJSONEncoder(encoderConfig)

	config := zaptail.Config{
		APIKey:        "your-logtail-api-key",
		BatchSize:     100,
		FlushInterval: 5 * time.Second,
	}

	logtailCore := zaptail.NewCore(encoder, zapcore.InfoLevel, config)
	defer logtailCore.Close()

	logger := zap.New(logtailCore)
	defer logger.Sync()

	logger.Info("Hello from Zaptail!",
		zap.String("environment", "production"),
		zap.Int("user_id", 12345),
	)

	logger.Error("An error occurred",
		zap.Error(fmt.Errorf("something went wrong")),
		zap.String("context", "user authentication"),
	)
}

Configuration

The Config struct accepts the following parameters:

  • APIKey (string, required): Your BetterStack Logtail API key
  • BatchSize (int, optional): Number of logs to batch before sending (default: 100)
  • FlushInterval (time.Duration, optional): Time interval to flush logs (default: 5 seconds)
  • HTTPClient (*http.Client, optional): Custom HTTP client (default: 10-second timeout client)

Combining with Console Output

You can combine Zaptail with console output using zapcore.NewTee:

package main

import (
	"os"
	"time"

	"github.com/BrunoKrugel/zaptail"
	"go.uber.org/zap"
	"go.uber.org/zap/zapcore"
)

func main() {
	encoderConfig := zap.NewProductionEncoderConfig()
	encoder := zapcore.NewJSONEncoder(encoderConfig)

	consoleCore := zapcore.NewCore(
		zapcore.NewConsoleEncoder(encoderConfig),
		zapcore.AddSync(os.Stdout),
		zapcore.DebugLevel,
	)

	logtailCore := zaptail.NewCore(encoder, zapcore.InfoLevel, zaptail.Config{
		APIKey: "your-logtail-api-key",
	})
	defer logtailCore.Close()

	core := zapcore.NewTee(consoleCore, logtailCore)
	logger := zap.New(core)
	defer logger.Sync()

	logger.Info("This log goes to both console and Logtail")
}

Features

  • Batched log sending for better performance
  • Automatic periodic flushing
  • Configurable batch size and flush interval
  • Thread-safe operations
  • Graceful shutdown with Close()

License

MIT

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Config

type Config struct {
	HTTPClient    *http.Client
	APIKey        string
	LogtailURL    string
	BatchSize     int
	FlushInterval time.Duration
}

type Core

type Core struct {
	zapcore.LevelEnabler
	// contains filtered or unexported fields
}

func NewCore

func NewCore(enc zapcore.Encoder, enab zapcore.LevelEnabler, config Config) *Core

func (*Core) Check

func (c *Core) Check(entry zapcore.Entry, ce *zapcore.CheckedEntry) *zapcore.CheckedEntry

func (*Core) Close

func (c *Core) Close() error

func (*Core) Sync

func (c *Core) Sync() error

func (*Core) With

func (c *Core) With(fields []zapcore.Field) zapcore.Core

func (*Core) Write

func (c *Core) Write(entry zapcore.Entry, fields []zapcore.Field) error

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL