gologger

package module
v0.0.5 Latest Latest
Warning

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

Go to latest
Published: Mar 27, 2023 License: MIT Imports: 8 Imported by: 4

README

gologger

A golang logger based on logrus library, implementing out-of-the-box features such as:

  • singleton logic
  • debuging option (WithDebugLevel(true))
  • decorated output (WithRunTimeContext())
  • nologger option (WithNullLogger())
  • option to write loggin output to file instead of stdout
  • convenience methods to create new logger instances (i.e. (l) func NewLoggerWithField())

usage

Example: Debug level with decorated output style
package main

import (
	log "github.com/alejoacosta74/gologger"
)

func main() {
	logger, err := log.NewLogger(log.WithDebugLevel(true), log.WithField("app", "example"), log.WithRuntimeContext())
	if err != nil {
		panic(err)
	}

	logger.Debug("Hello from logger...!")

}

Output:

❯ go run example.go
DEBU[19-11-2022 13:53:14] example.go:13 - main.main -  Hello from logger...!                         app=example fields.file=/Users/alejoacosta/code/golang/mylogger/logger.go fields.func=github.com/alejoacosta74/gologger.createNewLogger file=/Users/alejoacosta/code/golang/mylogger/logger.go func=github.com/alejoacosta74/gologger.createNewLogger line=70
Example: using singleton feature
import (
	"sync"

	log "github.com/alejoacosta74/gologger"
)

func main() {
	_, err := log.NewLogger(log.WithField("loggerid", 0))
	if err != nil {
		panic(err)
	}

	wg := sync.WaitGroup{}
	wg.Add(3)
	for i := 1; i < 4; i++ {
		go func(i int) {
			newLogger, err := log.NewLogger(log.WithField("loggerid", i))
			if err != nil {
				panic(err)
			}

			newLogger.Info("Hello from goroutine id: ", i)
			wg.Done()
		}(i)
	}
	wg.Wait()
}

output:

❯ go run example.go
INFO Hello from goroutine id: 3                    loggerid=0
INFO Hello from goroutine id: 1                    loggerid=0
INFO Hello from goroutine id: 2                    loggerid=0

Documentation

Overview

Package gologger defines an interface and logger based on logrus(https://github.com/sirupsen/logrus) library, implementing out-of-the-box features such as: - singleton logic - debuging option (`WithDebugLevel(true)`) - decorated output (`WithRunTimeContext()`) - nologger option (`WithNullLogger()`) - option to write loggin output to file - convenience methods to create new logger instances (i.e. `(l) func NewLoggerWithField()`) - option to set custom log level

package log implements a custom logger based on logrus

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ILogger added in v0.0.3

type ILogger interface {
	Infof(string, ...interface{})
	Debugf(string, ...interface{})
	Tracef(string, ...interface{})
	Errorf(string, ...interface{})
	Fatalf(string, ...interface{})
	IsDebug() bool
	With(key string, value interface{}) ILogger
	SetLevel(level Level)
}

type Level added in v0.0.2

type Level uint32
const (
	// PanicLevel level, highest level of severity. Logs and then calls panic with the
	// message passed to Debug, Info, ...
	PanicLevel Level = iota
	// FatalLevel level. Logs and then calls `logger.Exit(1)`. It will exit even if the
	// logging level is set to Panic.
	FatalLevel
	// ErrorLevel level. Logs. Used for errors that should definitely be noted.
	// Commonly used for hooks to send errors to an error tracking service.
	ErrorLevel
	// WarnLevel level. Non-critical entries that deserve eyes.
	WarnLevel
	// InfoLevel level. General operational entries about what's going on inside the
	// application.
	InfoLevel
	// DebugLevel level. Usually only enabled when debugging. Very verbose logging.
	DebugLevel
	// TraceLevel level. Designates finer-grained informational events than the Debug.
	TraceLevel
)

type Logger

type Logger struct {
	*logrus.Entry
}

func NewLogger

func NewLogger(opts ...Option) (*Logger, error)

Returns a singleton logger instance with the given options

func (*Logger) GetLevel

func (l *Logger) GetLevel() Level

Returns the log level

func (*Logger) IsDebug

func (l *Logger) IsDebug() bool

Returns true if the log level is set to 'debug' or 'trace'

func (*Logger) NewLoggerWithField

func (l *Logger) NewLoggerWithField(key string, value interface{}) *Logger

Returns a new logger instance with the given field

func (*Logger) NewLoggerWithFields

func (l *Logger) NewLoggerWithFields(fields map[string]interface{}) *Logger

Returns a new logger instantiated from the existing logger with the given fields

func (*Logger) SetLevel added in v0.0.3

func (l *Logger) SetLevel(level Level)

Sets the log level

func (*Logger) With added in v0.0.3

func (l *Logger) With(key string, value interface{}) ILogger

Same as WithField but implements the ILogger interface

func (*Logger) WithField added in v0.0.3

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

type Option

type Option func(l *Logger) error

func WithDebugLevel

func WithDebugLevel(debug bool) Option

WithDebugLevel sets the log level to debug with some preformatted output (this option is kept for legacy reasons. Use WithLevel instead)

func WithField

func WithField(msg string, val interface{}) Option

WithField sets the field for the logger

func WithFields

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

WithFields sets the fields for the logger

func WithFiles

func WithFiles(outputFile string, errorFile string) Option

WithFiles configures the logger to write to the given files

func WithLevel added in v0.0.2

func WithLevel(level Level) Option

WithLevel sets the log level and the corresponding format for the logger

func WithNullLogger

func WithNullLogger() Option

WithNullLogger sets the logger to discard all output

func WithOutput

func WithOutput(output io.Writer) Option

WithOutput sets the output for the logger

func WithRuntimeContext

func WithRuntimeContext() Option

WithRuntimeContext sets the logger to include runtime context

Jump to

Keyboard shortcuts

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