Help us shape the future of Go! Take the 2026 Go Developer Survey

logentrus

package module
v1.0.2 Latest Latest
Warning

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

Go to latest
Published: Jan 26, 2017 License: Apache-2.0 Imports: 5 Imported by: 1

README

Logentrus | a Logentries hook for Logrus GoDoc

Logrus created by Simon Eskildsen

Install

go get -u github.com/Sirupsen/logrus github.com/puddingfactory/logentrus

Setup

First, you should get a token for your logentries account, which you'll need to feed into your app somehow.

  1. Log into your logentries account
  2. Navigate to create Add New Log
  3. Select Manual (specifically since we're using TCP directly)
  4. Set your values such as your Log Name and Log Set
  5. Receive your token!

I'd strongly recommend against storing the token directly in your source code. I personally use Environment Variables for testing purposes and have done so in the example provided below.

Usage

Just like with Logrus, it's best to define your options in init or in some early stage of your program.

package main

import (
	"os"

	"github.com/Sirupsen/logrus"
	"github.com/puddingfactory/logentrus"
)

func init() {
	logrus.SetLevel(logrus.InfoLevel)
	logrus.SetFormatter(&logrus.TextFormatter{}) // logentrus hook will always submit JSON to Logentries

	hook, err := logentrus.New(
		os.Getenv("TOKEN"), // grabbing Logentries Token from environment variable
		"Jan 2 15:04:05",   // setting empty string here will default to logrus's typically time format
		logrus.InfoLevel,   // since set to InfoLevel, DebugLevel is the only level that will be ignored
		nil,                // setting config to nil means that conn will use root certs from local system
	)
	if err != nil {
		panic(err)
	}
	logrus.AddHook(hook)
}

func main() {
	logrus.Debug("This is a debug entry that should *not* show in logentries")
	logrus.Info("This is an info entry that should show up in logentries")
}

Features

Logentrus does its own formatting

Since Logentries prefers JSON formatting, I didn't want to require it to be set in Logrus. Instead, there is a separate logrus.JSONFormatter within this hook that processes the log entries for you automatically.

You have the option of setting the logrus.JSONFormatter.TimestampFormatter value when calling logentrus.New if there's a Timestamp format you prefer.

You can provide your own set of root certs

This is a feature of Google's crypto/tls package that you can apply to logentrus.

  1. First you'll want to create a tls.Config by following this example: https://golang.org/pkg/crypto/tls/#example_Dial
  2. After that, you can drop your tls.Config into logentrus.New.

Documentation

Overview

Package logentrus acts as a Logentries (https://logentries.com) hook for Logrus (https://github.com/Sirupsen/logrus)

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Hook

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

Hook used to send logs to logentries

func New

func New(token, timestampFormat string, priority logrus.Level, config *tls.Config) (hook *Hook, err error)

New creates and returns a new hook for use in Logrus

func (*Hook) Fire

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

Fire formats and sends JSON entry to Logentries service

func (*Hook) Levels

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

Levels returns the log levels supported by this hook

Jump to

Keyboard shortcuts

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