logger

package module
v0.0.0-...-fe37934 Latest Latest
Warning

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

Go to latest
Published: Mar 21, 2024 License: MIT Imports: 10 Imported by: 55

README

logger

Build Status | codecov | Go Report Card | GoDoc

A simplified logger that allows you to add complexity depending of your requirements. The easy way to use the logger:

import log github.com/joaosoft/logger

log.Info("hello")

you also can config it, as i prefer, please see below After a read of the project https://gitlab.com/vredens/loggerger extracted some concepts like allowing to add tags and fields to logger infrastructure.

If i miss something or you have something interesting, please be part of this project. Let me know! My contact is at the end.

With support for

  • formatted messages

  • prefixes (special prefixes: DATE, TIME, TIMESTAMP, LEVEL, IP, PACKAGE, FUNCTION, FILE, TRACE, STACK)

  • tags

  • fields

  • writers at [writer]

    • to file (with queue processing)[1]
    • to stdout (with queue processing)[1] [here]
  • addition commands (ToError())

    [1] this writer allows you to continue the processing and dispatch the logging

Levels

  • DefaultLevel, default level
  • PanicLevel, when there is no recover
  • FatalLevel, when the error is fatal to the application
  • ErrorLevel, when there is a controlled error
  • WarnLevel, when there is a warning
  • InfoLevel, when it is a informational message
  • DebugLevel, when it is a debugging message
  • PrintLevel, when it is a system message
  • NoneLevel, when the logging is disabled

Special Prefix's

  • LEVEL, add the level value to the prefix
  • TIMESTAMP, add the timestamp value to the prefix
  • DATE, add the date value to the prefix
  • TIME, add the time value to the prefix
  • IP, add the client ip address
  • TRACE, add the error trace
  • PACKAGE, add the package name
  • FILE, add the file
  • FUNCTION, add the function name
  • STACK, add the debug stack

Dependecy Management

Dep

Project dependencies are managed using Dep. Read more about Dep.

  • Install dependencies: dep ensure
  • Update dependencies: dep ensure -update
Go
go get github.com/joaosoft/logger/service

Interface

type Logger interface {
    SetLevel(level Level)

	With(prefixes, tags, fields, sufixes map[string]interface{}) ILogger
	WithPrefixes(prefixes map[string]interface{}) ILogger
	WithTags(tags map[string]interface{}) ILogger
	WithFields(fields map[string]interface{}) ILogger
	WithSufixes(sufixes map[string]interface{}) ILogger

	WithPrefix(key string, value interface{}) ILogger
	WithTag(key string, value interface{}) ILogger
	WithField(key string, value interface{}) ILogger
	WithSufix(key string, value interface{}) ILogger

	Debug(message interface{}) IAddition
	Info(message interface{}) IAddition
	Warn(message interface{}) IAddition
	Error(message interface{}) IAddition
	Panic(message interface{}) IAddition
	Fatal(message interface{}) IAddition
	Print(message interface{}) IAddition

	Debugf(format string, arguments ...interface{}) IAddition
	Infof(format string, arguments ...interface{}) IAddition
	Warnf(format string, arguments ...interface{}) IAddition
	Errorf(format string, arguments ...interface{}) IAddition
	Panicf(format string, arguments ...interface{}) IAddition
	Fatalf(format string, arguments ...interface{}) IAddition
	Printf(format string, arguments ...interface{}) IAddition

	IsDebugEnabled() bool
	IsInfoEnabled() bool
	IsWarnEnabled() bool
	IsErrorEnabled() bool
	IsPanicEnabled() bool
	IsFatalEnabled() bool
	IsPrintEnabled() bool

	Reconfigure(options ...LoggerOption)
}

type IAddition interface {
	ToError() error
}

type ISpecialWriter interface {
	SWrite(prefixes map[string]interface{}, tags map[string]interface{}, message interface{}, fields map[string]interface{}, sufixes map[string]interface{}) (n int, err error)
}

Usage

This examples are available in the project at logger/examples

//
// log to text
fmt.Println(":: LOG TEXT")
log := logger.NewLogger(
    logger.WithLevel(logger.InfoLevel),
    logger.WithFormatHandler(writer.TextFormatHandler),
    logger.WithWriter(os.Stdout)).
    With(
        map[string]interface{}{"level": logger.LEVEL, "timestamp": logger.TIMESTAMP, "date": logger.DATE, "time": logger.TIME},
        map[string]interface{}{"service": "log"},
        map[string]interface{}{"name": "joão"},
        map[string]interface{}{"ip": logger.IP, "function": logger.FUNCTION, "file": logger.FILE})

