Skip to content

abramovic/logrus_influxdb

Repository files navigation

InfluxDB Hook for Logrus

Feel free to create an issue or send me a pull request if you have any questions, bugs, or suggestions for this library.

Thank you for creating issues and pull requests!

Usage

import (
  "time"
  "github.com/Sirupsen/logrus"
  "github.com/Abramovic/logrus_influxdb"
)
func main() {
  log    := logrus.New()

  config := &logrus_influxdb.Config{
    Host: "localhost",
    Port: 8086,
    Database: "logrus",
    UseHTTPS: false,
    Precision: "ns",
    Tags: []string{"tag1", "tag2"},
    BatchInterval: (5 * time.Second),
    BatchCount: 200, // set to "0" to disable batching
  }

  /*
    Use nil if you want to use the default configurations

    hook, err := logrus_influxdb.NewInfluxDB(nil)
  */

  hook, err := logrus_influxdb.NewInfluxDB(config)
  if err == nil {
    log.Hooks.Add(hook)
  }  
}

With an existing InfluxDB Client

If you wish to initialize a InfluxDB Hook with an already initialized InfluxDB client, you can use the NewWithClientInfluxDBHook constructor:

import (
	"github.com/Abramovic/logrus_influxdb"
	"github.com/Sirupsen/logrus"
	client "github.com/influxdata/influxdb/client/v2"
)

func main() {
	log := logrus.New()

	// In this example we will use the default configurations
	config := &logrus_influxdb.Config{
		Tags: []string{"tag1", "tag2"}, // use the following tags
	}

	// Connect to InfluxDB using the standard client.
	influxClient, _ := client.NewHTTPClient(client.HTTPConfig{
		Addr: "http://localhost:8086",
	})

	hook, err := logrus_influxdb.NewInfluxDB(config, influxClient)
	if err == nil {
		log.Hooks.Add(hook)
	}
}

In syslog format for chronograf log viewer

If you wish to push your logs in syslog format so you can view all logs in the chronograf log viewer.

import (
  "time"
  "github.com/Sirupsen/logrus"
  "github.com/Abramovic/logrus_influxdb"
)

func main() {
  log    := logrus.New()

  config := &logrus_influxdb.Config{
    Host: "localhost",
    Port: 8086,
    Database: "syslog", // set to syslog to view in logviewer
    UseHTTPS: false,
    Precision: "ns",
    Tags: []string{"tag1", "tag2"},
    BatchInterval: (5 * time.Second),
    BatchCount: 200, // set to "0" to disable batching
    Syslog:        true, // enable syslog format or not
    Facility:      "user", // see https://en.wikipedia.org/wiki/Syslog#Facility
    FacilityCode:  1, // see https://en.wikipedia.org/wiki/Syslog#Facility
    AppName:       "cb-scheduler", // app_name to use
    Version:       "1.0", // version of app
  }

  /*
    Use nil if you want to use the default configurations

    hook, err := logrus_influxdb.NewInfluxDB(nil)
  */

  hook, err := logrus_influxdb.NewInfluxDB(config)
  if err == nil {
    log.Hooks.Add(hook)
  }  
}

Behind the scenes

Database Handling

When passing an empty string for the InfluxDB database name, we default to "logrus" as the database name.

When initializing the hook we attempt to first see if the database exists. If not, then we try to create it for your automagically.

Message Field

We will insert your message into InfluxDB with the field message so please make sure not to use that name with your Logrus fields or else it will be overwritten.

Special Fields

Some logrus fields have a special meaning in this hook, these are server_name, logger and http_request (taken from Sentry Hook).

  • server_name (also known as hostname) is the name of the server which is logging the event (hostname.example.com)
  • logger is the part of the application which is logging the event
  • http_request is the in-coming request (*http.Request)

About

📝 InfluxDB Hook for Logrus

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages