log

package module
v0.3.2 Latest Latest
Warning

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

Go to latest
Published: Jul 18, 2023 License: MIT Imports: 8 Imported by: 9

README

Log

Installation

go get github.com/facily-tech/go-core/log

Usage

See examples documentation.

Documentation

Overview

Package log is a interface for anyone who do not want to implement their own interface to popular logs. log has integration with telemetry trace for continuous liking between web request session at log.

Package log is a generated GoMock package.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Field

type Field struct {
	Key   string
	Value interface{}
}

Field is the way the parameters is received by logger.

func Any

func Any(key string, value interface{}) Field

Any receive any value to be add into logger.

func Error

func Error(value error) Field

Error handle errors to be given to looger function.

type Fields

type Fields struct {
	CTX    context.Context //nolint:containedctx // sadly required
	Fields []Field
}

Fields are the slice of fields that are handled internaly by logger function.

type Logger

type Logger interface {
	// Error logs a message at ErrorLevel. The message includes any fields passed at the log site, as well as any
	// fields accumulated on the logger.
	Error(ctx context.Context, msg string, fields ...Field)
	// Debug logs a message at DebugLevel. The message includes any fields passed at the log site, as well as any
	// fields accumulated on the logger.
	Debug(ctx context.Context, msg string, fields ...Field)
	// Fatal logs a message at FatalLevel. The message includes any fields passed at the log site, as well as any
	// fields accumulated on the logger. The logger then calls os.Exit(1), even if logging at FatalLevel is disabled.
	// Defer aren't executed before exit! Use only in appropriated places like simple main() without defer.
	Fatal(ctx context.Context, msg string, fields ...Field)
	// Info logs a message at InfoLevel. The message includes any fields passed at the log site, as well as any fields
	// accumulated on the logger.
	Info(ctx context.Context, msg string, fields ...Field)
	// Panic logs a message at PanicLevel. The message includes any fields passed at the log site, as well as any fields
	// accumulated on the logger. The logger then panics, even if logging at PanicLevel is disabled.
	Panic(ctx context.Context, msg string, fields ...Field)
	// Warn logs a message at WarnLevel. The message includes any fields passed at the log site, as well as any fields
	// accumulated on the logger.
	Warn(ctx context.Context, msg string, fields ...Field)
	// With create a new instance of Logger using fields.
	With(ctx context.Context, fields ...Field) Logger
}

Logger defines a common contract we should follow for each new log provider.

type MockLogger

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

MockLogger is a mock of Logger interface.

func NewMockLogger

func NewMockLogger(ctrl *gomock.Controller) *MockLogger

NewMockLogger creates a new mock instance.

func (*MockLogger) Debug

func (m *MockLogger) Debug(arg0 context.Context, arg1 string, arg2 ...Field)

Debug mocks base method.

func (*MockLogger) EXPECT

func (m *MockLogger) EXPECT() *MockLoggerMockRecorder

EXPECT returns an object that allows the caller to indicate expected use.

func (*MockLogger) Error

func (m *MockLogger) Error(arg0 context.Context, arg1 string, arg2 ...Field)

Error mocks base method.

func (*MockLogger) Fatal

func (m *MockLogger) Fatal(arg0 context.Context, arg1 string, arg2 ...Field)

Fatal mocks base method.

func (*MockLogger) Info

func (m *MockLogger) Info(arg0 context.Context, arg1 string, arg2 ...Field)

Info mocks base method.

func (*MockLogger) Panic

func (m *MockLogger) Panic(arg0 context.Context, arg1 string, arg2 ...Field)

Panic mocks base method.

func (*MockLogger) Warn

func (m *MockLogger) Warn(arg0 context.Context, arg1 string, arg2 ...Field)

Warn mocks base method.

func (*MockLogger) With added in v0.3.2

func (m *MockLogger) With(arg0 context.Context, arg1 ...Field) Logger

With mocks base method.

type MockLoggerMockRecorder

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

MockLoggerMockRecorder is the mock recorder for MockLogger.

func (*MockLoggerMockRecorder) Debug

func (mr *MockLoggerMockRecorder) Debug(arg0, arg1 interface{}, arg2 ...interface{}) *gomock.Call