// logging...
log.Error("isto é uma mensagem de error")
log.Info("isto é uma mensagem de info")
log.Debug("isto é uma mensagem de debug")
log.Error("")

fmt.Println("--------------")
<-time.After(time.Second)

//
// log to json
fmt.Println(":: LOG JSON")
log = logger.NewLogger(
    logger.WithLevel(logger.InfoLevel),
    logger.WithFormatHandler(writer.JsonFormatHandler),
    logger.WithWriter(os.Stdout)).
    With(
        map[string]interface{}{"level": logger.LEVEL, "timestamp": logger.TIMESTAMP, "date": logger.DATE, "time": logger.TIME},
        map[string]interface{}{"service": "log"},
        map[string]interface{}{"name": "joão"},
        map[string]interface{}{"ip": logger.IP, "function": logger.FUNCTION, "file": logger.FILE})

// logging...
log.Errorf("isto é uma mensagem de error %s", "hello")
log.Infof("isto é uma  mensagem de info %s ", "hi")
log.Debugf("isto é uma mensagem de debug %s", "ehh")
Output
default...
:: LOG TEXT
{prefixes:map[level:error timestamp:2018-08-16 20:27:13:18 date:2018-08-16 time:20:27:13:18] tags:map[service:log] message:isto é uma mensagem de error fields:map[name:joão] sufixes:map[ip:192.168.1.4 function:Example.ExampleDefaultLogger file:/Users/joaoribeiro/workspace/go/personal/src/logger/examples/main.go]}
{prefixes:map[level:info timestamp:2018-08-16 20:27:13:18 date:2018-08-16 time:20:27:13:18] tags:map[service:log] message:isto é uma mensagem de info fields:map[name:joão] sufixes:map[ip:192.168.1.4]}
{prefixes:map[level:error timestamp:2018-08-16 20:27:13:18 date:2018-08-16 time:20:27:13:18] tags:map[service:log] message: fields:map[name:joão] sufixes:map[ip:192.168.1.4 function:Example.ExampleDefaultLogger file:/Users/joaoribeiro/workspace/go/personal/src/logger/examples/main.go]}
--------------
:: LOG JSON
{"prefixes":{"date":"2018-08-16","level":"error","time":"20:27:14:18","timestamp":"2018-08-16 20:27:14:18"},"tags":{"service":"log"},"message":"isto é uma mensagem de error hello","fields":{"name":"joão"},"sufixes":{"file":"/Users/joaoribeiro/workspace/go/personal/src/logger/examples/main.go","function":"Example.ExampleDefaultLogger","ip":"192.168.1.4"}}
{"prefixes":{"date":"2018-08-16","level":"info","time":"20:27:14:18","timestamp":"2018-08-16 20:27:14:18"},"tags":{"service":"log"},"message":"isto é uma  mensagem de info hi ","fields":{"name":"joão"},"sufixes":{"ip":"192.168.1.4"}}

Known issues

  • all the maps do not guarantee order of the items!

Follow me at

Facebook: https://www.facebook.com/joaosoft

LinkedIn: https://www.linkedin.com/in/jo%C3%A3o-ribeiro-b2775438/

If you have something to add, please let me know joaosoft@gmail.com

Documentation

Index

Constants

View Source
const (
	LevelDefault = LevelInfo // LevelDefault Level

	LevelNone  Level = iota // LevelNone, when the logging is disabled
	LevelPrint              // LevelPrint, when it is a system message
	LevelPanic              // LevelPanic, when there is no recover
	LevelFatal              // LevelFatal, when the error is fatal to the application
	LevelError              // LevelError, when there is a controlled error
	LevelWarn               // LevelWarn, when there is a warning
	LevelInfo               // LevelInfo, when it is a informational message
	LevelDebug              // LevelDebug, when it is a debugging message

	// Special Prefixes
	LEVEL     Prefix = "{{LEVEL}}"     // Add the level value to the prefix
	TIMESTAMP Prefix = "{{TIMESTAMP}}" // Add the timestamp value to the prefix
	DATE      Prefix = "{{DATE}}"      // Add the date value to the prefix
	TIME      Prefix = "{{TIME}}"      // Add the time value to the prefix
	IP        Prefix = "{{IP}}"        // Add the client ip address
	TRACE     Prefix = "{{TRACE}}"     // Add the error trace
	PACKAGE   Prefix = "{{PACKAGE}}"   // Add the package name
	FILE      Prefix = "{{FILE}}"      // Add the file
	FUNCTION  Prefix = "{{FUNCTION}}"  // Add the function name
	STACK     Prefix = "{{STACK}}"     // Add the debug stack
)

