Documentation ¶
Overview ¶
Package glog implements logging analogous to the Google-internal C++ INFO/ERROR/V setup. It provides functions Info, Warning, Error, Fatal, plus formatting variants such as Infof. It also provides V-style logging controlled by the -v and -vmodule=file=2 flags.
Basic examples:
glog.Info("Prepare to repel boarders") glog.Fatalf("Initialization failed: %s", err)
See the documentation for the V function for an explanation of these examples:
if glog.V(2) { glog.Info("Starting transaction...") } glog.V(2).Infoln("Processed", nItems, "elements")
Log output is buffered and written periodically using Flush. Programs should call Flush before exiting to guarantee all log output is written.
By default, all log statements write to files in a temporary directory. This package provides several flags that modify this behavior. As a result, flag.Parse must be called before any logging is done.
-logtostderr=false Logs are written to standard error instead of to files. -alsologtostderr=false Logs are written to standard error as well as to files. -stderrthreshold=ERROR Log events at or above this severity are logged to standard error as well as to files. -log_dir="" Log files will be written to this directory instead of the default temporary directory. Other flags provide aids to debugging. -log_backtrace_at="" When set to a file and line number holding a logging statement, such as -log_backtrace_at=gopherflakes.go:234 a stack trace will be written to the Info log whenever execution hits that statement. (Unlike with -vmodule, the ".go" must be present.) -v=0 Enable V-leveled logging at the specified level. -vmodule="" The syntax of the argument is a comma-separated list of pattern=N, where pattern is a literal file name or "glob" pattern matching and N is a V level. For instance, -vmodule=gopher.go=3 sets the V level to 3 in all Go files named "gopher.go". -vmodule=foo=3 sets V to 3 in all files of any packages whose import path ends in "foo". -vmodule=foo/*=3 sets V to 3 in all files of any packages whose import path contains "foo".
This fork of original golang/glog adds log rotation functionality. Logs are rotated after reaching file size limit or age limit. Additionally limiting total amount of logs is supported (also by both size and age). To keep it simple, log-rotation is configured with package-level variables:
- MaxSize - maximum file size (in bytes) - default value: 1024 * 1024 * 1800
- MinSize - minimum file size (in bytes) - default 0 (even empty file can be rotated)
- MaxTotalSize - maximum size of all files (in bytes) - default 0 (do not remove old files)
- RotationInterval - how often log should be rotated - default Never
- MaxAge - maximum age (time.Duration) of log file - default 0 (do not remove old files)
- Compress - whether to GZIP compress rotated logs - default - false
Default values provide backward-compatibility with golang/glog. If compression is used, all files except the current one are compressed with GZIP.
Rotation works like this:
- if MaxSize or RotationInterval is reached, and file size is > MinSize, current file became old file, and new file is created as a current log file
- all log files older than MaxAge are removed
- if compression is enabled, the old file is compressed
- size of all log files in log_dir is recalculated (to handle external removals of files, etc)
- oldest log files are removed until total size of log files doesn't exceed MaxTotalSize-MaxSize
For sanity, this action is executed only when current file is needs to be rotated
Index ¶
- Variables
- func CopyStandardLogTo(name string)
- func Error(args ...interface{})
- func ErrorDepth(depth int, args ...interface{})
- func Errorf(format string, args ...interface{})
- func Errorln(args ...interface{})
- func Exit(args ...interface{})
- func ExitDepth(depth int, args ...interface{})
- func Exitf(format string, args ...interface{})
- func Exitln(args ...interface{})
- func Fatal(args ...interface{})
- func FatalDepth(depth int, args ...interface{})
- func Fatalf(format string, args ...interface{})
- func Fatalln(args ...interface{})
- func Flush()
- func GetLogDir() string
- func GetLogDirs() []string
- func GetVModule() *moduleSpec
- func Info(args ...interface{})
- func InfoDepth(depth int, args ...interface{})
- func Infof(format string, args ...interface{})
- func Infoln(args ...interface{})
- func Separator(iterable string) string
- func SetAlsoToStderr(to bool)
- func SetD(v int)
- func SetLogDir(str string)
- func SetToStderr(toStderr bool)
- func SetV(v int)
- func SetVTraceThreshold(v int)
- func Warning(args ...interface{})
- func WarningDepth(depth int, args ...interface{})
- func Warningf(format string, args ...interface{})
- func Warningln(args ...interface{})
- type Displayable
- func (d Displayable) Errorf(format string, args ...interface{})
- func (d Displayable) Errorln(args ...interface{})
- func (d Displayable) Infof(format string, args ...interface{})
- func (d Displayable) Infoln(args ...interface{})
- func (d Displayable) Warnf(format string, args ...interface{})
- func (d Displayable) Warnln(args ...interface{})
- type Interval
- type Level
- type OutputStats
- type TraceLocation
- type Verbose
- func (v Verbose) Error(args ...interface{})
- func (v Verbose) Errorf(format string, args ...interface{})
- func (v Verbose) Errorln(args ...interface{})
- func (v Verbose) Info(args ...interface{})
- func (v Verbose) Infof(format string, args ...interface{})
- func (v Verbose) Infoln(args ...interface{})
- func (v Verbose) Warn(args ...interface{})
- func (v Verbose) Warnf(format string, args ...interface{})
- func (v Verbose) Warnln(args ...interface{})
Constants ¶
This section is empty.
Variables ¶
var Compress bool
Compress determines whether to compress rotated logs with GZIP or not.
var DefaultAlsoToStdErr = false
DefaultAlsoToStdErr establishes the default bool toggling whether logging should be written to BOTH file and stderr.
var DefaultDisplay = 3
DefaultDisplay establishes the default verbosity Level for display (stderr) logging.
var DefaultLogDirName = "log"
DefaultLogDirName establishes the default directory name for debug (V) logs. Log files will be written inside this dir. By default, this directory will be created if it does not exist within the context's chain directory, eg. <datadir>/<chain>/log/.
var DefaultToStdErr = false
DefaultToStdErr establishes the default bool toggling whether logging should be directed ONLY to stderr.
var DefaultVerbosity = 5
DefaultVerbosity establishes the default verbosity Level for to-file (debug) logging.
var MaxAge time.Duration
MaxAge defines the maximum age of the oldest log file. All log files older than MaxAge will be removed.
var MaxSize uint64 = 1024 * 1800
MaxSize is the maximum size of a log file in bytes.
var MaxTotalSize uint64
MaxTotalSize is a maximum size of all log files.
var MinSize uint64
MinSize is a minimum file size qualifying for rotation. This variable can be used to avoid rotation of empty or almost emtpy files.
var RotationInterval = Never
RotationInterval determines how often log rotation should take place
var Stats struct { Info, Warning, Error OutputStats }
Stats tracks the number of lines of output and number of bytes per severity level. Values must be read with atomic.LoadInt64.
Functions ¶
func CopyStandardLogTo ¶
func CopyStandardLogTo(name string)
CopyStandardLogTo arranges for messages written to the Go "log" package's default logs to also appear in the Google logs for the named and lower severities. Subsequent changes to the standard log's default output location or format may break this behavior.
Valid names are "INFO", "WARNING", "ERROR", and "FATAL". If the name is not recognized, CopyStandardLogTo panics.
func Error ¶
func Error(args ...interface{})
Error logs to the ERROR, WARNING, and INFO logs. Arguments are handled in the manner of fmt.Print; a newline is appended if missing.
func ErrorDepth ¶
func ErrorDepth(depth int, args ...interface{})
ErrorDepth acts as Error but uses depth to determine which call frame to log. ErrorDepth(0, "msg") is the same as Error("msg").
func Errorf ¶
func Errorf(format string, args ...interface{})
Errorf logs to the ERROR, WARNING, and INFO logs. Arguments are handled in the manner of fmt.Printf; a newline is appended if missing.
func Errorln ¶
func Errorln(args ...interface{})
Errorln logs to the ERROR, WARNING, and INFO logs. Arguments are handled in the manner of fmt.Println; a newline is appended if missing.
func Exit ¶
func Exit(args ...interface{})
Exit logs to the FATAL, ERROR, WARNING, and INFO logs, then calls os.Exit(1). Arguments are handled in the manner of fmt.Print; a newline is appended if missing.
func ExitDepth ¶
func ExitDepth(depth int, args ...interface{})
ExitDepth acts as Exit but uses depth to determine which call frame to log. ExitDepth(0, "msg") is the same as Exit("msg").
func Exitf ¶
func Exitf(format string, args ...interface{})
Exitf logs to the FATAL, ERROR, WARNING, and INFO logs, then calls os.Exit(1). Arguments are handled in the manner of fmt.Printf; a newline is appended if missing.
func Exitln ¶
func Exitln(args ...interface{})
Exitln logs to the FATAL, ERROR, WARNING, and INFO logs, then calls os.Exit(1).
func Fatal ¶
func Fatal(args ...interface{})
Fatal logs to the FATAL, ERROR, WARNING, and INFO logs, including a stack trace of all running goroutines, then calls os.Exit(255). Arguments are handled in the manner of fmt.Print; a newline is appended if missing.
func FatalDepth ¶
func FatalDepth(depth int, args ...interface{})
FatalDepth acts as Fatal but uses depth to determine which call frame to log. FatalDepth(0, "msg") is the same as Fatal("msg").
func Fatalf ¶
func Fatalf(format string, args ...interface{})
Fatalf logs to the FATAL, ERROR, WARNING, and INFO logs, including a stack trace of all running goroutines, then calls os.Exit(255). Arguments are handled in the manner of fmt.Printf; a newline is appended if missing.
func Fatalln ¶
func Fatalln(args ...interface{})
Fatalln logs to the FATAL, ERROR, WARNING, and INFO logs, including a stack trace of all running goroutines, then calls os.Exit(255). Arguments are handled in the manner of fmt.Println; a newline is appended if missing.
func GetLogDirs ¶
func GetLogDirs() []string
func GetVModule ¶
func GetVModule() *moduleSpec
GetVModule returns the global verbosity pattern flag.
func Info ¶
func Info(args ...interface{})
Info logs to the INFO log. Arguments are handled in the manner of fmt.Print; a newline is appended if missing.
func InfoDepth ¶
func InfoDepth(depth int, args ...interface{})
InfoDepth acts as Info but uses depth to determine which call frame to log. InfoDepth(0, "msg") is the same as Info("msg").
func Infof ¶
func Infof(format string, args ...interface{})
Infof logs to the INFO log. Arguments are handled in the manner of fmt.Printf; a newline is appended if missing.
func Infoln ¶
func Infoln(args ...interface{})
Infoln logs to the INFO log. Arguments are handled in the manner of fmt.Println; a newline is appended if missing.
func SetAlsoToStderr ¶
func SetAlsoToStderr(to bool)
SetAlsoToStderr sets global output option for logging to both FS and stderr.
func SetVTraceThreshold ¶
func SetVTraceThreshold(v int)
SetVTraceThreshold sets the current verbosity trace threshold for logging.
func Warning ¶
func Warning(args ...interface{})
Warning logs to the WARNING and INFO logs. Arguments are handled in the manner of fmt.Print; a newline is appended if missing.
func WarningDepth ¶
func WarningDepth(depth int, args ...interface{})
WarningDepth acts as Warning but uses depth to determine which call frame to log. WarningDepth(0, "msg") is the same as Warning("msg").
Types ¶
type Displayable ¶
type Displayable bool
func D ¶
func D(level Level) Displayable
func (Displayable) Errorf ¶
func (d Displayable) Errorf(format string, args ...interface{})
func (Displayable) Errorln ¶
func (d Displayable) Errorln(args ...interface{})
func (Displayable) Infof ¶
func (d Displayable) Infof(format string, args ...interface{})
func (Displayable) Infoln ¶
func (d Displayable) Infoln(args ...interface{})
func (Displayable) Warnf ¶
func (d Displayable) Warnf(format string, args ...interface{})
func (Displayable) Warnln ¶
func (d Displayable) Warnln(args ...interface{})
type Interval ¶
type Interval uint8
Interval is a type for rotation interval specification
These constants identify the interval for log rotation.
func ParseInterval ¶
type Level ¶
type Level int32
Level specifies a level of verbosity for V logs. *Level implements flag.Value; the -v flag is of type Level and should be modified only through the flag.Value interface.
func GetDisplayable ¶
func GetDisplayable() *Level
func GetVTraceThreshold ¶
func GetVTraceThreshold() *Level
GetVTraceThreshold gets the current verbosity trace threshold for logging.
func GetVerbosity ¶ added in v1.4.0
func GetVerbosity() *Level
GetVerbosity returns the global verbosity level flag.
type OutputStats ¶
type OutputStats struct {
// contains filtered or unexported fields
}
OutputStats tracks the number of output lines and bytes written.
func (*OutputStats) Bytes ¶
func (s *OutputStats) Bytes() int64
Bytes returns the number of bytes written.
func (*OutputStats) Lines ¶
func (s *OutputStats) Lines() int64
Lines returns the number of lines written.
type TraceLocation ¶
type TraceLocation struct {
// contains filtered or unexported fields
}
traceLocation represents the setting of the -log_backtrace_at flag.
func GetTraceLocation ¶
func GetTraceLocation() *TraceLocation
GetTraceLocation returns the global TraceLocation flag.
func (*TraceLocation) Get ¶
func (t *TraceLocation) Get() interface{}
Get is part of the (Go 1.2) flag.Getter interface. It always returns nil for this flag type since the struct is not exported
func (*TraceLocation) Set ¶
func (t *TraceLocation) Set(value string) error
Syntax: -log_backtrace_at=gopherflakes.go:234 Note that unlike vmodule the file extension is included here.
func (*TraceLocation) String ¶
func (t *TraceLocation) String() string
type Verbose ¶
type Verbose bool
Verbose is a boolean type that implements Infof (like Printf) etc. See the documentation of V for more information.
func V ¶
V reports whether verbosity at the call site is at least the requested level. The returned value is a boolean of type Verbose, which implements Info, Infoln and Infof. These methods will write to the Info log if called. Thus, one may write either
if glog.V(2) { glog.Info("log this") }
or
glog.V(2).Info("log this")
The second form is shorter but the first is cheaper if logging is off because it does not evaluate its arguments.
Whether an individual call to V generates a log record depends on the setting of the -v and --vmodule flags; both are off by default. If the level in the call to V is at least the value of -v, or of -vmodule for the source file containing the call, the V call will log.
func (Verbose) Error ¶
func (v Verbose) Error(args ...interface{})
ERROR Error is equivalent to the global Error function, guarded by the value of v. See the documentation of V for usage.
func (Verbose) Errorf ¶
Errorf is equivalent to the global Errorf function, guarded by the value of v. See the documentation of V for usage.
func (Verbose) Errorln ¶
func (v Verbose) Errorln(args ...interface{})
Errorln is equivalent to the global Errorln function, guarded by the value of v. See the documentation of V for usage.
func (Verbose) Info ¶
func (v Verbose) Info(args ...interface{})
INFO Info is equivalent to the global Info function, guarded by the value of v. See the documentation of V for usage.
func (Verbose) Infof ¶
Infof is equivalent to the global Infof function, guarded by the value of v. See the documentation of V for usage.
func (Verbose) Infoln ¶
func (v Verbose) Infoln(args ...interface{})
Infoln is equivalent to the global Infoln function, guarded by the value of v. See the documentation of V for usage.
func (Verbose) Warn ¶
func (v Verbose) Warn(args ...interface{})
WARN Warn is equivalent to the global Warn function, guarded by the value of v. See the documentation of V for usage.