log

package module
v1.8.5 Latest Latest
Warning

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

Go to latest
Published: Nov 14, 2022 License: Apache-2.0 Imports: 10 Imported by: 4

README

log

Another logger with telemetry capabilities using StatsD, Prometheus, InfluxDB...

How to use it

You can use it like any common log package:

import (
    "github.com/thehivecorporation/log/writers/text"
    "github.com/thehivecorporation/log"
)

log.SetWriter(text.New(os.Stdout))
log.SetLevel(log.LevelInfo)

log.Info("Some information")
log.Debug("Some Debug info")

How to use telemetry:

Use any of the included implementations on telemetry folder:

import (
    "github.com/thehivecorporation/log/telemetry/statsd"
    "github.com/thehivecorporation/log/writers/json"
    "github.com/thehivecorporation/log"
)

log.SetWriter(text.New(os.Stdout))
log.SetTelemetry(statsd.New(statsd.Conf{
    Address:   "localhost:9125",
    Namespace: "myapp.",
}))

log.WithTags("tag1").Inc("mycounter", 1).WithField("key", "value").Info("incremented")
// Outputs
// {"level":1,"messages":["incremented"],"fields":{"key":"value"},"ts":"2017-10-16T00:03:07.685786669+02:00"}

At the same time a +1 with tag 'tag1' to the metric 'mycounter'

More information

Common functionality is covered by the following interface:

type Tags map[string]string
type Fields map[string]interface{}
type Level int

type Logger interface {
	Debug(msg interface{}) Telemetry
	Info(msg interface{}) Telemetry
	Warn(msg interface{}) Telemetry
	Error(msg interface{}) Telemetry
	Fatal(msg interface{}) Telemetry

	Debugf(msg string, v ...interface{}) Telemetry
	Infof(msg string, v ...interface{}) Telemetry
	Warnf(msg string, v ...interface{}) Telemetry
	Errorf(msg string, v ...interface{}) Telemetry
	Fatalf(msg string, v ...interface{}) Telemetry

	WithField(s string, v interface{}) Logger
	WithFields(Fields) Logger
	WithError(...error) Logger
	WithErrors(...error) Logger

	WithTags(t Tags) Telemetry
	WithTag(string, string) Telemetry

	Clone(callStack int) Logger
}

Using WithTags(s ...string) Telemetry will return an instance to use it with the following methods:

type Telemetry interface {
	WithTags(t Tags) Telemetry
	WithTag(string, string) Telemetry

	Inc(name string, value float64, extra ...interface{}) Logger
	Gauge(string, float64, ...interface{}) Logger
	Histogram(name string, value float64, extra ...interface{}) Logger
	Summary(name string, value float64, extra ...interface{}) Logger

	Clone() Telemetry
	SetLogger(l Logger)
}

Log outputs (Writers)

  • Text with a provided io.Writer
  • JSON with a provided io.Writer
  • Custom will resend any output back for your control in case that you need to customize what to do with each output
  • Memory (stores each log to an array, useful for testing purposes)
  • Multi: Allows to use more than one Writer so you can log to console AND to Papertrail
  • Papertrail
  • TODO Kafka
  • TODO Google PubSub
  • TODO NATS
  • TODO NSQ
  • TODO AWS Kinesis
  • TODO AWS SQS
  • TODO Elastic

Telemetry implementations:

  • StatsD
  • Prometheus
  • InfluxDB
  • Multi (alpha) to monitor using more than one system
  • TODO OpenTSDB Test

Documentation

Index

Constants

This section is empty.

Variables

Colors mapping.

View Source
var LevelNames = [...]string{
	LevelDebug: "debug",
	LevelInfo:  "info",
	LevelWarn:  "warn",
	LevelError: "error",
	LevelFatal: "fatal",
}
View Source
var LevelStrings = map[string]Level{
	"debug": LevelDebug,
	"info":  LevelInfo,
	"warn":  LevelWarn,
	"error": LevelError,
	"fatal": LevelFatal,
}

Functions

func DisableStackInfo added in v1.8.1

func DisableStackInfo()

func SetLevel

func SetLevel(l Level)

func SetTelemetry

func SetTelemetry(t Telemetry)

func SetWriter

func SetWriter(w Writer)

Types

type Fields

type Fields map[string]interface{}

type Level

