log

package module
v0.2.7 Latest Latest
Warning

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

Go to latest
Published: Apr 23, 2023 License: MIT Imports: 10 Imported by: 16

README

glog

godoc license Coverage

中文

Glog is a library of asynchronous loggers, with configurable cache sizes to accommodate different high concurrency requirements. It also controls the different behaviors and logging methods of the logger through various fine-grained configurations.

Quick start

package main

import (
	"time"

	"github.com/ml444/glog"
)

func main() {
	log.Info()
	log.Debug("hello world")                            // No output by default
	log.Info("hello world")
	log.Warn("hello world")
	log.Error("hello world")
	time.Sleep(time.Millisecond * 10)
	log.Printf("%s alone could waken love!", "Love")    // No output by default
	log.Debugf("%s alone could waken love!", "Love")    // No output by default
	log.Infof("%s alone could waken love!", "Love")
	log.Warnf("%s alone could waken love!", "Love")
	log.Errorf("%s alone could waken love!", "Love")
}

Similar result: !QuickStart

By default, the logs are output to standard output and the logger level is set to Info level, so the Debug level logs in this example are not output. Different logging levels are identified by different colors.

General config

By default, logs are output to standard output, so for production environments, we need to make the following settings (simple):

package main

import (
	"os"
	
	"github.com/ml444/glog"
	"github.com/ml444/glog/config"
	"github.com/ml444/glog/level"
)

func main() {
	var err error
	err = log.InitLog(
		config.SetLoggerName("serviceName"),
		config.SetLevel2Logger(level.DebugLevel),
		config.SetHandlerType2Logger(config.HandlerTypeFile),
		config.SetCacheSize2Logger(1024*8),
		config.SetFileDir2Logger("/var/log"),
	)
	if err != nil {
		log.Errorf("err: %v", err)
		os.Exit(-1)
	}
	// doing something
	log.Info("hello world")
	// doing something

	log.Exit()
}

More detailed configuration can be seen in the code: config/option.go and config/config.go. In the log storage selection with files, use the rolling way to keep the files, the default value to keep the latest 24 copies, you can adjust the number of backups according to your actual needs SetFileBackupCount2Logger(). And the way of scrolling can be done by scrolling by specified size (FileRotatorTypeTime), scrolling by time (FileRotatorTypeSize), scrolling by time and size common limit (FileRotatorTypeTimeAndSize). The third type of FileRotatorTypeTimeAndSize is described here in particular. It scrolls by time, but when it reaches the specified size limit, it stops logging and discards the rest of the log until the next point in time before a new file starts. This is done to protect the server's disk.

Enum of levels

To be compatible with the logging levels of the standard library, three levels of print, fatal and panic have been added.

package level

type LogLevel int8
const (
	DebugLevel LogLevel = iota + 1
	PrintLevel
	InfoLevel
	WarnLevel
	ErrorLevel
	FatalLevel
	PanicLevel
)

Report feature

In production environment sometimes we need to store some special logs, such as:

  1. keep the error logs to keep them for a longer period of time. To facilitate us to trace some bugs.
  2. when some high level logs need to be notified by the system alarm component, developers do not need to develop these special logging components.
  3. when some special logs need special operations, such as operation logs into the database.
  4. etc.

By enabling the report feature and combining it with the filter function to filter the required data and perform special operations, it makes the style and management uniform and developer-friendly.

package main

import (
	"os"

	"github.com/ml444/glog"
	"github.com/ml444/glog/config"
	"github.com/ml444/glog/level"
)

func main() {
	var err error
	err = log.InitLog(
		config.SetLoggerName("serviceName"),
		config.SetLevel2Logger(level.DebugLevel),
		config.SetHandlerType2Logger(config.HandlerTypeFile),
		config.SetCacheSize2Logger(1024*8),
		config.SetFileDir2Logger("/var/log"),

		config.SetEnableReport(),
		config.SetLevel2Logger(level.WarnLevel),
		config.SetHandlerType2Logger(config.HandlerTypeFile),
		config.SetFileRotatorType2Report(config.FileRotatorTypeSize),
		config.SetCacheSize2Logger(1024*2),
		config.SetFileDir2Logger("/var/report"),
	)
	if err != nil {
		log.Errorf("err: %v", err)
		os.Exit(-1)
	}
	// doing something
	log.Info("hello world")
	// doing something

	log.Exit()
}

Pattern template

When using text format mode to output logs, you can control the display data and the order of each message in the log by configuring a pattern template. The default is: PatternTemplate1 = "%[LoggerName]s (%[Pid]d,%[RoutineId]d) %[DateTime]s.%[Msecs]d %[LevelName]s %[Caller]s %[Message]v" You can adjust the order of the fields and add or remove them yourself, for example: "<<%[Pid]d,%[RoutineId]d>> %[LoggerName]s %[DateTime]s %[LevelName]s %[Message]v %[Caller]s" You can configure it to your liking. The following are all the options that can be configured:

%[LoggerName]s      the name of the logger.
%[LevelNo]s         the logging level in numeric form.
%[LevelName]s       the text name of the logging level.
%[Caller]s          Execution logging call (including file path and name, line number, function name).
%[File]s            The path and file name of the source file where the logging call is executed.
%[Func]s            The name of the function called in the execution log.
%[Line]d            The line number of the execution log call.
%[DateTime]s        The time of the log execution.
%[Msecs]d           The millisecond portion of the execution logging time.
%[TradeId]s         the ID of the context trace.
%[IP]s              local IP address of the server.
%[HostName]s        Server host name.
%[Pid]d             Process ID.
%[RoutineId]d       Concurrent process ID.
%[Message]s         Message recorded.

Note: In systems with microservices, a similar TradeId is typically present to assist in stringing together the entire call chain. glog makes the logger automatically get the TradeId by configuring the hook function of TradeIDFunc.

Documentation

Index

Constants

This section is empty.

Variables

Functions

func Debug

func Debug(args ...interface{})

func Debugf

func Debugf(template string, args ...interface{})

func Error

func Error(args ...interface{})

func Errorf

func Errorf(template string, args ...interface{})

func Exit

func Exit()

func Fatal

func Fatal(args ...interface{})

func Fatalf

func Fatalf(template string, args ...interface{})

func Info

func Info(args ...interface{})

func Infof

func Infof(template string, args ...interface{})

func InitLog

func InitLog(opts ...config.OptionFunc) error

func Panic

func Panic(args ...interface{})

func Panicf

func Panicf(template string, args ...interface{})

func Print

func Print(args ...interface{})

func Printf

func Printf(template string, args ...interface{})

func SetLogger

func SetLogger(g ILogger)

func Warn

func Warn(args ...interface{})

func Warnf

func Warnf(template string, args ...interface{})

Types

type ILogger

type ILogger interface {
	GetLevel() level.LogLevel
	SetLevel(level.LogLevel)
	EnabledLevel(lvl level.LogLevel) bool

	Debug(...interface{})
	Info(...interface{})
	Warn(...interface{})
	Error(...interface{})

	Print(...interface{})
	Fatal(...interface{})
	Panic(...interface{})

	Debugf(template string, args ...interface{})
	Infof(template string, args ...interface{})
	Warnf(template string, args ...interface{})
	Errorf(template string, args ...interface{})

	Printf(template string, args ...interface{})
	Fatalf(template string, args ...interface{})
	Panicf(template string, args ...interface{})

	Stop() error
}

func GetLogger

func GetLogger() ILogger

type Logger

type Logger struct {
	Name  string
	Level level.LogLevel

	TradeIDFunc    func(entry *message.Entry) string
	ExitFunc       func(code int) // Function to exit the application, defaults to `os.Exit()`
	ExitOnFatal    bool
	IsRecordCaller bool
	// contains filtered or unexported fields
}

func NewLogger

func NewLogger(cfg *config.Config) (*Logger, error)

NewLogger returns a new ILogger

func (*Logger) Debug

func (l *Logger) Debug(args ...interface{})

func (*Logger) Debugf

func (l *Logger) Debugf(template string, args ...interface{})

func (*Logger) EnabledLevel

func (l *Logger) EnabledLevel(lvl level.LogLevel) bool

func (*Logger) Error

func (l *Logger) Error(args ...interface{})

func (*Logger) Errorf

func (l *Logger) Errorf(template string, args ...interface{})

func (*Logger) Fatal

func (l *Logger) Fatal(args ...interface{})

func (*Logger) Fatalf

func (l *Logger) Fatalf(template string, args ...interface{})

func (*Logger) Fatalln

func (l *Logger) Fatalln(args ...interface{})

func (*Logger) GetLevel

func (l *Logger) GetLevel() level.LogLevel

func (*Logger) Info

func (l *Logger) Info(args ...interface{})

func (*Logger) Infof

func (l *Logger) Infof(template string, args ...interface{})

func (*Logger) Panic

func (l *Logger) Panic(args ...interface{})

func (*Logger) Panicf

func (l *Logger) Panicf(template string, args ...interface{})

func (*Logger) Panicln

func (l *Logger) Panicln(args ...interface{})

func (*Logger) Print

func (l *Logger) Print(args ...interface{})

func (*Logger) Printf

func (l *Logger) Printf(template string, args ...interface{})

func (*Logger) Println

func (l *Logger) Println(args ...interface{})

func (*Logger) SetLevel

func (l *Logger) SetLevel(lvl level.LogLevel)

func (*Logger) Stop

func (l *Logger) Stop() error

func (*Logger) Warn

func (l *Logger) Warn(args ...interface{})

func (*Logger) Warnf

func (l *Logger) Warnf(template string, args ...interface{})

type StdLogger

type StdLogger interface {
	Print(...interface{})
	Println(...interface{})
	Printf(string, ...interface{})

	Fatal(...interface{})
	Fatalln(...interface{})
	Fatalf(string, ...interface{})

	Panic(...interface{})
	Panicln(...interface{})
	Panicf(string, ...interface{})
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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