vlog

package
v1.2.0 Latest Latest
Warning

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

Go to latest
Published: Sep 9, 2024 License: Apache-2.0 Imports: 23 Imported by: 24

Documentation

Overview

Package vlog ********** this file is modified based on "gopkg.in/natefinch/lumberjack.v2" ********** Package lumberjack provides a rolling logger.

Note that this is v2.0 of lumberjack, and should be imported using gopkg.in thusly:

import "gopkg.in/natefinch/lumberjack.v2"

The package name remains simply lumberjack, and the code resides at https://github.com/natefinch/lumberjack under the v2.0 branch.

Lumberjack is intended to be one part of a logging infrastructure. It is not an all-in-one solution, but instead is a pluggable component at the bottom of the logging stack that simply controls the files to which logs are written.

Lumberjack plays well with any logging package that can write to an io.Writer, including the standard library's log package.

Lumberjack assumes that only one process is writing to the output files. Using the same lumberjack configuration from multiple processes on the same machine will result in improper behavior.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AccessLog

func AccessLog(logType *AccessLogEntity)

func AddAsyncFilter added in v1.1.0

func AddAsyncFilter(filterName string, filter AsyncFilter)

func Errorf

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

func Errorln

func Errorln(args ...interface{})

func Fatalf

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

func Fatalln

func Fatalln(args ...interface{})

func GetAccessLogAvailable

func GetAccessLogAvailable() bool

func GetLogDir added in v1.1.0

func GetLogDir() string

func GetMetricsLogAvailable

func GetMetricsLogAvailable() bool

func Infof

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

func Infoln

func Infoln(args ...interface{})

func LogInit

func LogInit(logger Logger)

func MetricsLog

func MetricsLog(msg string)

func ReleaseAccessLogEntity added in v1.1.0

func ReleaseAccessLogEntity(entity *AccessLogEntity)

func SetAccessLogAvailable

func SetAccessLogAvailable(status bool)

func SetAsync

func SetAsync(value bool)

func SetLevel

func SetLevel(level LogLevel)

func SetMetricsLogAvailable

func SetMetricsLogAvailable(status bool)

func Warningf

func Warningf(format string, args ...interface{})

func Warningln

func Warningln(args ...interface{})

Types

type AccessLogEntity

type AccessLogEntity struct {
	FilterName    string `json:"filterName"`
	Role          string `json:"role"`
	RequestID     uint64 `json:"requestID"`
	Service       string `json:"service"`
	Method        string `json:"method"`
	Desc          string `json:"desc"`
	RemoteAddress string `json:"remoteAddress"`
	ReqSize       int    `json:"reqSize"`
	ResSize       int    `json:"resSize"`
	BizTime       int64  `json:"bizTime"`
	TotalTime     int64  `json:"totalTime"`
	Success       bool   `json:"success"`
	ResponseCode  string `json:"responseCode"`
	Exception     string `json:"exception"`
	UpstreamCode  string `json:"upstream_code"`
}

func AcquireAccessLogEntity added in v1.1.0

func AcquireAccessLogEntity() *AccessLogEntity

type AsyncFilter added in v1.1.0

type AsyncFilter interface {
	GetChannel() chan *FilterItem
}

type Caller added in v1.1.0

type Caller struct {
	Ok   bool
	File string
	Line int
}

type Entrypoint added in v1.1.0

type Entrypoint int
const (
	EntrypointInfoln Entrypoint = iota
	EntrypointInfof
	EntrypointWarningln
	EntrypointWarningf
	EntrypointErrorln
	EntrypointErrorf
	EntrypointFatalln
	EntrypointFatalf
	EntrypointAccessLog
	EntrypointMetricsLog
)

type FilterItem added in v1.1.0

type FilterItem struct {
	Level      LogLevel
	Content    string
	Entrypoint Entrypoint
	Caller     *Caller
}

type LogLevel

type LogLevel int8
const (
	TraceLevel LogLevel = iota - 2
	DebugLevel
	InfoLevel
	WarnLevel
	ErrorLevel
	DPanicLevel
	PanicLevel
	FatalLevel
)

func GetLevel

func GetLevel() LogLevel

func (*LogLevel) Set

func (l *LogLevel) Set(level string) error

func (LogLevel) String

func (l LogLevel) String() string

type Logger

