logger

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Apr 8, 2020 License: Apache-2.0 Imports: 9 Imported by: 2

README

ZKits Logger Library

Build Status Coverage Status Codacy Badge

About

This package is a library of ZKits project. This library provides structured JSON format logger. At the same time, it supports 7 levels of logs and provides complete hook function to assist log scheduling.

Usage

  1. Import package.

    go get -u -v github.com/edoger/zkits-logger
    
  2. Example.

    package main
    
    import (
        "github.com/edoger/zkits-logger"
    )
    
    func main() {
        // Creates a logger instance with the specified name.
        // By default, logs are output to standard output.
        // In addition, logger only record INFO or higher level logs.
        // You can modify these settings by calling methods.
        // instance.SetOutput()
        // instance.SetLevel()
        instance := logger.New("test")
    
        // Record an info level log.
        // {"level":"info","logger":"test","message":"It's log!","time":"2006-01-02 15:04:05","fields":{}}
        instance.Info("It's log!")
    }
    

License

Apache-2.0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Hook

type Hook func(Summary) error

Hook is the log hook type. Before the log is written, the corresponding level of hooks will be executed. The hooks will be executed sequentially in synchronization until an error is encountered or all execution is complete.

type Level

type Level uint32

The log level type.

const (
	// PanicLevel indicates a very serious event at the highest level.
	// When the log is written, panic will be triggered automatically.
	PanicLevel Level = iota + 1
	// FatalLevel indicates that an event occurred that the application
	// cannot continue to run. When the log is written, the exit function
	// will be called automatically.
	FatalLevel
	// ErrorLevel indicates that an event occurred within the application
	// but does not affect continued operation.
	ErrorLevel
	// WarnLevel indicates that a noteworthy event has occurred inside
	// the application.
	WarnLevel
	// InfoLevel represents some general information events.
	InfoLevel
	// DebugLevel represents some informational events for debugging, and
	// very verbose logging.
	DebugLevel
	// TraceLevel represents the finest granular information event.
	TraceLevel
)

func MustParseLevel

func MustParseLevel(s string) Level

Parses the log level from the given string. If the given string is invalid, it will panic.

func ParseLevel

func ParseLevel(s string) (Level, error)

Parses the log level from the given string.

func (Level) IsValid

func (level Level) IsValid() bool

Determines whether the current log level is a valid value.

func (Level) String

func (level Level) String() string

Gets the string form of the current log level. If the log level is not supported, always returns "unknown".

type Log

type Log interface {
	// Gets the name of the logger.
	Name() string
	// Add an extra data to the current log.
	// This method will return a new log instance.
	WithField(string, interface{}) Log
	// Add error to the current log.
	// This method will return a new log instance.
	// This method is relative to WithField("error", error).
	WithError(error) Log
	// Add extra data to the current log.
	// This method will return a new log instance.
	WithFields(map[string]interface{}) Log
	// Add context to the current log.
	// This method will return a new log instance.
	WithContext(context.Context) Log
	// Record the current log and specify the level.
	Log(Level, ...interface{})
	Logln(Level, ...interface{})
	Logf(Level, string, ...interface{})
	// Record the current log and specify the level as TraceLevel.
	Trace(...interface{})
	Traceln(...interface{})
	Tracef(string, ...interface{})
	Print(...interface{})
	Println(...interface{})
	Printf(string, ...interface{})
	// Record the current log and specify the level as DebugLevel.
	Debug(...interface{})
	Debugln(...interface{})
	Debugf(string, ...interface{})
	// Record the current log and specify the level as InfoLevel.
	Info(...interface{})
	Infoln(...interface{})
	Infof(string, ...interface{})
	// Record the current log and specify the level as WarnLevel.
	Warn(...interface{})
	Warnln(...interface{})
	Warnf(string, ...interface{})
	Warning(...interface{})
	Warningln(...interface{})
	Warningf(string, ...interface{})
	// Record the current log and specify the level as ErrorLevel.
	Error(...interface{})
	Errorln(...interface{})
	Errorf(string, ...interface{})
	// Record the current log and specify the level as FatalLevel.
	// The exit function will be executed automatically.
	Fatal(...interface{})
	Fatalln(...interface{})
	Fatalf(string, ...interface{})
	// Record the current log and specify the level as PanicLevel.
	// Panic will be triggered automatically.
	Panic(...interface{})
	Panicln(...interface{})
	Panicf(string, ...interface{})
}

type Logger

type Logger interface {
	Log

	// Get the current logger level.
	GetLevel() Level
	// Set the current logger level.
	SetLevel(Level) Logger
	// Get the current logger output writer.
	GetOutput() io.Writer
	// Set the current logger output writer.
	SetOutput(io.Writer) Logger
	// Set the current logger system exit function.
	WithExitFunc(func(int)) Logger
	// Register hook to the current logger.
	WithHook([]Level, Hook) Logger
}

Definition of the logger.

func New

func New(name string) Logger

Create a new logger. By default, the logger level is InfoLevel and logs will be output to standard output.

type Summary

type Summary interface {
	Name() string                     // Get the name of the logger.
	Bytes() []byte                    // Get the log content and return it as bytes.
	String() string                   // Get the log content and return it as string.
	Size() int                        // Get the size of the log.
	Level() Level                     // Get the level of the log.
	Time() time.Time                  // Get the creation time of the log.
	Message() string                  // Get the message of the log.
	Fields() map[string]interface{}   // Get the bound data of the log.
	Field(string) (interface{}, bool) // Query the bound data of the log.
	Context() context.Context         // Get the context of the log.
}

Summary of the log.

Jump to

Keyboard shortcuts

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