Debug indicates an expected call of Debug.

func (*MockLoggerMockRecorder) Error

func (mr *MockLoggerMockRecorder) Error(arg0, arg1 interface{}, arg2 ...interface{}) *gomock.Call

Error indicates an expected call of Error.

func (*MockLoggerMockRecorder) Fatal

func (mr *MockLoggerMockRecorder) Fatal(arg0, arg1 interface{}, arg2 ...interface{}) *gomock.Call

Fatal indicates an expected call of Fatal.

func (*MockLoggerMockRecorder) Info

func (mr *MockLoggerMockRecorder) Info(arg0, arg1 interface{}, arg2 ...interface{}) *gomock.Call

Info indicates an expected call of Info.

func (*MockLoggerMockRecorder) Panic

func (mr *MockLoggerMockRecorder) Panic(arg0, arg1 interface{}, arg2 ...interface{}) *gomock.Call

Panic indicates an expected call of Panic.

func (*MockLoggerMockRecorder) Warn

func (mr *MockLoggerMockRecorder) Warn(arg0, arg1 interface{}, arg2 ...interface{}) *gomock.Call

Warn indicates an expected call of Warn.

func (*MockLoggerMockRecorder) With added in v0.3.2

func (mr *MockLoggerMockRecorder) With(arg0 interface{}, arg1 ...interface{}) *gomock.Call

With indicates an expected call of With.

type Zap

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

Zap wraps a zap.Logger and implements Logger inteface.

func NewLoggerZap

func NewLoggerZap(config ZapConfig) (*Zap, error)

NewLoggerZap implements Logger using uber zap structured log package.

Example
log, err := NewLoggerZap(ZapConfig{
	Version:           "v0.1.0",
	DisableStackTrace: false,
})
if err != nil {
	// panic should not be used outside func main
	// but it is not possible to return on it tests
	panic(err)
}

ctx := context.Background()
log.Info(ctx, "new log created successfully")
Output:

Example (WithTracing)
version := "v0.1.0"

// If is there already a tracer instance of telemetry you should use it
// this imsplementation exists only for example how to add a log tracer on it
tracer, err := telemetry.NewDataDog(telemetry.DataDogConfig{
	Env:     "Local",   // environment one of: local, development, homolog, production.
	Service: "Service", // application service name, it should be the same of it repository ( Github, Bitbucket, etc ).
	Version: version,   // Code Version, if there is no version control, then use Git Commit Hash instead.
})
if err != nil {
	// panic should not be used outside func main
	// but it is not possible to return on it tests
	panic(err)
}

log, err := NewLoggerZap(ZapConfig{
	Version:           version,
	DisableStackTrace: false,
	Tracer:            tracer,
})
if err != nil {
	// panic should not be used outside func main
	// but it is not possible to return on it tests
	panic(err)
}

ctx := context.Background()
log.Info(ctx, "new log created successfully")
Output:

func (*Zap) Debug

func (z *Zap) Debug(ctx context.Context, msg string, fields ...Field)

Debug will write a log with level debug.

func (*Zap) Error

func (z *Zap) Error(ctx context.Context, msg string, fields ...Field)

Error will write a log with level error.

func (*Zap) Fatal

func (z *Zap) Fatal(ctx context.Context, msg string, fields ...Field)

Fatal will write a log with level fatal.

func (*Zap) Info

func (z *Zap) Info(ctx context.Context, msg string, fields ...Field)

Info will write a log with level info.

func (*Zap) Panic

func (z *Zap) Panic(ctx context.Context, msg string, fields ...Field)

Panic will write a log with level panic.

func (*Zap) Warn

func (z *Zap) Warn(ctx context.Context, msg string, fields ...Field)

Warn will write a log with level warn.

func (*Zap) With added in v0.3.0

func (z *Zap) With(ctx context.Context, fields ...Field) Logger

With create a new instance of Logger using fields.

type ZapConfig

type ZapConfig struct {
	Version           string
	DisableStackTrace bool
	Tracer            telemetry.Tracer
	Debug             bool `env:"DEBUG"`
}

ZapConfig handle the config information that will be passed to zap.

Jump to

Keyboard shortcuts

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