type Level int
const (
	InvalidLevel Level = iota - 1
	LevelDebug
	LevelInfo
	LevelWarn
	LevelError
	LevelFatal
)

Log levels.

func LevelFromString added in v1.8.1

func LevelFromString(s string) Level

type Logger

type Logger interface {
	Debug(msg interface{}, more ...interface{}) Telemetry
	Info(msg interface{}, more ...interface{}) Telemetry
	Warn(msg interface{}, more ...interface{}) Telemetry
	Error(msg interface{}, more ...interface{}) Telemetry
	Fatal(msg interface{}, more ...interface{}) Telemetry

	Debugf(msg string, v ...interface{}) Telemetry
	Infof(msg string, v ...interface{}) Telemetry
	Warnf(msg string, v ...interface{}) Telemetry
	Errorf(msg string, v ...interface{}) Telemetry
	Fatalf(msg string, v ...interface{}) Telemetry

	WithField(s string, v interface{}) Logger
	WithFields(Fields) Logger
	WithError(...error) Logger
	WithErrors(...error) Logger

	WithTags(t Tags) Telemetry
	WithTag(string, string) Telemetry

	Clone() Logger
}

func Fix added in v1.8.1

func Fix(name string, value float64) Logger

func Gauge added in v0.1.7

func Gauge(name string, value float64) Logger

func Histogram added in v0.1.7

func Histogram(name string, value float64, extra ...interface{}) Logger

func Inc

func Inc(name string, value float64) Logger

func Summary added in v1.8.1

func Summary(name string, value float64, extra ...interface{}) Logger

func WithError

func WithError(err ...error) Logger

func WithField

func WithField(s string, v interface{}) Logger

func WithFields

func WithFields(f Fields) Logger

type Payload

type Payload struct {
	Level             Level         `json:"level,omitempty"`
	Messages          []interface{} `json:"messages,omitempty"`
	Fields            Fields        `json:"fields,omitempty"`
	Timestamp         time.Time     `json:"ts,omitempty"`
	Tags              Tags          `json:"tags,omitempty"`
	Errors            []string      `json:"Errors,omitempty"`
	ElapsedSinceStart time.Duration `json:"ElapsedSinceStart,omitempty"`
}

type Tags

type Tags map[string]string

type Telemetry

type Telemetry interface {
	WithTags(t Tags) Telemetry
	WithTag(string, string) Telemetry

	Inc(name string, value float64, extra ...interface{}) Logger
	Gauge(string, float64, ...interface{}) Logger
	Fix(string, float64, ...interface{}) Logger
	Histogram(name string, value float64, extra ...interface{}) Logger
	Summary(name string, value float64, extra ...interface{}) Logger

	Clone() Telemetry
	SetLogger(l Logger)
}

func Debug

func Debug(s interface{}, more ...interface{}) Telemetry

func Debugf

func Debugf(s string, i ...interface{}) Telemetry

func Error

func Error(s interface{}, more ...interface{}) Telemetry

func Errorf

func Errorf(s string, i ...interface{}) Telemetry

func Fatal

func Fatal(s interface{}, more ...interface{}) Telemetry

func FatalFIfError added in v1.8.2

func FatalFIfError(err error, s string, i ...interface{}) Telemetry

func FatalIfError added in v1.8.2

func FatalIfError(err error) Telemetry

func FatalIfErrorS added in v1.8.3

func FatalIfErrorS(err error, s interface{}) Telemetry

func Fatalf

func Fatalf(s string, i ...interface{}) Telemetry

func Info

func Info(s interface{}, more ...interface{}) Telemetry

func Infof

func Infof(s string, i ...interface{}) Telemetry

func Warn

func Warn(s interface{}, more ...interface{}) Telemetry

func Warnf

func Warnf(s string, i ...interface{}) Telemetry

func WithTag

func WithTag(k string, v string) Telemetry

func WithTags

func WithTags(tags Tags) Telemetry

type TextWriter

type TextWriter struct {
	sync.Mutex
	IOWriter      io.Writer
	ErrorIOWriter io.Writer
}

func (*TextWriter) WriteLog

func (w *TextWriter) WriteLog(p *Payload)

type Writer

type Writer interface {
	WriteLog(payload *Payload)
}

Directories

Path Synopsis
writers

Jump to

Keyboard shortcuts

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