log

package module
v0.0.3 Latest Latest
Warning

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

Go to latest
Published: Nov 21, 2019 License: MIT Imports: 11 Imported by: 1

README

go-log Travix logging library

Build Status Coverage Status Go Report Card

A package with logger implementation for Go. Contains Travix-specific fields

Implementation is influenced by zerolog

Usage

Import the package

go get "github.com/Travix-International/go-log"

A way to create logger with default settings

    import "github.com/Travix-International/go-log"
    ...
    cfg := log.NewLoggerConfig()
    logger := log.NewLogger(cfg)

Log a message with particular level, type and text

logger.Info("MessageTypeInfo").Log("MessageText")

Log an error

logger.Error("ErrorType").LogErr(err)

Create a new copy of logger with additional field

logger = logger.Str("key", "value")

Play with logging

Benchmarks

Benchmark_Logger/DebugLog-12              181819              5804 ns/op            3504 B/op         31 allocs/op
Benchmark_Logger/DebugStrLog-12           219472              5743 ns/op            3449 B/op         33 allocs/op

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Level

type Level uint8

Level defines log levels.

const (
	// DebugLevel defines debug log level.
	DebugLevel Level = iota
	// InfoLevel defines info log level.
	InfoLevel
	// WarnLevel defines warn log level.
	WarnLevel
	// ErrorLevel defines error log level.
	ErrorLevel
	// FatalLevel defines fatal log level.
	FatalLevel
	// PanicLevel defines panic log level.
	PanicLevel
	// NoLevel defines an absent log level.
	NoLevel
	// Disabled disables the logger.
	Disabled
)

func ParseLogLevel

func ParseLogLevel(str string) Level

ParseLogLevel parses string representation of log level to an object

func (Level) String

func (l Level) String() string

type Logger

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

Logger is a base container for log level and Message template messageTemplate is a pre-filled message which is cloned when a new message is created

func NewLogger

func NewLogger(cfg LoggerConfig) Logger

NewLogger create a new instance of Logger cfg - initial configration

func Nop

func Nop() Logger

Nop returns a disabled logger for which all operation are no-op.

func (Logger) Bool

func (l Logger) Bool(key string, b bool) Logger

Bool adds the field key with val as a bool to the Logger.

func (Logger) Bytes

func (l Logger) Bytes(key string, val []byte) Logger

Bytes adds the field key with val as a string to the Logger. Runes outside of normal ASCII ranges will be hex-encoded in the resulting JSON.

func (Logger) Debug

func (l Logger) Debug(msgType string) Message

Debug starts a new message with debug level.

func (Logger) Error

func (l Logger) Error(errorcode string) Message

Error starts a new message with error level and additional error fields

func (Logger) Float64

func (l Logger) Float64(key string, f float64) Logger

Float64 adds the field key with val as a float64 to the Logger.

func (Logger) GetLogLevel

func (l Logger) GetLogLevel() string

GetLogLevel returns the log level of the Logger.

func (Logger) Info

func (l Logger) Info(msgType string) Message

Info starts a new message with info level.

func (Logger) Int

func (l Logger) Int(key string, i int) Logger

Int adds the field key with val as an int to the Logger.

func (Logger) IsDebugLevel

func (l Logger) IsDebugLevel() bool

IsDebugLevel returns true if the current logger has debug level.

func (Logger) JSON

func (l Logger) JSON(key string, val interface{}) Logger

JSON adds the field key with val as JSON-encoded to the Logger.

func (Logger) Str

func (l Logger) Str(key, val string) Logger

Str adds the field key with val as a string to the Logger.

func (Logger) Warn

func (l Logger) Warn(msgType string) Message

Warn starts a new message with warn level.

func (Logger) With

func (l Logger) With() Logger

With clones the current Logger to be used within a separate context.

func (Logger) WithLevel

func (l Logger) WithLevel(logLevel Level) Logger

WithLevel clones the current logger within a separate context and with the specified log level.

type LoggerConfig

type LoggerConfig struct {
	AppGroup   string
	AppName    string
	AppVersion string
	HostName   string
	LogLevel   Level
	// contains filtered or unexported fields
}

LoggerConfig set of arguments to create an instance of Logger AppGroup - group name of application. Example: flights AppName - application name. Example: searchApi AppVersion - current version of application Hostname - current hostname Loglevel - initial log level of the Logger out - an output for log messages. Default: os.Stderr uuidGen - a function to generate UUID. Default: uuid.New().String timestampFunc - a function to generate current time.Time. Default: time.Now

func NewLoggerConfig

func NewLoggerConfig() LoggerConfig

NewLoggerConfig returns default configuration for the logger

type Message

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

Message represents a log message before marshaling into text representation err - holds error object canLog - flag to write messages with acceptable log level enc - encoder to json format timestampFunc - a function to generate current time.Time. v3template - pre-filled message in v3 log format

func (Message) Bool

func (m Message) Bool(key string, b bool) Message

Bool adds the field key with val as a bool to the Message.

func (Message) Bytes

func (m Message) Bytes(key string, val []byte) Message

Bytes adds the field key with val as a string to the Message. Runes outside of normal ASCII ranges will be hex-encoded in the resulting JSON.

func (Message) Float64

func (m Message) Float64(key string, f float64) Message

Float64 adds the field key with val as a float64 to the Message.

func (Message) Int

func (m Message) Int(key string, i int) Message

Int adds the field key with val as an int to the Message.

func (Message) JSON

func (m Message) JSON(key string, val interface{}) Message

JSON adds the field key with val as JSON-encoded to the Message.

func (Message) Log

func (m Message) Log(msg string)

Log sends the Message with msg added as the message field

func (Message) LogErr

func (m Message) LogErr(err error)

LogErr sends the Message with the error

func (Message) LogJSON

func (m Message) LogJSON(msg string, obj interface{})

LogJSON sends the Message with the obj added as JSON-encoded in the message field.

func (Message) LogMsg

func (m Message) LogMsg(shortMsg, msg string)

LogMsg sends the Message with shortmsg added as the message field and msg in the payload

func (Message) Logf

func (m Message) Logf(format string, v ...interface{})

Logf sends the Message with formatted msg added as the message field

func (Message) Str

func (m Message) Str(key, val string) Message

Str adds the field key with val as a string to the Message.

Jump to

Keyboard shortcuts

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