log

package module
v0.5.0 Latest Latest
Warning

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

Go to latest
Published: Jun 27, 2022 License: Apache-2.0 Imports: 4 Imported by: 4

README

GoDoc

log

import "github.com/netbookai/log"

Package log provides a minimal interface for structured logging in services. ColdBrew uses this log package for all logs.

How To Use

The simplest way to use this package is by calling static log functions to report particular level (error/warning/info/debug)

log.Error(...)
log.Warn(...)
log.Info(...)
log.Debug(...)

You can also initialize a new logger by calling 'log.NewLogger' and passing a loggers.BaseLogger implementation (loggers package provides a number of pre built implementations)

logger := log.NewLogger(gokit.NewLogger())
logger.Info(ctx, "key", "value")

Note: Preferred logging output is in either logfmt or json format, so to facilitate these log function arguments should be in pairs of key-value

Contextual Logs

log package uses context.Context to pass additional information to logs, you can use 'loggers.AddToLogContext' function to add additional information to logs. For example in access log from service

{"@timestamp":"2018-07-30T09:58:18.262948679Z","caller":"http/http.go:66","error":null,"grpcMethod":"/AuthSvc.AuthService/Authenticate","level":"info","method":"POST","path":"/2.0/authenticate/","took":"1.356812ms","trace":"15592e1b-93df-11e8-bdfd-0242ac110002","transport":"http"}

we pass 'grpcMethod' from context, this information gets automatically added to all log calls called inside the service and makes debugging services much easier. ColdBrew also generates a 'trace' ID per request, this can be used to trace an entire request path in logs.

this package is based on https://github.com/carousell/Orion/tree/master/utils/log

Index

func Debug

func Debug(ctx context.Context, args ...interface{})

Debug writes out a debug log to global logger

func Error

func Error(ctx context.Context, args ...interface{})

Error writes out an error log to global logger

func GetLevel

func GetLevel() loggers.Level

GetLevel returns the current log level

func Info

func Info(ctx context.Context, args ...interface{})

Info writes out an info log to global logger

func SetLevel

func SetLevel(level loggers.Level)

SetLevel sets the log level to filter logs

func SetLogger

func SetLogger(l Logger)

SetLogger sets the global logger

func Warn

func Warn(ctx context.Context, args ...interface{})

Warn writes out a warning log to global logger

type Logger

Logger interface is implemnted by the log implementation

type Logger interface {
    loggers.BaseLogger
    Debug(ctx context.Context, args ...interface{})
    Info(ctx context.Context, args ...interface{})
    Warn(ctx context.Context, args ...interface{})
    Error(ctx context.Context, args ...interface{})
}
func GetLogger
func GetLogger() Logger

GetLogger returns the global logger

func NewLogger
func NewLogger(log loggers.BaseLogger) Logger

NewLogger creates a new logger with a provided BaseLogger

Generated by gomarkdoc

Documentation

Overview

Package log provides a minimal interface for structured logging in services. ColdBrew uses this log package for all logs.

How To Use

The simplest way to use this package is by calling static log functions to report particular level (error/warning/info/debug)

log.Error(...)
log.Warn(...)
log.Info(...)
log.Debug(...)

You can also initialize a new logger by calling 'log.NewLogger' and passing a loggers.BaseLogger implementation (loggers package provides a number of pre built implementations)

logger := log.NewLogger(gokit.NewLogger())
logger.Info(ctx, "key", "value")

Note:

Preferred logging output is in either logfmt or json format, so to facilitate these log function arguments should be in pairs of key-value

Contextual Logs

log package uses context.Context to pass additional information to logs, you can use 'loggers.AddToLogContext' function to add additional information to logs. For example in access log from service

{"@timestamp":"2018-07-30T09:58:18.262948679Z","caller":"http/http.go:66","error":null,"grpcMethod":"/AuthSvc.AuthService/Authenticate","level":"info","method":"POST","path":"/2.0/authenticate/","took":"1.356812ms","trace":"15592e1b-93df-11e8-bdfd-0242ac110002","transport":"http"}

we pass 'grpcMethod' from context, this information gets automatically added to all log calls called inside the service and makes debugging services much easier. ColdBrew also generates a 'trace' ID per request, this can be used to trace an entire request path in logs.

this package is based on https://github.com/carousell/Orion/tree/master/utils/log

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Debug

func Debug(ctx context.Context, args ...interface{})

Debug writes out a debug log to global logger

func Error

func Error(ctx context.Context, args ...interface{})

Error writes out an error log to global logger

func GetLevel

func GetLevel() loggers.Level

GetLevel returns the current log level

func Info

func Info(ctx context.Context, args ...interface{})

Info writes out an info log to global logger

func SetLevel

func SetLevel(level loggers.Level)

SetLevel sets the log level to filter logs

func SetLogger

func SetLogger(l Logger)

SetLogger sets the global logger

func Warn

func Warn(ctx context.Context, args ...interface{})

Warn writes out a warning log to global logger

Types

type Logger

type Logger interface {
	loggers.BaseLogger
	Debug(ctx context.Context, args ...interface{})
	Info(ctx context.Context, args ...interface{})
	Warn(ctx context.Context, args ...interface{})
	Error(ctx context.Context, args ...interface{})
}

Logger interface is implemnted by the log implementation

func GetLogger

func GetLogger() Logger

GetLogger returns the global logger

func NewLogger

func NewLogger(log loggers.BaseLogger) Logger

NewLogger creates a new logger with a provided BaseLogger

Directories

Path Synopsis
Package loggers provides loggers implementation for log package
Package loggers provides loggers implementation for log package
gokit
Package gokit provides BaseLogger implementation for go-kit/log
Package gokit provides BaseLogger implementation for go-kit/log
logrus
Package logrus provides a BaseLogger implementation for logrus
Package logrus provides a BaseLogger implementation for logrus
stdlog
Package stdlog provides a BaseLogger implementation for golang "log" package
Package stdlog provides a BaseLogger implementation for golang "log" package
zap
Package zap provides a BaseLogger implementation for uber/zap
Package zap provides a BaseLogger implementation for uber/zap
Package wrap provides multiple wrap functions to wrap log implementation of other log packages
Package wrap provides multiple wrap functions to wrap log implementation of other log packages

Jump to

Keyboard shortcuts

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