log

package module
v1.0.2 Latest Latest
Warning

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

Go to latest
Published: May 26, 2022 License: MIT Imports: 16 Imported by: 1

README

logger

CI

Go logging library.

Features

  • simplest Log interface for business-adapted log access.
  • std log implementation.
  • zerolog implementation.

Usage

std log
package log_test

import (
	"github.com/sraphs/go/log"
	"github.com/sraphs/go/log/stdlog"
)

func Example_stdlog() {
	stdlog.RegisterGlobal()

	log.Debug("hello world")

	// Outputs:
	// DEBUG: 2022/05/17 16:07:38.303205 example_test.go:21: hello world
}

zerolog
package log_test

import (
	"github.com/sraphs/go/log"
	"github.com/sraphs/go/log/zerolog"
)

func Example_zerolog() {
	zerolog.RegisterGlobal()

	log.Debug("hello world")

	// Outputs:
	// {"level":"debug","msg":"hello world","ts":"2022-05-17T15:58:44+08:00","caller":"example_test.go:11"}
}

Contributing

We alway welcome your contributions 👏

  1. Fork the repository
  2. Create Feat_xxx branch
  3. Commit your code
  4. Create Pull Request

CHANGELOG

See Releases

License

MIT © sraph.com

Documentation

Overview

Package log provides a global logger for log.

Example (Zerolog)
package main

import (
	"github.com/sraphs/log"
)

func main() {
	log.Init(nil)
	log.Debug("hello world")

	// Outputs:
	// {"level":"debug","msg":"hello world","ts":"2022-05-27T00:22:01+08:00","caller":"example_test.go:9"}
}
Output:

Index

Examples

Constants

This section is empty.

Variables

View Source
var (
	Level_name = map[int32]string{
		0:   "DEFAULT",
		100: "DEBUG",
		200: "INFO",
		300: "WARN",
		400: "ERROR",
		500: "FATAL",
		600: "PANIC",
	}
	Level_value = map[string]int32{
		"DEFAULT": 0,
		"DEBUG":   100,
		"INFO":    200,
		"WARN":    300,
		"ERROR":   400,
		"FATAL":   500,
		"PANIC":   600,
	}
)

Enum value maps for Level.

View Source
var (
	// TimestampFieldName is the field name used for the timestamp field.
	TimestampFieldName = "ts"

	// LevelFieldName is the field name used for the level field.
	LevelFieldName = "level"

	// LevelTraceValue is the value used for the trace level field.
	LevelTraceValue = "trace"
	// LevelDebugValue is the value used for the debug level field.
	LevelDebugValue = "debug"
	// LevelInfoValue is the value used for the info level field.
	LevelInfoValue = "info"
	// LevelWarnValue is the value used for the warn level field.
	LevelWarnValue = "warn"
	// LevelErrorValue is the value used for the error level field.
	LevelErrorValue = "error"
	// LevelFatalValue is the value used for the fatal level field.
	LevelFatalValue = "fatal"
	// LevelPanicValue is the value used for the panic level field.
	LevelPanicValue = "panic"

	// LevelFieldMarshalFunc allows customization of global level field marshaling.
	LevelFieldMarshalFunc = func(l zlog.Level) string {
		return l.String()
	}

	// MessageFieldName is the field name used for the message field.
	MessageFieldName = "msg"

	// ErrorFieldName is the field name used for error fields.
	ErrorFieldName = "error"

	// CallerFieldName is the field name used for caller field.
	CallerFieldName = "caller"

	// CallerSkipFrameCount is the number of stack frames to skip to find the caller.
	CallerSkipFrameCount = 2 + 1

	// CallerMarshalFunc allows customization of global caller marshaling
	CallerMarshalFunc = func(file string, line int) string {
		short := file
		for i := len(file) - 1; i > 0; i-- {
			if file[i] == '/' {
				short = file[i+1:]
				break
			}
		}
		file = short
		return file + ":" + strconv.Itoa(line)
	}

	// ErrorStackFieldName is the field name used for error stacks.
	ErrorStackFieldName = "stack"

	// ErrorStackMarshaler extract the stack from err if any.
	ErrorStackMarshaler = pkgerrors.MarshalStack

	// ErrorMarshalFunc allows customization of global error marshaling
	ErrorMarshalFunc = func(err error) interface{} {
		return err
	}

	// InterfaceMarshalFunc allows customization of interface marshaling.
	// Default: "encoding/json.Marshal"
	InterfaceMarshalFunc = json.Marshal

	// TimeFieldFormat defines the time format of the Time field type. If set to
	// TimeFormatUnix, TimeFormatUnixMs or TimeFormatUnixMicro, the time is formatted as an UNIX
	// timestamp as integer.
	TimeFieldFormat = time.RFC3339

	// TimestampFunc defines the function called to generate a timestamp.
	TimestampFunc = time.Now

	// DurationFieldUnit defines the unit for time.Duration type fields added
	// using the Dur method.
	DurationFieldUnit = time.Millisecond

	// DurationFieldInteger renders Dur fields as integer instead of float if
	// set to true.
	DurationFieldInteger = false

	// ErrorHandler is called whenever zerolog fails to write an event on its
	// output. If not set, an error is printed on the stderr. This handler must
	// be thread safe and non-blocking.
	ErrorHandler func(err error)
)
View Source
var (
	MultiLevelWriter = zlog.MultiLevelWriter
)

