glo

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Jan 23, 2019 License: MIT Imports: 5 Imported by: 2

README

GoDoc Go Report Card codecov Codacy Badge Build Status

GLO

Logging library for Golang

Inspired by Monolog for PHP, severity levels are identical

Install
go get github.com/lajosbencz/glo
Severity levels
Debug     = 100
Info      = 200
Notice    = 250
Warning   = 300
Error     = 400
Critical  = 500
Alert     = 550
Emergency = 600
Simple example
package main

import "github.com/lajosbencz/glo"

func main() {
	// Info - Warning will go to os.Stdout
	// Error - Emergency will go to os.Stderr
	log := glo.NewStdFacility()

	// goes to os.Stdout
	log.Debug("Detailed debug line: %#v", map[string]string{"x": "foo", "y": "bar"})

	// goes to os.Stderr
	log.Error("Oooof!")
}

Output:

2019-01-22T15:16:08+01:00 [DEBUG] Detailed debug line [map[x:foo y:bar]]
2019-01-22T15:16:08+01:00 [ERROR] Oooof! []
Customized example
package main

import (
	"bytes"
	"fmt"
	"os"
	"strings"

	"github.com/lajosbencz/glo"
)

func main() {
	log := glo.NewFacility()

	// write everything to a buffer
	bfr := bytes.NewBufferString("")
	handlerBfr := glo.NewHandler(bfr)
	log.PushHandler(handlerBfr)

	// write only errors and above using a short format
	handlerStd := glo.NewHandler(os.Stdout)
	formatter := glo.NewFormatter("{L}: {M}")
	filter := glo.NewFilterLevel(glo.Error)
	handlerStd.SetFormatter(formatter)
	handlerStd.PushFilter(filter)
	log.PushHandler(handlerStd)

	fmt.Println("Log output:")
	fmt.Println(strings.Repeat("=", 70))
	log.Info("Only written to the buffer")
	log.Alert("Written to both buffer and stdout")

	fmt.Println("")
	fmt.Println("Buffer contents:")
	fmt.Println(strings.Repeat("=", 70))
	fmt.Println(bfr.String())
}

Output:

Log output:
======================================================================
ALERT: Written to both buffer and stdout []

Buffer contents:
======================================================================
2019-01-22T15:14:16+01:00 [INFO] Only written to the buffer []
2019-01-22T15:14:16+01:00 [ALERT] Written to both buffer and stdout []
Custom filter
package main

import (
	"os"
	"regexp"

	"github.com/lajosbencz/glo"
)

func main() {
	handler := glo.NewHandler(os.Stdout)
	filterEmptyLines := &filterRgx{regexp.MustCompile(`^.+$`)}
	handler.PushFilter(filterEmptyLines)

	log := glo.NewFacility()
	log.PushHandler(handler)

	log.Debug("", "format is empty, should be ignored")
	log.Debug("only this should appear at the output")
}

type filterRgx struct {
	rgx *regexp.Regexp
}

func (f *filterRgx) Check(level glo.Level, line string, params ...interface{}) bool {
	return f.rgx.MatchString(line)
}

Output:

2019-01-22T15:30:23+01:00 [DEBUG] only this should appear at the output

Documentation

Index

Constants

View Source
const (
	// DefaultFormat is used by default
	DefaultFormat string = "{T} [{L}] {M}"
)

Variables

LevelList incrementally lists severities

Functions

This section is empty.

Types

type Facility

Facility is the main entry when used

func NewFacility

func NewFacility() Facility

NewFacility creates a logger facility, this is the main entry

func NewStdFacility

func NewStdFacility() Facility

NewStdFacility creates a logger with two handlers, pointing to os.Stdout and os.Stderr anything below Error gets sent to Stdout, anything above Warning gets sent to Stderr

type Filter

type Filter interface {
	Check(Level, string, ...interface{}) bool
}

Filter checks a log line against custom logic

func NewFilterLevel

func NewFilterLevel(min Level) Filter

NewFilterLevel checks the level of the log (inclusive)

func NewFilterLevelRange

func NewFilterLevelRange(min, max Level) Filter

NewFilterLevelRange checks if the level of the log is in a range (inclusive)

type Formatter

type Formatter interface {
	Format(time.Time, Level, string, ...interface{}) string
}

Formatter formats a log event

func NewFormatter

func NewFormatter(f string) Formatter

NewFormatter creates a Formatter from a string

type Handler

type Handler interface {
	Logger
	SetFormatter(Formatter) Handler
	ClearFilters() Handler
	PushFilter(Filter) Handler
}

Handler logs to stdout

func NewHandler

func NewHandler(writer io.Writer) Handler

NewHandler creates handler that prints to an io.Writer

type Level

type Level uint16

Level defines the severity

const (
	// Debug severity
	Debug Level = 100
	// Info severity
	Info Level = 200
	// Notice severity
	Notice Level = 250
	// Warning severity
	Warning Level = 300
	// Error severity
	Error Level = 400
	// Critical severity
	Critical Level = 500
	// Alert severity
	Alert Level = 550
	// Emergency severity
	Emergency Level = 600
)

func (Level) String

func (l Level) String() string

String formats the Level

type Logger

type Logger interface {
	Log(Level, string, ...interface{}) error
}

Logger logs a line with a specific level

type LoggerAlert

type LoggerAlert interface {
	Alert(string, ...interface{}) error
}

LoggerAlert logs an alert line

type LoggerCritical

type LoggerCritical interface {
	Critical(string, ...interface{}) error
}

LoggerCritical logs a critical line

type LoggerDebug

type LoggerDebug interface {
	Debug(string, ...interface{}) error
}

LoggerDebug logs a debug line

type LoggerEmergency

type LoggerEmergency interface {
	Emergency(string, ...interface{}) error
}

LoggerEmergency logs an emergency line

type LoggerError

type LoggerError interface {
	Error(string, ...interface{}) error
}

LoggerError logs an error line

type LoggerInfo

type LoggerInfo interface {
	Info(string, ...interface{}) error
}

LoggerInfo logs an info line

type LoggerNotice

type LoggerNotice interface {
	Notice(string, ...interface{}) error
}

LoggerNotice logs a notice line

type LoggerSeverity

LoggerSeverity includes all levels

type LoggerWarning

type LoggerWarning interface {
	Warning(string, ...interface{}) error
}

LoggerWarning logs a warning line

Directories

Path Synopsis
example

Jump to

Keyboard shortcuts

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