type Logger interface {
	Infoln(...interface{})
	Infof(string, ...interface{})
	Warningln(...interface{})
	Warningf(string, ...interface{})
	Errorln(...interface{})
	Errorf(string, ...interface{})
	Fatalln(...interface{})
	Fatalf(string, ...interface{})
	AccessLog(*AccessLogEntity)
	MetricsLog(string)
	Flush()
	SetAsync(bool)
	GetLevel() LogLevel
	SetLevel(LogLevel)
	SetAccessStructured(bool)
	GetAccessLogAvailable() bool
	SetAccessLogAvailable(bool)
	GetMetricsLogAvailable() bool
	SetMetricsLogAvailable(bool)
}

type RotateWriter

type RotateWriter struct {
	// Filename is the file to write logs to.  Backup log files will be retained
	// in the same directory.  It uses <processname>-lumberjack.log in
	// os.TempDir() if empty.
	Filename string `json:"filename" yaml:"filename"`

	// MaxSize is the maximum size in megabytes of the log file before it gets
	// rotated. It defaults to 100 megabytes.
	MaxSize int `json:"maxsize" yaml:"maxsize"`

	// MaxAge is the maximum number of days to retain old log files based on the
	// timestamp encoded in their filename.  Note that a day is defined as 24
	// hours and may not exactly correspond to calendar days due to daylight
	// savings, leap seconds, etc. The default is not to remove old log files
	// based on age.
	MaxAge int `json:"maxage" yaml:"maxage"`

	// MaxBackups is the maximum number of old log files to retain.  The default
	// is to retain all old log files (though MaxAge may still cause them to get
	// deleted.)
	MaxBackups int `json:"maxbackups" yaml:"maxbackups"`

	// LocalTime determines if the time used for formatting the timestamps in
	// backup files is the computer's local time.  The default is to use UTC
	// time.
	LocalTime bool `json:"localtime" yaml:"localtime"`

	// Compress determines if the rotated log files should be compressed
	// using gzip. The default is not to perform compression.
	Compress bool `json:"compress" yaml:"compress"`

	BackupTimeFormat string `json:"backuptimeformat" yaml:"backuptimeformat"`

	RotatePerHour bool `json:"rotateperhour" yaml:"rotateperhour"`
	// contains filtered or unexported fields
}

RotateWriter is an io.WriteCloser that writes to the specified filename.

RotateWriter opens or creates the logfile on first Write. If the file exists and is less than MaxSize megabytes, lumberjack will open and append to that file. If the file exists and its size is >= MaxSize megabytes, the file is renamed by putting the current time in a timestamp in the name immediately before the file's extension (or the end of the filename if there's no extension). A new log file is then created using original filename.

Whenever a write would cause the current log file exceed MaxSize megabytes, the current file is closed, renamed, and a new log file created with the original name. Thus, the filename you give RotateWriter is always the "current" log file.

Backups use the log file name given to RotateWriter, in the form `name-timestamp.ext` where name is the filename without the extension, timestamp is the time at which the log was rotated formatted with the time.Time format of `2006-01-02T15-04-05.000` and the extension is the original extension. For example, if your RotateWriter.Filename is `/var/log/foo/server.log`, a backup created at 6:30pm on Nov 11 2016 would use the filename `/var/log/foo/server-2016-11-04T18-30-00.000.log`

Cleaning Up Old Log Files

Whenever a new logfile gets created, old log files may be deleted. The most recent files according to the encoded timestamp will be retained, up to a number equal to MaxBackups (or all of them if MaxBackups is 0). Any files with an encoded timestamp older than MaxAge days are deleted, regardless of MaxBackups. Note that the time encoded in the timestamp is the rotation time, which may differ from the last time that file was written to.

If MaxBackups and MaxAge are both 0, no old log files will be deleted.

func (*RotateWriter) Close

func (l *RotateWriter) Close() error

Close implements io.Closer, and closes the current logfile.

func (*RotateWriter) Rotate

func (l *RotateWriter) Rotate() error

Rotate causes RotateWriter to close the existing log file and immediately create a new one. This is a helper function for applications that want to initiate rotations outside of the normal rotation rules, such as in response to SIGHUP. After rotating, this initiates compression and removal of old log files according to the configuration.

func (*RotateWriter) Write

func (l *RotateWriter) Write(p []byte) (n int, err error)

Write implements io.Writer. If a write would cause the log file to be larger than MaxSize, the file is closed, renamed to include a timestamp of the current time, and a new log file is created using the original log file name. If the length of the write is greater than MaxSize, an error is returned.

Jump to

Keyboard shortcuts

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