Variables

Functions

func IsDebugEnabled

func IsDebugEnabled() bool

func IsErrorEnabled

func IsErrorEnabled() bool

func IsFatalEnabled

func IsFatalEnabled() bool

func IsInfoEnabled

func IsInfoEnabled() bool

func IsPanicEnabled

func IsPanicEnabled() bool

func IsPrintEnabled

func IsPrintEnabled() bool

func IsWarnEnabled

func IsWarnEnabled() bool

func Reconfigure

func Reconfigure(options ...LoggerOption)

func SetLevel

func SetLevel(level Level)

Types

type Addition

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

func (*Addition) ToError

func (addition *Addition) ToError() error

ToError

type IAddition

type IAddition interface {
	ToError() error
}

func Debug

func Debug(message interface{}) IAddition

func Debugf

func Debugf(format string, arguments ...interface{}) IAddition

func Error

func Error(message interface{}) IAddition

func Errorf

func Errorf(format string, arguments ...interface{}) IAddition

func Fatal

func Fatal(message interface{}) IAddition

func Fatalf

func Fatalf(format string, arguments ...interface{}) IAddition

func Info

func Info(message interface{}) IAddition

func Infof

func Infof(format string, arguments ...interface{}) IAddition

func NewAddition

func NewAddition(message string) IAddition

NewAddition ...

func Panic

func Panic(message interface{}) IAddition

func Panicf

func Panicf(format string, arguments ...interface{}) IAddition

func Print

func Print(message interface{}) IAddition

func Printf

func Printf(format string, arguments ...interface{}) IAddition

func Warn

func Warn(message interface{}) IAddition

func Warnf

func Warnf(format string, arguments ...interface{}) IAddition

type ILogger

type ILogger interface {
	SetLevel(level Level)

	With(prefixes, tags, fields, sufixes map[string]interface{}) ILogger
	WithPrefixes(prefixes map[string]interface{}) ILogger
	WithTags(tags map[string]interface{}) ILogger
	WithFields(fields map[string]interface{}) ILogger
	WithSufixes(sufixes map[string]interface{}) ILogger

	WithPrefix(key string, value interface{}) ILogger
	WithTag(key string, value interface{}) ILogger
	WithField(key string, value interface{}) ILogger
	WithSufix(key string, value interface{}) ILogger

	Debug(message interface{}) IAddition
	Info(message interface{}) IAddition
	Warn(message interface{}) IAddition
	Error(message interface{}) IAddition
	Panic(message interface{}) IAddition
	Fatal(message interface{}) IAddition
	Print(message interface{}) IAddition

	Debugf(format string, arguments ...interface{}) IAddition
	Infof(format string, arguments ...interface{}) IAddition
	Warnf(format string, arguments ...interface{}) IAddition
	Errorf(format string, arguments ...interface{}) IAddition
	Panicf(format string, arguments ...interface{}) IAddition
	Fatalf(format string, arguments ...interface{}) IAddition
	Printf(format string, arguments ...interface{}) IAddition

	IsDebugEnabled() bool
	IsInfoEnabled() bool
	IsWarnEnabled() bool
	IsErrorEnabled() bool
	IsPanicEnabled() bool
	IsFatalEnabled() bool
	IsPrintEnabled() bool

	Reconfigure(options ...LoggerOption)
}

func NewLogDefault

func NewLogDefault(service string, level Level) ILogger

NewLogDefault

func NewLogger

func NewLogger(options ...LoggerOption) ILogger

NewLogger ...

func NewLoggerEmpty

func NewLoggerEmpty(level Level) ILogger

NewLoggerEmpty

func With

func With(prefixes, tags, fields, sufixes map[string]interface{}) ILogger

func WithField

func WithField(key string, value interface{}) ILogger

func WithFields

func WithFields(fields map[string]interface{}) ILogger

func WithPrefix

func WithPrefix(key string, value interface{}) ILogger

func WithPrefixes

func WithPrefixes(prefixes map[string]interface{}) ILogger

func WithSufix

func WithSufix(key string, value interface{}) ILogger

func WithSufixes

func WithSufixes(sufixes map[string]interface{}) ILogger

func WithTag

func WithTag(key string, value interface{}) ILogger

func WithTags

func WithTags(tags map[string]interface{}) ILogger

type ISpecialWriter

type ISpecialWriter interface {
	SWrite(prefixes map[string]interface{}, tags map[string]interface{}, message interface{}, fields map[string]interface{}, sufixes map[string]interface{}) (n int, err error)
}

type Level

type Level int

func ParseLevel

