Skip to content

immanent-tech/slog-elasticsearch

Β 
Β 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

84 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
slog-elasticsearch


Report a Bug Β· Request a Feature . Ask a Question

Project license

Pull Requests welcome code with love by immanent-tech

Table of Contents

About

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.

Getting Started

Prerequisites

  • Elasticsearch up and running.
  • An index (or ideally, a logs data stream created for the logs in Elasticsearch.

Installation

go get github.com/immanent-tech/slog-elasticsearch/v2

Usage

Handler options

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"}

Example

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")
}

Roadmap

See the open issues for a list of proposed features (and known issues).

Support

Reach out to the maintainer at one of the following places:

Project assistance

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!

Contributing

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!

Authors & contributors

The original setup of this repository is by joshuar.

For a full list of all authors and contributors, see the contributors page.

Security

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.

License

This project is licensed under the MIT license.

See LICENSE for more information.

Acknowledgements

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.

Releases

No releases published

Packages

No packages published

Languages

  • Go 86.6%
  • Makefile 13.4%