Functions

func Debug

func Debug(v ...interface{})

func Error

func Error(v ...interface{})

func Fatal

func Fatal(v ...interface{})

func Info

func Info(v ...interface{})

func IsLevelEnabled

func IsLevelEnabled(lvl Level) bool

func Panic

func Panic(v ...interface{})

func SetLevel

func SetLevel(l Level)

SetLevel sets the current global log level.

func SetOptions added in v1.0.1

func SetOptions(opts ...Option)

SetOptions sets the global logger options.

func SetOutput added in v1.0.1

func SetOutput(w io.Writer)

SetOutput sets the global logger output.

func Warn

func Warn(v ...interface{})

Types

type Config added in v1.0.1

type Config struct {

	// level is the minimum severity level at which to log. e.g. "INFO".
	Level Level `protobuf:"varint,1,opt,name=level,proto3,enum=sraph.log.Level" json:"level,omitempty"`
	// path to the log file. e.g. "/var/log/my_app.log"
	Path string `protobuf:"bytes,2,opt,name=path,proto3" json:"path,omitempty"`
	// max_size is the maximum size to store in the log file. unit is MB.
	MaxSize int32 `protobuf:"varint,3,opt,name=max_size,json=maxSize,proto3" json:"max_size,omitempty"`
	// max_age is the maximum age of the log file. unit is days.
	MaxAge int32 `protobuf:"varint,4,opt,name=max_age,json=maxAge,proto3" json:"max_age,omitempty"`
	// contains filtered or unexported fields
}

func (*Config) Descriptor deprecated added in v1.0.1

func (*Config) Descriptor() ([]byte, []int)

Deprecated: Use Config.ProtoReflect.Descriptor instead.

func (*Config) GetLevel added in v1.0.1

func (x *Config) GetLevel() Level

func (*Config) GetMaxAge added in v1.0.1

func (x *Config) GetMaxAge() int32

func (*Config) GetMaxSize added in v1.0.1

func (x *Config) GetMaxSize() int32

func (*Config) GetPath added in v1.0.1

func (x *Config) GetPath() string

func (*Config) ProtoMessage added in v1.0.1

func (*Config) ProtoMessage()

func (*Config) ProtoReflect added in v1.0.1

func (x *Config) ProtoReflect() protoreflect.Message

func (*Config) Reset added in v1.0.1

func (x *Config) Reset()

func (*Config) String added in v1.0.1

func (x *Config) String() string

type Level added in v1.0.1

type Level int32

The severity of the event described in a log entry

const (
	// (0) The log entry has no assigned severity level.
	Level_DEFAULT Level = 0
	// (100) Debug or trace information.
	Level_DEBUG Level = 100
	// (200) Routine information, such as start up, shut down, ongoing status,
	// performance, or a configuration change.
	Level_INFO Level = 200
	// (300) Warning events might cause problems.
	Level_WARN Level = 300
	// (400) Error events are likely to cause problems.
	Level_ERROR Level = 400
	// (500) Fatal events cause more severe problems or outages.
	Level_FATAL Level = 500
	// (600) Panic events cause panic-like problems and panic the server.
	Level_PANIC Level = 600
)

func GetLevel

func GetLevel() Level

GetLevel returns the current global log level.

func ParseLevel added in v1.0.1

func ParseLevel(lvl string) Level

ParseLevel takes a string level and returns the logger log level constant.

func (Level) Descriptor added in v1.0.1

func (Level) Descriptor() protoreflect.EnumDescriptor

func (Level) Enum added in v1.0.1

func (x Level) Enum() *Level

func (Level) EnumDescriptor deprecated added in v1.0.1

func (Level) EnumDescriptor() ([]byte, []int)

Deprecated: Use Level.Descriptor instead.

func (Level) Number added in v1.0.1

func (x Level) Number() protoreflect.EnumNumber

func (Level) String added in v1.0.1

func (x Level) String() string

func (Level) Type added in v1.0.1

func (Level) Type() protoreflect.EnumType

type Logger added in v1.0.1

type Logger interface {
	Log(lvl Level, v ...interface{}) error
}

Logger defines as interface a writer may implement in order to receive level information with payload.

func GetLogger

func GetLogger() Logger

GetLogger returns the current global

func Init

func Init(c *Config) Logger

type Option added in v1.0.1

type Option func(zlog.Context) zlog.Context

Options returns the options used to create the

func WithCaller added in v1.0.1

func WithCaller() Option

func WithCallerWithSkipFrameCount added in v1.0.1

func WithCallerWithSkipFrameCount(skipFrameCount int) Option

func WithFields added in v1.0.1

func WithFields(fields interface{}) Option

func WithStack added in v1.0.1

func WithStack() Option

func WithTimestamp added in v1.0.1

func WithTimestamp() Option

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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