func ParseLevel(level string) (Level, error)

func (Level) String

func (level Level) String() string

type Logger

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

Logger ...

func (*Logger) Debug

func (logger *Logger) Debug(message interface{}) IAddition

func (*Logger) Debugf

func (logger *Logger) Debugf(format string, arguments ...interface{}) IAddition

func (*Logger) Error

func (logger *Logger) Error(message interface{}) IAddition

func (*Logger) Errorf

func (logger *Logger) Errorf(format string, arguments ...interface{}) IAddition

func (*Logger) Fatal

func (logger *Logger) Fatal(message interface{}) IAddition

func (*Logger) Fatalf

func (logger *Logger) Fatalf(format string, arguments ...interface{}) IAddition

func (*Logger) Info

func (logger *Logger) Info(message interface{}) IAddition

func (*Logger) Infof

func (logger *Logger) Infof(format string, arguments ...interface{}) IAddition

func (*Logger) IsDebugEnabled

func (logger *Logger) IsDebugEnabled() bool

func (*Logger) IsErrorEnabled

func (logger *Logger) IsErrorEnabled() bool

func (*Logger) IsFatalEnabled

func (logger *Logger) IsFatalEnabled() bool

func (*Logger) IsInfoEnabled

func (logger *Logger) IsInfoEnabled() bool

func (*Logger) IsPanicEnabled

func (logger *Logger) IsPanicEnabled() bool

func (*Logger) IsPrintEnabled

func (logger *Logger) IsPrintEnabled() bool

func (*Logger) IsWarnEnabled

func (logger *Logger) IsWarnEnabled() bool

func (*Logger) Panic

func (logger *Logger) Panic(message interface{}) IAddition

func (*Logger) Panicf

func (logger *Logger) Panicf(format string, arguments ...interface{}) IAddition

func (*Logger) Print

func (logger *Logger) Print(message interface{}) IAddition

func (*Logger) Printf

func (logger *Logger) Printf(format string, arguments ...interface{}) IAddition

func (*Logger) Reconfigure

func (logger *Logger) Reconfigure(options ...LoggerOption)

Reconfigure ...

func (*Logger) SetLevel

func (logger *Logger) SetLevel(level Level)

func (*Logger) Warn

func (logger *Logger) Warn(message interface{}) IAddition

func (*Logger) Warnf

func (logger *Logger) Warnf(format string, arguments ...interface{}) IAddition

func (*Logger) With

func (logger *Logger) With(prefixes, tags, fields, sufixes map[string]interface{}) ILogger

func (*Logger) WithField

func (logger *Logger) WithField(key string, value interface{}) ILogger

func (*Logger) WithFields

func (logger *Logger) WithFields(fields map[string]interface{}) ILogger

func (*Logger) WithPrefix

func (logger *Logger) WithPrefix(key string, value interface{}) ILogger

func (*Logger) WithPrefixes

func (logger *Logger) WithPrefixes(prefixes map[string]interface{}) ILogger

func (*Logger) WithSufix

func (logger *Logger) WithSufix(key string, value interface{}) ILogger

func (*Logger) WithSufixes

func (logger *Logger) WithSufixes(sufixes map[string]interface{}) ILogger

func (*Logger) WithTag

func (logger *Logger) WithTag(key string, value interface{}) ILogger

func (*Logger) WithTags

func (logger *Logger) WithTags(tags map[string]interface{}) ILogger

type LoggerOption

type LoggerOption func(log *Logger)

LoggerOption ...

func WithFormatHandler

func WithFormatHandler(formatHandler writer.FormatHandler) LoggerOption

WithFormatHandler ...

func WithLevel

func WithLevel(level Level) LoggerOption

WithLevel ...

func WithOptField

func WithOptField(key string, value interface{}) LoggerOption

func WithOptFields

func WithOptFields(fields map[string]interface{}) LoggerOption

func WithOptPrefix

func WithOptPrefix(key string, value interface{}) LoggerOption

func WithOptPrefixes

func WithOptPrefixes(prefixes map[string]interface{}) LoggerOption

func WithOptTag

func WithOptTag(key string, value interface{}) LoggerOption

func WithOptTags

func WithOptTags(tags map[string]interface{}) LoggerOption

func WithOptions

func WithOptions(prefixes, tags, fields map[string]interface{}) LoggerOption

func WithSpecialWriter

func WithSpecialWriter(writer ...ISpecialWriter) LoggerOption

WithSpecialWriter ...

func WithWriter

func WithWriter(writer ...io.Writer) LoggerOption

WithWriter ...

type Prefix

type Prefix string

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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