lager

package
v0.0.2 Latest Latest
Warning

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

Go to latest
Published: Aug 17, 2021 License: Apache-2.0, Apache-2.0 Imports: 11 Imported by: 0

README

lager

Lager is a logging library for go.

Usage

Instantiate a logger with the name of your component.

import (
  "gitee.com/anyp2p/log/lager"
)

logger := lager.NewLogger("my-app")
Sinks

Lager can write logs to a variety of destinations. You can specify the destinations using Lager sinks:

To write to an arbitrary Writer object:

logger.RegisterSink(lager.NewWriterSink(myWriter, lager.INFO))
Emitting logs

Lager supports the usual level-based logging, with an optional argument for arbitrary key-value data.

logger.Info("doing-stuff", logger.Data{
  "informative": true,
})

output:

{ "source": "my-app", "message": "doing-stuff", "data": { "informative": true }, "timestamp": 1232345, "log_level": 1 }

Error messages also take an Error object:

logger.Error("failed-to-do-stuff", errors.New("Something went wrong"))

output:

{ "source": "my-app", "message": "failed-to-do-stuff", "data": { "error": "Something went wrong" }, "timestamp": 1232345, "log_level": 1 }
Sessions

You can avoid repetition of contextual data using 'Sessions':


contextualLogger := logger.Session("my-task", logger.Data{
  "request-id": 5,
})

contextualLogger.Info("my-action")

output:

{ "source": "my-app", "message": "my-task.my-action", "data": { "request-id": 5 }, "timestamp": 1232345, "log_level": 1 }

License

Lager is Apache 2.0 licensed.

Documentation

Index

Constants

View Source
const StackTraceBufferSize = 1024 * 100

StackTraceBufferSize is a constant which defines stack track buffer size

Variables

This section is empty.

Functions

func FormatLogLevel

func FormatLogLevel(x LogLevel) string

FormatLogLevel is a function which returns string format of log level

Types

type Data

type Data map[string]interface{}

Data is a map

type LogFormat

type LogFormat struct {
	LogLevel  LogLevel `json:"level"`
	Source    string   `json:"source"`
	Timestamp string   `json:"timestamp"`
	File      string   `json:"file"`
	Message   string   `json:"msg"`
	Data      Data     `json:"data,omitempty"`
}

LogFormat is a struct which stores details about log

func (LogFormat) ToJSON

func (log LogFormat) ToJSON() ([]byte, error)

ToJSON which converts data of log file in to JSON file

type LogLevel

type LogLevel int

LogLevel is a user defined variable of type int

const (
	//DEBUG is a constant of user defined type LogLevel
	DEBUG LogLevel = iota
	INFO
	WARN
	ERROR
	FATAL
)

func (LogLevel) MarshalJSON

func (x LogLevel) MarshalJSON() ([]byte, error)

MarshalJSON is a function which returns data in JSON format

type Logger

type Logger interface {
	RegisterSink(Sink)
	Session(task string, data ...Data) Logger
	SessionName() string
	Debug(action string, data ...Data)
	Info(action string, data ...Data)
	Warn(action string, data ...Data)
	Error(action string, err error, data ...Data)
	Fatal(action string, err error, data ...Data)
	Debugf(format string, args ...interface{})
	Infof(format string, args ...interface{})
	Warnf(format string, args ...interface{})
	Errorf(err error, format string, args ...interface{})
	Fatalf(err error, format string, args ...interface{})
	WithData(Data) Logger
}

Logger is a interface

func NewLogger

func NewLogger(component string) Logger

NewLogger is a function used to get new logger object

func NewLoggerExt

func NewLoggerExt(component string, isFormatText bool) Logger

NewLoggerExt is a function which returns logger struct object

type ReconfigurableSink

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

ReconfigurableSink is a struct

func NewReconfigurableSink

func NewReconfigurableSink(sink Sink, initialMinLogLevel LogLevel) *ReconfigurableSink

NewReconfigurableSink is a function which returns struct object

func (*ReconfigurableSink) GetMinLevel

func (sink *ReconfigurableSink) GetMinLevel() LogLevel

GetMinLevel is a method which gets minimum log level

func (*ReconfigurableSink) Log

func (sink *ReconfigurableSink) Log(level LogLevel, log []byte)

Log is a method which returns log level and log

func (*ReconfigurableSink) SetMinLevel

func (sink *ReconfigurableSink) SetMinLevel(level LogLevel)

SetMinLevel is a function which sets minimum log level

type Sink

type Sink interface {
	//Log to the sink.  Best effort -- no need to worry about errors.
	Log(level LogLevel, payload []byte)
}

A Sink represents a write destination for a Logger. It provides a thread-safe interface for writing logs

func NewWriterSink

func NewWriterSink(name string, writer io.Writer, minLogLevel LogLevel) Sink

NewWriterSink is function which returns new struct object

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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