logger

package module
v0.5.2 Latest Latest
Warning

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

Go to latest
Published: Aug 8, 2023 License: MIT Imports: 16 Imported by: 0

README

Logger

This logger basically configure zerolog so that you can log via github.com/rs/zerolog/log

Usage

Initialization

Import shared/logger/auto package. It will be self-initialized.

import  "github.com/xmlking/toolkit/logger/auto"

Other option to initialize logger is to set DefaultLogger your self. this will give more control to developer.

logger.DefaultLogger =logger.NewLogger()
logger.DefaultLogger =logger.NewLogger(logger.WithLevel(zerolog.DebugLevel), logger.WithFormat(logger.PRETTY))
logger.DefaultLogger =logger.NewLogger(logger.WithLevel(zerolog.DebugLevel), logger.WithFormat(logger.PRETTY), logger.EnableGrpcLog(true))
// with rotating file writer
lw := logger.FileWriter(
	    "test-demo.log",
        logger.FileConfig{
            MaxSize:    5,
            MaxBackups: 10,
            MaxAge:     14,
            Compress:   true,
        }
	)
logger.NewLogger(logger.WithOutput(lw), logger.WithFormat(logger.JSON), logger.WithLevel(zerolog.WarnLevel))

Once logger is initialized, then you can use standard github.com/rs/zerolog/log package's helper methods to log in your code.

Environment Variables

Your can set Logger config via Environment Variables

grpc logs are disabled by default. you can enable via CONFY_LOG_GRPC

grpc internal logs also adopt CONFY_LOG_LEVEL and CONFY_LOG_FORMAT

No need to set GRPC_GO_LOG_SEVERITY_LEVEL and GRPC_GO_LOG_VERBOSITY_LEVEL

CONFY_LOG_LEVEL=<trace,debug,info,warn,error,fatal,panic>
CONFY_LOG_FORMAT=<pretty/json/gcp>
CONFY_LOG_GRPC=true
CONFY_LOG_FILE=app1.log

Aside from logging in JSON, you can also configure Zerolog to output binary logs encoded in CBOR format. You can enable it by using the binary_log build tag while compiling your application:

go build -tags binary_log -o build ./service/engine/...

You can decode this binary log entry to JSON with any CBOR decoder, such as csd

CONFY_LOG_FORMAT=json ./build/engine  2> >(csd)

Test

CONFY_LOG_LEVEL=info  CONFY_LOG_FORMAT=json go test github.com/xmlking/toolkit/logger  -count=1

Reference

  • A Complete Guide to Logging in Go with Zerolog

Documentation

Overview

import "github.com/xmlking/toolkit/logger" to `main.go` and all test files. set log level and format in via Env: CONFY_LOG_LEVEL, CONFY_LOG_FORMAT, CONFY_LOG_GRPC

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func FileWriter added in v0.5.1

func FileWriter(filename string, c FileConfig) zerolog.LevelWriter

func Init

func Init(options ...Option) error

Init : Helper functions on DefaultLogger

Types

type FileConfig added in v0.5.1

type FileConfig struct {
	// MaxSize is the maximum size in megabytes of the log file before it gets
	// rotated. It defaults to 100 megabytes.
	MaxSize int

	// MaxAge is the maximum number of days to retain old log files based on the
	// timestamp encoded in their filename.  Note that a day is defined as 24
	// hours and may not exactly correspond to calendar days due to daylight
	// savings, leap seconds, etc. The default is not to remove old log files
	// based on age.
	MaxAge int

	// MaxBackups is the maximum number of old log files to retain.  The default
	// is to retain all old log files (though MaxAge may still cause them to get
	// deleted.)
	MaxBackups int

	// LocalTime determines if the time used for formatting the timestamps in
	// backup files is the computer's local time.  The default is to use UTC
	// time.
	LocalTime bool

	// Compress determines if the rotated log files should be compressed
	// using gzip. The default is not to perform compression.
	Compress bool
}

type Format

type Format string

Format format enum.

const (
	PRETTY Format = "pretty"
	JSON   Format = "json"
	GCP    Format = "gcp"
	AZURE  Format = "azure"
	AWS    Format = "aws"
)

func ParseFormat

func ParseFormat(formatStr string) (Format, error)

type Logger

type Logger interface {
	Init(options ...Option) error
	Options() Options
	String() string
}
var (
	// Default Logger
	DefaultLogger Logger
)

func NewLogger

func NewLogger(opts ...Option) Logger

type Option

type Option func(*Options)

func EnableGrpcLog

func EnableGrpcLog(val bool) Option

func ReportCaller

func ReportCaller() Option

ReportCaller set value to `true`

func SetOption

func SetOption(k, v interface{}) Option

func WithFields

func WithFields(fields map[string]interface{}) Option

WithFields set default fields for the logger

func WithFormat

func WithFormat(format Format) Option

WithFormat set default log format for the logger

func WithLevel

func WithLevel(level zerolog.Level) Option

WithLevel set default level for the logger

func WithOutput

func WithOutput(out io.Writer) Option

WithOutput set default output writer for the logger

func WithTimeFormat

func WithTimeFormat(timeFormat string) Option

WithTimeFormat set default timeFormat for the logger

type Options

type Options struct {
	// The logging level the logger should log at. default is `InfoLevel`
	Level zerolog.Level
	// Log format. default `json`
	Format Format
	// TimeFormat is one of time.RFC3339, time.RFC3339Nano, time.*
	TimeFormat string
	// Flag for whether to log caller info (off by default)
	ReportCaller bool
	// fields to always be logged
	Fields map[string]interface{}
	// It's common to set this to a file, or leave it default which is `os.Stderr`
	Out io.Writer
	// Enable/Disable GRPC Logs
	EnableGrpcLog bool
	// Alternative options
	Context context.Context
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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