logger

package module
v0.1.3 Latest Latest
Warning

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

Go to latest
Published: Mar 26, 2021 License: MIT Imports: 13 Imported by: 0

README

Logger

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

Usage

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

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

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=false

Test

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

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Init

func Init(options ...Option) error

Helper functions on DefaultLogger

Types

type Format

type Format string

Log 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