logging

package
v0.0.8 Latest Latest
Warning

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

Go to latest
Published: Apr 13, 2020 License: MIT Imports: 10 Imported by: 0

README

Logging

Structured logging interface and providers.

Interface

The interface was intensionally reduced to only 2 methods (log, error) to reduce possibility of unintentional different log messages.

All methods receive context.Context as first parameter, a string message (so we can have a value to group) and a map of strings for building the log.

Usage

ctx := context.TODO()
l := NewMockLogger()

l.Log(
  ctx, 
  "Main log message", 
  map[string]string{
    "some":  "key",
    "value": "params"
  },
)

For this code, the expected generated log is:

{ "level": "info/error", "msg": "Main log message", "some": "key", "value": "params" }
Where

"level": "debug" is the log level
"msg": "Main log message" main string message
"some": "key", "value": "params" map parameters

Usage with Transaction Context

ctx := context.TODO()
id, _ := uuid.NewUUID()
ctx = tctx.CreateTransactionContext(ctx, id, time.Now())
  
l := NewMockLogger()

l.Log(
  ctx, 
  "Main log message", 
  map[string]string{
    "some":  "key",
    "value": "params"
  },
)

For this code, the expected generated log is:

{ "level": "info/error", "msg": "Main log message", "tid": "65345fc5-8468-4709-a21f-fcd30d9eb08f", "tms": {unix nano}, "some": "key", "value": "params" }
Where

"level": "info/error" is the log level
"msg": "Main log message" main string message
"tid": "65345fc5-8468-4709-a21f-fcd30d9eb08f" ExecutionContext transaction id
"tms": "{unix nano}" transaction started at ms
"some": "key", "value": "params" map parameters

Supported providers

uber-go/zap

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ContextLogger

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

func NewContextLogger

func NewContextLogger(l Logger) *ContextLogger

func (*ContextLogger) Error

func (cl *ContextLogger) Error(ctx context.Context, msg string, params map[string]interface{})

func (*ContextLogger) Log

func (cl *ContextLogger) Log(ctx context.Context, msg string, params map[string]interface{})

type ContextLoggerMiddleware

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

func NewContextLoggerMiddleware

func NewContextLoggerMiddleware(l *ContextLogger) *ContextLoggerMiddleware

func (*ContextLoggerMiddleware) Handler

type LogLevel

type LogLevel int
const (
	Log LogLevel = iota
	Error
)

type Logger

type Logger interface {
	Log(string, map[string]interface{})
	Error(string, map[string]interface{})
}

type LoggerConfig

type LoggerConfig struct {
	Output io.Writer
}

type MockLogger

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

func NewMockLogger

func NewMockLogger() *MockLogger

func (*MockLogger) Error

func (l *MockLogger) Error(msg string, params map[string]interface{})

func (*MockLogger) Log

func (l *MockLogger) Log(msg string, params map[string]interface{})

func (*MockLogger) String

func (l *MockLogger) String() string

type ZapLogger

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

func NewZapLogger

func NewZapLogger(cfgs []LoggerConfig) *ZapLogger

func (*ZapLogger) Error

func (l *ZapLogger) Error(msg string, params map[string]interface{})

func (*ZapLogger) Log

func (l *ZapLogger) Log(msg string, params map[string]interface{})

Jump to

Keyboard shortcuts

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