logrus_influxdb

package module
v0.0.0-...-ec7855d Latest Latest
Warning

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

Go to latest
Published: Dec 25, 2019 License: MIT Imports: 8 Imported by: 10

README

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.

Contributors

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)

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Config

type Config struct {
	// InfluxDB Configurations
	Host      string        `json:"influxdb_host"`
	Port      int           `json:"influxdb_port"`
	Timeout   time.Duration `json:"influxdb_timeout"`
	Database  string        `json:"influxdb_database"`
	Username  string        `json:"influxdb_username"`
	Password  string        `json:"influxdb_password"`
	UseHTTPS  bool          `json:"influxdb_https"`
	Precision string        `json:"influxdb_precision"`

	// Enable syslog format for chronograf logviewer usage
	Syslog       bool   `json:"syslog_enabled"`
	Facility     string `json:"syslog_facility"`
	FacilityCode int    `json:"syslog_facility_code"`
	AppName      string `json:"syslog_app_name"`
	Version      string `json:"syslog_app_version"`

	// Minimum level for push
	MinLevel string `json:"syslog_min_level"`

	// Logrus tags
	Tags []string `json:"logrus_tags"`

	// Defaults
	Measurement string `json:"measurement"`

	// Batching
	BatchInterval time.Duration `json:"batch_interval"` // Defaults to 5s.
	BatchCount    int           `json:"batch_count"`    // Defaults to 200.
}

Config handles InfluxDB configuration, Logrus tags and batching inserts to InfluxDB

type InfluxDBHook

type InfluxDBHook struct {
	sync.Mutex // TODO: we should clean up all of these locks
	// contains filtered or unexported fields
}

InfluxDBHook delivers logs to an InfluxDB cluster.

func NewInfluxDB

func NewInfluxDB(config *Config, clients ...influxdb.Client) (hook *InfluxDBHook, err error)

NewInfluxDB returns a new InfluxDBHook.

func NewInfluxDBHook

func NewInfluxDBHook(host, database string, tags []string, batching ...bool) (hook *InfluxDBHook, err error)

NewInfluxDBHook /* DO NOT USE */ creates a hook to be added to an instance of logger and initializes the InfluxDB client

func NewWithClientInfluxDBHook

func NewWithClientInfluxDBHook(host, database string, tags []string, client influxdb.Client, batching ...bool) (hook *InfluxDBHook, err error)

NewWithClientInfluxDBHook /* DO NOT USE */ creates a hook and uses the provided influxdb client

func (*InfluxDBHook) Fire

func (hook *InfluxDBHook) Fire(entry *logrus.Entry) (err error)

Fire adds a new InfluxDB point based off of Logrus entry

func (*InfluxDBHook) Levels

func (hook *InfluxDBHook) Levels() []logrus.Level

Levels are the available logging levels.

Directories

Path Synopsis
examples

Jump to

Keyboard shortcuts

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