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 ¶
- Variables
- func Debug(v ...interface{})
- func Error(v ...interface{})
- func Fatal(v ...interface{})
- func Info(v ...interface{})
- func IsLevelEnabled(lvl Level) bool
- func Panic(v ...interface{})
- func SetLevel(l Level)
- func SetOptions(opts ...Option)
- func SetOutput(w io.Writer)
- func Warn(v ...interface{})
- type Config
- func (*Config) Descriptor() ([]byte, []int)deprecated
- func (x *Config) GetLevel() Level
- func (x *Config) GetMaxAge() int32
- func (x *Config) GetMaxSize() int32
- func (x *Config) GetPath() string
- func (*Config) ProtoMessage()
- func (x *Config) ProtoReflect() protoreflect.Message
- func (x *Config) Reset()
- func (x *Config) String() string
- type Level
- type Logger
- type Option
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 File_log_proto protoreflect.FileDescriptor
View Source
var (
MultiLevelWriter = zlog.MultiLevelWriter
)
Functions ¶
func IsLevelEnabled ¶
func SetOptions ¶ added in v1.0.1
func SetOptions(opts ...Option)
SetOptions sets the global logger options.
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) GetMaxSize ¶ added in v1.0.1
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
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 ParseLevel ¶ added in v1.0.1
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) EnumDescriptor
deprecated
added in
v1.0.1
func (Level) Number ¶ added in v1.0.1
func (x Level) Number() protoreflect.EnumNumber
func (Level) Type ¶ added in v1.0.1
func (Level) Type() protoreflect.EnumType
type Logger ¶ added in v1.0.1
Logger defines as interface a writer may implement in order to receive level information with payload.
type Option ¶ added in v1.0.1
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 WithFields ¶ added in v1.0.1
func WithFields(fields interface{}) Option
func WithTimestamp ¶ added in v1.0.1
func WithTimestamp() Option
Click to show internal directories.
Click to hide internal directories.