logrustash

package module
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Oct 30, 2022 License: MIT Imports: 5 Imported by: 197

README

Logstash hook for logrus

Build Status Go Report Status

Use this hook to send the logs to Logstash.

Usage

package main

import (
        "github.com/bshuster-repo/logrus-logstash-hook"
        "github.com/sirupsen/logrus"
        "net"
)

func main() {
        log := logrus.New()
        conn, err := net.Dial("tcp", "logstash.mycompany.net:8911")
        if err != nil {
                log.Fatal(err)
        }
        hook := logrustash.New(conn, logrustash.DefaultFormatter(logrus.Fields{"type": "myappName"}))

        log.Hooks.Add(hook)
        ctx := log.WithFields(logrus.Fields{
                "method": "main",
        })
        ctx.Info("Hello World!")
}

This is how it will look like:

{
    "@timestamp" => "2016-02-29T16:57:23.000Z",
      "@version" => "1",
         "level" => "info",
       "message" => "Hello World!",
        "method" => "main",
          "host" => "172.17.0.1",
          "port" => 45199,
          "type" => "myappName"
}

FAQ

Q: I would like to add characters to each line before sending to Logstash? A: Logrustash gives you the ability to mutate the message before sending it to Logstash. Just follow this example.

Q: Is there a way to maintain the connection when it drops A: It's recommended to use GoAutoSocket for that. See here how it can be done.

Maintainers

Name Github
Boaz Shuster boaz0

License

MIT.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DefaultFormatter added in v1.0.0

func DefaultFormatter(fields logrus.Fields) logrus.Formatter

DefaultFormatter returns a default Logstash formatter: A JSON format with "@version" set to "1" (unless set differently in `fields`, "type" to "log" (unless set differently in `fields`), "@timestamp" to the log time and "message" to the log message.

Note: to set a different configuration use the `LogstashFormatter` structure.

func New added in v1.0.0

func New(w io.Writer, f logrus.Formatter) logrus.Hook

New returns a new logrus.Hook for Logstash.

To create a new hook that sends logs to `tcp://logstash.corp.io:9999`:

conn, _ := net.Dial("tcp", "logstash.corp.io:9999") hook := logrustash.New(conn, logrustash.DefaultFormatter())

Types

type Hook

type Hook struct {
	// contains filtered or unexported fields
}

Hook represents a Logstash hook. It has two fields: writer to write the entry to Logstash and formatter to format the entry to a Logstash format before sending.

To initialize it use the `New` function.

func (Hook) Fire

func (h Hook) Fire(e *logrus.Entry) error

Fire takes, formats and sends the entry to Logstash. Hook's formatter is used to format the entry into Logstash format and Hook's writer is used to write the formatted entry to the Logstash instance.

func (Hook) Levels

func (h Hook) Levels() []logrus.Level

Levels returns all logrus levels.

type LogstashFormatter

type LogstashFormatter struct {
	logrus.Formatter
	logrus.Fields
}

LogstashFormatter represents a Logstash format. It has logrus.Formatter which formats the entry and logrus.Fields which are added to the JSON message if not given in the entry data.

Note: use the `DefaultFormatter` function to set a default Logstash formatter.

func (LogstashFormatter) Format

func (f LogstashFormatter) Format(e *logrus.Entry) ([]byte, error)

Format formats an entry to a Logstash format according to the given Formatter and Fields.

Note: the given entry is copied and not changed during the formatting process.

Jump to

Keyboard shortcuts

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