logs

package module
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Oct 7, 2022 License: MIT Imports: 5 Imported by: 0

README

logs-go

logs-go is a logging library wrapped around zerolog.

It sacrifices a bit of zerolog's advantage of low allocation and instead improves usability. In most cases, it can be used as-is, but in situations where performance must be pursued, it is also possible to call zerolog's API.

Usage

Global logger

Source:

package main

import logs "github.com/rtkym/logs-go"

func main() {
	logs.Set("key", "val")
	logs.V("foo", "1").Info("hoge")
	logs.V("bar", "2").Info("fuga")
}

Output:

{"level":"info","key":"val","foo":"1","time":"2022-08-16T14:05:47.215002900+09:00","message":"hoge"}
{"level":"info","key":"val","bar":"2","time":"2022-08-16T14:05:47.263814600+09:00","message":"fuga"}
Logger

Source:

package main

import logs "github.com/rtkym/logs-go"

func main() {
	logger := logs.New()
	logger.Set("key", "val")
	logger.V("foo", "1").Info("hoge")
	logger.V("bar", "2").Info("fuga")
}

Output:

{"level":"info","key":"val","foo":"1","time":"2022-08-16T14:10:41.535728100+09:00","message":"hoge"}
{"level":"info","key":"val","bar":"2","time":"2022-08-16T14:10:41.575108000+09:00","message":"fuga"}
With context
ctx := context.Background()
logCtx := &optctx.OptCtx{ /* some configuration options */}
ctx = optctx.NewContext(ctx, logCtx)
logger := optctx.NewLogger(ctx)

Environments

LOG_LEVEL

Supported values ​​for the environment variable LOG_LEVEL are trace, debug, info, warning, error and fatal. default value is info.

LOG_FORMAT

Supported values ​​for the environment variable LOG_FORMAT are json and console. default value is json.

Layout (writer)

JSON
logger := logs.New()
logger := logs.NewWithOption()
logger := logs.NewWithOption(logs.OptionWriter("json"))
logger := logs.NewWithOption(logs.OptionJSONWriter())
Console
logger := logs.NewWithOption(logs.OptionWriter("console"))
logger := logs.NewWithOption(logs.OptionConsoleWriter())
Other
buf := &bytes.Buffer{}
logger := logs.NewWithOption(func(opt *logs.Option) { opt.Writer = buf })

Documentation

Index

Constants

View Source
const DefaultDatetimeFormat = "2006-01-02T15:04:05.000000000Z07:00"

Variables

View Source
var (

	// GlobalLoggerOptions is gLogger configurations options.
	GlobalLoggerOptions []OptionFunc // nolint:gochecknoglobals
)

Functions

func Debug

func Debug(msg string)

Debug outputs messages at debug level.

func Error

func Error(msg string)

Error outputs messages at error level.

func Fatal

func Fatal(msg string)

Fatal outputs messages at fatal level.

func Info

func Info(msg string)

Info outputs messages at info level.

func InitGlobalLogger

func InitGlobalLogger()

InitGlobalLogger initialize global logger

func Set

func Set(key string, value interface{})

Set saves key and value to logger. The key and value are output permanently

func SetWithZC

func SetWithZC(fn func(zc ZC) ZC)

SetWithZC saves key and value to logger. The key and value are output permanently

func Trace

func Trace(msg string)

Trace outputs messages at trace level.

func Warn

func Warn(msg string)

Warn outputs messages at warn level.

func With

func With() zerolog.Context

With gets zerolog.Context

Types

type LogEntry

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

LogEntry is one record of logging.

func E

func E(err error) *LogEntry

E adds error attribute to log message.

func Entry

func Entry() *LogEntry

Entry returns a new LogEntry

func V

func V(key string, value interface{}) *LogEntry

V adds key and value attribute to log message.

func (*LogEntry) Debug

func (x *LogEntry) Debug(msg string)

Debug outputs messages at debug level.

func (*LogEntry) E

func (x *LogEntry) E(err error) *LogEntry

E adds error attribute to log message.

func (*LogEntry) Error

func (x *LogEntry) Error(msg string)

Error outputs messages at error level.

func (*LogEntry) Fatal

func (x *LogEntry) Fatal(msg string)

Fatal outputs messages at fatal level.

func (*LogEntry) Info

func (x *LogEntry) Info(msg string)

Info outputs messages at info level.

func (*LogEntry) Trace

func (x *LogEntry) Trace(msg string)

Trace outputs messages at trace level.

func (*LogEntry) V

func (x *LogEntry) V(key string, value interface{}) *LogEntry

V adds key and value attribute to log message.

func (*LogEntry) Warn

func (x *LogEntry) Warn(msg string)

Warn outputs messages at warn level.

type Logger

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

Logger provides basic logging functionality.

func New

func New() *Logger

New returns a new Logger.

func NewWithOption

func NewWithOption(opts ...OptionFunc) *Logger

NewWithOption returns a new Logger with options.

func (*Logger) Debug

func (x *Logger) Debug(msg string)

Debug outputs messages at debug level.

func (*Logger) E

func (x *Logger) E(err error) *LogEntry

E adds error attribute to log message.

func (*Logger) Entry

func (x *Logger) Entry() *LogEntry

Entry returns a new LogEntry

func (*Logger) Error

func (x *Logger) Error(msg string)

Error outputs messages at error level.

func (*Logger) Fatal

func (x *Logger) Fatal(msg string)

Fatal outputs messages at fatal level.

func (*Logger) Info

func (x *Logger) Info(msg string)

Info outputs messages at info level.

func (*Logger) Set

func (x *Logger) Set(key string, value interface{})

Set saves key and value attribute to logger. The attribute are output permanently.

func (*Logger) SetWithZC

func (x *Logger) SetWithZC(fn func(zc ZC) ZC)

SetWithZC saves key and value to logger. The key and value are output permanently

func (*Logger) Trace

func (x *Logger) Trace(msg string)

Trace outputs messages at trace level.

func (*Logger) V

func (x *Logger) V(key string, value interface{}) *LogEntry

V adds key and value attribute to log message.

func (*Logger) Warn

func (x *Logger) Warn(msg string)

Warn outputs messages at warn level.

func (*Logger) With

func (x *Logger) With() zerolog.Context

With gets zerolog.Context

type Option

type Option struct {
	Level  zerolog.Level
	Writer io.Writer
}

type OptionFunc

type OptionFunc func(opt *Option)

func OptionConsoleWriter

func OptionConsoleWriter() OptionFunc

OptionConsoleWriter returns an OptionFunc for configuring console format.

func OptionJSONWriter

func OptionJSONWriter() OptionFunc

OptionJSONWriter returns an OptionFunc for configuring json format.

func OptionLevel

func OptionLevel(level string) OptionFunc

OptionLevel returns an OptionFunc for configuring log levels.

func OptionWriter

func OptionWriter(format string) OptionFunc

OptionWriter returns an OptionFunc for configuring log format.

type ZC

type ZC = zerolog.Context

ZC is alias for zerolog.Context.

Directories

Path Synopsis
examples

Jump to

Keyboard shortcuts

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