Table of Contents
An slog handler for logging to Elasticsearch.
Uses the the official go-elasticsearch package under the hood. In particular, it makes use of the bulk indexer helper provided in that package. Where appropriate, options for tuning the bulk indexer are exposed by the handler.
- Elasticsearch up and running.
- An index (or ideally, a logs data stream created for the logs in Elasticsearch.
go get github.com/immanent-tech/slog-elasticsearch/v2
type Option struct {
// Log level (default: debug)
Level slog.Leveler
// Connection to Elasticsearch
Conn *elasticsearch.Client
// Index/alias to use for logging.
Index string
// Optional: The number of workers. Defaults to runtime.NumCPU().
Numworkers int
// Optional: The flush threshold in bytes. Defaults to 5MB.
FlushBytes int
// Optional: The flush threshold as duration. Defaults to 30sec.
FlushInterval time.Duration
// Optional: customize json payload builder
Converter Converter
// Optional: custom marshaler
Marshaler func(v any) ([]byte, error)
// Optional: fetch attributes from context
AttrFromContext []func(ctx context.Context) []slog.Attr
// Optional: see slog.HandlerOptions
AddSource bool
ReplaceAttr func(groups []string, a slog.Attr) slog.Attr
}
Attributes will be injected in log payload.
Other global parameters:
slogelasticsearch.SourceKey = "source"
slogelasticsearch.ContextKey = "extra"
slogelasticsearch.ErrorKeys = []string{"error", "err"}
package main
import (
"fmt"
"log"
"log/slog"
"time"
"github.com/elastic/go-elasticsearch/v9"
slogelasticsearch "github.com/immanent-tech/slog-elasticsearch/v2"
)
func main() {
// Create the Elasticsearch client
//
es, err := elasticsearch.NewDefaultClient()
if err != nil {
log.Fatalf("Error creating the client: %s", err)
}
// Create a logger using the slog-elasticsearch handler.
//
logger := slog.New(slogelasticsearch.Option{
Level: slog.LevelDebug,
Conn: es,
Index: "test-logs",
}.NewElasticsearchHandler(context.Background()))
// Use the logger.
//
logger = logger.With("release", "v1.0.0")
logger.
With(
slog.Group("user",
slog.String("id", "user-123"),
slog.Time("created_at", time.Now().AddDate(0, 0, -1)),
),
).
With("environment", "dev").
With("error", fmt.Errorf("an error")).
Error("A message")
}
See the open issues for a list of proposed features (and known issues).
- Top Feature Requests (Add your votes using the π reaction)
- Top Bugs (Add your votes using the π reaction)
- Newest Bugs
Reach out to the maintainer at one of the following places:
If you want to say thank you or/and support active development of slog-elasticsearch:
- Add a GitHub Star to the project.
- Post on social media about slog-elasticsearch.
- Write interesting articles about the project on Dev.to, Medium or your personal blog.
Together, we can make slog-elasticsearch better!
First off, thanks for taking the time to contribute! Contributions are what make the open-source community such an amazing place to learn, inspire, and create. Any contributions you make will benefit everybody else and are greatly appreciated.
Please read our contribution guidelines, and thank you for being involved!
The original setup of this repository is by joshuar.
For a full list of all authors and contributors, see the contributors page.
slog-elasticsearch follows good practices of security, but 100% security cannot be assured. slog-elasticsearch is provided "as is" without any warranty. Use at your own risk.
For more information and to report security issues, please refer to our security documentation.
This project is licensed under the MIT license.
See LICENSE for more information.
slog-elasticsearch was original forked from and based on the code in slog-logstash. Many thanks go to the authors and contributors of that project for a great basis for slog-elasticsearch.