logger

package module
v0.0.4 Latest Latest
Warning

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

Go to latest
Published: Aug 10, 2022 License: Apache-2.0 Imports: 6 Imported by: 0

README

GO Logger

In this package we combined different loggers so they can be compiled with a single interface. This enables switching between the different loggers without changing your code!

Supported loggers

  • Pretty printer
  • Zap
  • Mock (empty logger)

TODO

  • log
  • glog
  • klog

How to use

Basic usage

It is possible to simply call the logger without any initialization, the default logger is the pretty logger

package main

import logger "github.com/dwertent/go-logger"

func main(){

    logger.L().Info("This is a nice and colorful logger")
	// output: [info] This is a nice and colorful logger
}
Initialize a logger
package main

import logger "github.com/dwertent/go-logger"

func main() {
	// initialize colored logger
	logger.InitLogger("pretty")
	logger.L().Info("This is the pretty logger")
	// output: [info] This is the pretty logger

	// initialize zap (json) logger
	logger.InitLogger("zap")
	logger.L().Info("This is the zap logger")
	// output: {"level":"info","ts":"2022-06-20T19:11:34-04:00","msg":"This is the zap logger"}

	// initialize a mock logger. The mock logger does not print anything
	logger.InitLogger("mock")
	logger.L().Info("This message will not be printed")
	// output:
}
Adding other information to the log

It is possible to add additional information to the log so as strings, integers, errors, date

package main

import "github.com/dwertent/go-logger/helpers"
import logger "github.com/dwertent/go-logger"

func main(){

    logger.L().Info("ID", helpers.String("name", "my name"), helpers.Int("age", 45), helpers.Interface("address", "address object"))
    // output: [info] ID. name: my name; age: 45; address: address object

}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DisableColor

func DisableColor(flag bool)

func InitDefaultLogger

func InitDefaultLogger()

func InitLogger

func InitLogger(loggerName string)
InitLogger initialize desired logger

Use: InitLogger("<logger name>")

Supported logger names (call ListLoggersNames() for listing supported loggers) - "zap": Logger from package "go.uber.org/zap" - "pretty", "colorful": Human friendly colorful logger - "none", "mock", "empty", "ignore": Logger will not print anything

Default: - "pretty"

e.g. InitLogger("none") -> will initialize the mock logger

func ListLoggersNames

func ListLoggersNames() []string

Types

type ILogger

type ILogger interface {
	Fatal(msg string, details ...helpers.IDetails) // print log and exit 1
	Error(msg string, details ...helpers.IDetails)
	Success(msg string, details ...helpers.IDetails)
	Warning(msg string, details ...helpers.IDetails)
	Info(msg string, details ...helpers.IDetails)
	Debug(msg string, details ...helpers.IDetails)

	SetLevel(level string) error
	GetLevel() string

	SetWriter(w *os.File)
	GetWriter() *os.File

	LoggerName() string
}

func L

func L() ILogger

Return initialized logger. If logger not initialized, will call InitializeLogger() with the default value

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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