lumberjack

package module
v0.0.0-...-97b85b0 Latest Latest
Warning

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

Go to latest
Published: Aug 10, 2023 License: MIT Imports: 17 Imported by: 0

Documentation

Overview

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

View Source
const (
	LOCAL_HOST = "127.0.0.1"

	DATEFORMAT = "2006-01-02" //formate of date, used to rotate
	LASTFILE   = "-LAST"      // flag of last file of today

)

Variables

View Source
var (

	// megabyte is the conversion factor between MaxSize and bytes.  It is a
	// variable so tests can mock it out and not need to write megabytes of data
	// to disk.
	Kilobyte = 1024

	Gigabyte = 1024 * megabyte
)

Functions

func ExternalIP

func ExternalIP() string

获取当前服务器IP

func GetStackInfo

func GetStackInfo() string

GetStackInfo 获取Panic堆栈信息

func Init

func Init(mysqlConf *MySQLConfig)

func RecoverPanic

func RecoverPanic()

RecoverPanic 恢复panic

func Reload

func Reload(mysqlConf *MySQLConfig) error

func UnCompressFile

func UnCompressFile(src, dst string) (err error)

unCompressLogFile uncompresses the given file, removing the compressed file if successful.

Types

type Logger

type Logger 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"`
	// Db is the db name
	Db string `json:"db" yaml:"db"`
	// Tb is the table name
	Tb string `json:"tb" yaml:"tb"`

	// 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"`
	MaxSize string `json:"maxsize" yaml:"maxsize"`

	// PostHandler is the handler after rotating of the file. Notify the remote server
	// or some others to do something, such as transfer the file to the FILE-STORAGE-SYS.
	PostHandler PostRotateCallback
	// contains filtered or unexported fields
}

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

Logger 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 Logger is always the "current" log file.

Backups use the log file name given to Logger, 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 Logger.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 (*Logger) Close

func (l *Logger) Close() error

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

func (*Logger) CurrentFileInfo

func (l *Logger) CurrentFileInfo() os.FileInfo

CurrentFileInfo returns the file info.

func (*Logger) Delete

func (l *Logger) Delete() error

func (l *Logger) Delete() {

func (*Logger) FileStartWriteDate

func (l *Logger) FileStartWriteDate() *time.Time

func (*Logger) GetLastRotateTime

func (l *Logger) GetLastRotateTime() time.Time

func (*Logger) Rotate

func (l *Logger) Rotate() error

Rotate causes Logger 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 (*Logger) SetFileStartWriteDate

func (l *Logger) SetFileStartWriteDate(date *time.Time)

func (*Logger) SetLastRotateTime

func (l *Logger) SetLastRotateTime(time time.Time)

func (*Logger) Write

func (l *Logger) 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.

type MySQLConfig

type MySQLConfig struct {
	User         string
	Passwd       string
	Host         string
	DbName       string
	Port         int
	MaxIdleConns int
	MaxOpenConns int
}

type PostRotateCallback

type PostRotateCallback func(args ...interface{}) ([]byte, error)

Jump to

Keyboard shortcuts

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