coolog

package module
v0.0.0-...-357fe6b Latest Latest
Warning

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

Go to latest
Published: Oct 28, 2021 License: MIT Imports: 9 Imported by: 0

README

Coolog

Coolog is a logging interface based upon Chris Hines' Slide Deck and gokit's log package.

The motivation for this package can be found here.

This library is meant as a common interface to be able to implement any type of log package like github.com/sirupsen/logrus or go.uber.org/zap.

So where you need a logger in your code, do this:

func funcWhichPassesLogger(log *coolog.Logger) {

}

That way, any logger which implements this logging interface is good to go. Using a different kind of method than we are used to:

Print(level string, msg string, data ...map[string]interface{}) error

So to call the logger, we need to pass the level (which has defaults depending upon the implementation currently in this library -- aka zap or logrus), the message string, and and detail fields.

logLevel := "info"
logFilename := "/var/log/your_log_mang.log"
logType := "text"

log, err := coolog.NewLogrusLogger(logLevel , logFilename, logType)
if err != nil {
    // handle d stuff mang!
}

log.Print("error", "this is a serious thing you did wrong, mang", map[string]interface{}{"key1":"value1", "key2":"value2"})

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Logger

type Logger interface {
	Print(level string, msg string, data ...map[string]interface{}) error
}

Logger is influenced by gokit's log (https://pkg.go.dev/github.com/go-kit/kit@v0.10.0/log), but I did not agree with the oversimplification of their interface -- obfuscates too much. I wanted a structured logger which did not hide how to use it, was simple (i only want to implement a couple methods), and allowed all the complexity of what package was being used (e.g. zap, logrus, etc.) to be handled in a "New" function and a single call function -- aka "Print".

type LogrusLogger

type LogrusLogger struct {
	Logger      *logrus.Logger
	LogrusLevel logrus.Level
	Level       string
}

LogrusLogger is our struct to implement the Logger interface.

func NewLogrusLogger

func NewLogrusLogger(logLevel, logFilename, logType string) (*LogrusLogger, error)

NewLogrusLogger is a convenience function to create a logrus Logger which implements Logger interface.

logLevel is "trace", "debug", "info", "warn", "error", "fatal", or "panic" and will be the minimum level the Logger will log in the file or console. If level is empty, it will default to "info" level.

logFilename is where you want the log file stored. If this is empty, it will default to stdout.

logType is "json" or "text". If this is empty, it will default to "json".

func (*LogrusLogger) Print

func (l *LogrusLogger) Print(level string, msg string, data ...map[string]interface{}) error

Print is the main method, and implements interface Logger.

type ZapLogger

type ZapLogger struct {
	Logger *zap.Logger
	Level  string
	Atom   zap.AtomicLevel
}

ZapLogger is the struct which implements the Logger interface which allows access to the "go.uber.org/zap" style of logging.

func NewZapLogger

func NewZapLogger(level string, logLocations []string, logType string) (*ZapLogger, error)

NewZapLogger provides a newly initialized Zap Logger which implements Logger interface.

level is "trace", "debug", "info", "warn", "error" which will be the minimum level this Logger will log in the file or console. If level is empty, it will default to "info" level.

logLocations s where you want the log file stored. If this is empty, it will default to stdout.

logType is "json" or "text". If this is empty, it will default to "json".

func (*ZapLogger) Close

func (z *ZapLogger) Close() error

Close will perform the Logger.Sync() specifically for zap.

func (*ZapLogger) Print

func (z *ZapLogger) Print(level string, msg string, data ...map[string]interface{}) error

Print is the main method, and implements interface Logger.

Directories

Path Synopsis
example

Jump to

Keyboard shortcuts

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