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.
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 (minus the ".go" suffix) or "glob" pattern and N is a V level. For instance, -vmodule=gopher*=3 sets the V level to 3 in all Go files whose names begin "gopher".
Index ¶
- Variables
- func ContextWithData(ctx context.Context, args ...interface{}) context.Context
- func ContextWithPrefix(ctx context.Context, prefix string) context.Context
- func Data(arg interface{}) interface{}
- func Error(args ...interface{})
- func ErrorIf(err error, args ...interface{})
- func ErrorWithDepth(extraDepth int, args ...interface{})
- func Errorf(format string, args ...interface{})
- func ErrorfIf(err error, format string, args ...interface{})
- func ErrorfWithDepth(extraDepth int, format string, args ...interface{})
- func Errorln(args ...interface{})
- func ErrorlnWithDepth(extraDepth int, args ...interface{})
- func Exit(args ...interface{})
- func ExitWithDepth(depth int, args ...interface{})
- func Exitf(format string, args ...interface{})
- func Exitln(args ...interface{})
- func Fatal(args ...interface{})
- func FatalIf(err error, args ...interface{})
- func FatalWithDepth(extraDepth int, args ...interface{})
- func Fatalf(format string, args ...interface{})
- func FatalfWithDepth(extraDepth int, format string, args ...interface{})
- func Fatalln(args ...interface{})
- func FatallnWithDepth(extraDepth int, args ...interface{})
- func Flush()
- func Info(args ...interface{})
- func InfoWithDepth(extraDepth int, args ...interface{})
- func Infof(format string, args ...interface{})
- func InfofWithDepth(extraDepth int, format string, args ...interface{})
- func Infoln(args ...interface{})
- func InfolnWithDepth(extraDepth int, args ...interface{})
- func RegisterBackend() <-chan Event
- func SetOutput(w io.Writer)
- func Warning(args ...interface{})
- func WarningIf(err error, args ...interface{})
- func WarningWithDepth(extraDepth int, args ...interface{})
- func Warningf(format string, args ...interface{})
- func WarningfIf(err error, format string, args ...interface{})
- func WarningfWithDepth(extraDepth int, format string, args ...interface{})
- func Warningln(args ...interface{})
- func WarninglnWithDepth(extraDepth int, args ...interface{})
- type ErrorArg
- type Event
- type FormatStringArg
- type Level
- type Logger
- func (l *Logger) AppendData(vars ...interface{}) *Logger
- func (l *Logger) Error(args ...interface{})
- func (l *Logger) ErrorIf(err error, args ...interface{})
- func (l *Logger) Errorf(format string, args ...interface{})
- func (l *Logger) ErrorfIf(err error, format string, args ...interface{})
- func (l *Logger) Errorln(args ...interface{})
- func (l *Logger) Fatal(args ...interface{})
- func (l *Logger) FatalIf(err error, args ...interface{})
- func (l *Logger) Fatalf(format string, args ...interface{})
- func (l *Logger) Fatalln(args ...interface{})
- func (l *Logger) GetErrorEvent(args ...interface{}) Event
- func (l *Logger) Info(args ...interface{})
- func (l *Logger) Infof(format string, args ...interface{})
- func (l *Logger) Infoln(args ...interface{})
- func (l *Logger) Warning(args ...interface{})
- func (l *Logger) Warningf(format string, args ...interface{})
- func (l *Logger) Warningln(args ...interface{})
- func (l *Logger) WithData(vars ...interface{}) *Logger
- func (l *Logger) WithPrefix(prefix string) *Logger
- type OutputStats
- type Verbose
- func (v Verbose) Info(args ...interface{})
- func (v Verbose) InfoWithDepth(extraDepth int, args ...interface{})
- func (v Verbose) Infof(format string, args ...interface{})
- func (v Verbose) InfofWithDepth(extraDepth int, format string, args ...interface{})
- func (v Verbose) Infoln(args ...interface{})
- func (v Verbose) InfolnWithDepth(extraDepth int, args ...interface{})
Constants ¶
This section is empty.
Variables ¶
var ExternalOutput externalWriter
This ExternalOutput is to be used only for external logs
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 ContextWithData ¶
ContextWithData creates a context as extension of the parent context, with the provided data stored as a value. Any existing data on the parent context will be replaced.
func ContextWithPrefix ¶
ContextWithPrefix creates a context as extension of the parent context, with the provided prefix stored as a value. Any existing prefix on the parent context will be replaced.
func Data ¶
func Data(arg interface{}) interface{}
Data tags an item to be ignored by glog when logging but to pass it to any registered backends. An example would be:
var req *http.Request ... glog.Error("failed to complete process", glog.Data(req))
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 ErrorIf ¶
func ErrorIf(err error, args ...interface{})
ErrorIf logs to the ERROR, WARNING, and INFO logs. Arguments are handled in the manner of fmt.Print; a newline is appended if missing.
func ErrorWithDepth ¶
func ErrorWithDepth(extraDepth int, args ...interface{})
ErrorWithDepth is equivalent to Error but with a specified extra depth (on the call stack). Arguments are handled in the manner of fmt.Print; a newline is appended if missing.
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 ErrorfIf ¶
ErrorfIf logs to the ERROR, WARNING, and INFO logs. Arguments are handled in the manner of fmt.Printf; a newline is appended if missing.
func ErrorfWithDepth ¶
ErrorfWithDepth is equivalent to Errorf but with a specified extra depth (on the call stack). 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 ErrorlnWithDepth ¶
func ErrorlnWithDepth(extraDepth int, args ...interface{})
ErrorlnWithDepth is equivalent to Errorln but with a specified extra depth (on the call stack). 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 ExitWithDepth ¶
func ExitWithDepth(depth int, args ...interface{})
ExitWithDepth acts as Exit but uses depth to determine which call frame to log. ExitWithDepth(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 FatalIf ¶
func FatalIf(err error, args ...interface{})
FatalIf logs to the FATAL, ERROR, WARNING, and INFO, 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 FatalWithDepth ¶
func FatalWithDepth(extraDepth int, args ...interface{})
FatalWithDepth is equivalent to Fatal but with a specified extra depth (on the call stack). Arguments are handled in the manner of fmt.Print; a newline is appended if missing.
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 FatalfWithDepth ¶
FatalfWithDepth is equivalent to Fatalf but with a specified extra depth (on the call stack). 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 FatallnWithDepth ¶
func FatallnWithDepth(extraDepth int, args ...interface{})
FatallnWithDepth is equivalent to Fatalln but with a specified extra depth (on the call stack). Arguments are handled in the manner of fmt.Println; a newline is appended if missing.
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 InfoWithDepth ¶
func InfoWithDepth(extraDepth int, args ...interface{})
InfoWithDepth is equivalent to Info but with a specified extra depth (on the call stack). Arguments are handled in the manner of fmt.Print; a newline is appended if missing.
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 InfofWithDepth ¶
InfofWithDepth is equivalent to Infof but with a specified extra depth (on the call stack). 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 InfolnWithDepth ¶
func InfolnWithDepth(extraDepth int, args ...interface{})
InfolnWithDepth is equivalent to Infoln but with a specified extra depth (on the call stack). Arguments are handled in the manner of fmt.Println; a newline is appended if missing.
func RegisterBackend ¶
func RegisterBackend() <-chan Event
RegisterBackend returns a channel on which Event's will be passed when they are logged.
The caller is responsible for any necessary synchronization such that the call to this function "happens before" any events to be logged to this channel or other calls to RegisterBackend().
func SetOutput ¶
SetOutput overrides the logging output writer.
If the provided writer is an *os.File, or otherwise has a 'Sync() error' method, it is called periodically (and by Flush) to commit pending data.
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 WarningIf ¶
func WarningIf(err error, args ...interface{})
WarningIf logs to the WARNING, and INFO logs. Arguments are handled in the manner of fmt.Print; a newline is appended if missing.
func WarningWithDepth ¶
func WarningWithDepth(extraDepth int, args ...interface{})
WarningWithDepth is equivalent to Warning but with a specified extra depth (on the call stack). Arguments are handled in the manner of fmt.Print; a newline is appended if missing.
func Warningf ¶
func Warningf(format string, args ...interface{})
Warningf logs to the WARNING and INFO logs. Arguments are handled in the manner of fmt.Printf; a newline is appended if missing.
func WarningfIf ¶
WarningfIf logs to the WARNING, and INFO logs. Arguments are handled in the manner of fmt.Print; a newline is appended if missing.
func WarningfWithDepth ¶
WarningfWithDepth is equivalent to Warningf but with a specified extra depth (on the call stack). Arguments are handled in the manner of fmt.Printf; a newline is appended if missing.
func Warningln ¶
func Warningln(args ...interface{})
Warningln logs to the WARNING and INFO logs. Arguments are handled in the manner of fmt.Println; a newline is appended if missing.
func WarninglnWithDepth ¶
func WarninglnWithDepth(extraDepth int, args ...interface{})
WarninglnWithDepth is equivalent to Warningln but with a specified extra depth (on the call stack). Arguments are handled in the manner of fmt.Println; a newline is appended if missing.
Types ¶
type ErrorArg ¶
type ErrorArg struct {
Error error
}
ErrorArg captures information about an error passed as an argument. It is passed to backends as a data arg.
type Event ¶
type Event struct { Severity string Message []byte Data []interface{} StackTrace []uintptr // inner to outer }
An Event contains a logged event's severity (INFO, WARN, ERROR, FATAL), a format string (if Infof, Warnf, Errorf or Fatalf were used) and a slice of everything else passed to the log call.
func GetErrorEvent ¶
func GetErrorEvent(args ...interface{}) Event
GetErrorEvent returns an ERROR level Event of args
type FormatStringArg ¶
type FormatStringArg struct {
Format string
}
FormatStringArg is used to capture the raw format string passed to glog, which often contains an error message without identifying details that can be used to help group the message.
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.
type Logger ¶
type Logger struct {
// contains filtered or unexported fields
}
Logger provides logging functionality with additional data and prefixing.
func NewLogger ¶
func NewLogger() *Logger
NewLogger creates a Logger instance with no additional data.
func WithContext ¶
WithContext creates a logger from a context.Context
func WithData ¶
func WithData(args ...interface{}) *Logger
WithData creates a Logger with a given set of backend data. All args provided will be wrapped with glog.Data prior to being sent.
func WithPrefix ¶
WithPrefix creates a Logger with a given prefix.
func (*Logger) AppendData ¶
AppendData creates a Logger from an existing logger, appending the provided data to the data in the existing logger.
func (*Logger) Error ¶
func (l *Logger) Error(args ...interface{})
Error is equivalent to the global Error function, with the addition of prefix and data content from this Logger.
func (*Logger) ErrorIf ¶
ErrorIf is equivalent to the global ErrorIf function, with the addition of prefix and data content from this Logger.
func (*Logger) Errorf ¶
Errorf is equivalent to the global Errorf function, with the addition of prefix and data content from this Logger.
func (*Logger) ErrorfIf ¶
ErrorfIf is equivalent to the global ErrorfIf function, with the addition of prefix and data content from this Logger.
func (*Logger) Errorln ¶
func (l *Logger) Errorln(args ...interface{})
Errorln is equivalent to the global Errorln function, with the addition of prefix and data content from this Logger.
func (*Logger) Fatal ¶
func (l *Logger) Fatal(args ...interface{})
Fatal is equivalent to the global Fatal function, with the addition of prefix and data content from this Logger.
func (*Logger) FatalIf ¶
FatalIf is equivalent to the global FatalIf function, with the addition of prefix and data content from this Logger.
func (*Logger) Fatalf ¶
Fatalf is equivalent to the global Fatalf function, with the addition of prefix and data content from this Logger.
func (*Logger) Fatalln ¶
func (l *Logger) Fatalln(args ...interface{})
Fatalln is equivalent to the global Fatalln function, with the addition of prefix and data content from this Logger.
func (*Logger) GetErrorEvent ¶
GetErrorEvent is equivalent to the global GetErrorEvent function, with the addition of prefix and data content from this Logger.
func (*Logger) Info ¶
func (l *Logger) Info(args ...interface{})
Info is equivalent to the global Info function, with the addition of prefix and data content from this Logger.
func (*Logger) Infof ¶
Infof is equivalent to the global Infof function, with the addition of prefix and data content from this Logger.
func (*Logger) Infoln ¶
func (l *Logger) Infoln(args ...interface{})
Infoln is equivalent to the global Infoln function, with the addition of prefix and data content from this Logger.
func (*Logger) Warning ¶
func (l *Logger) Warning(args ...interface{})
Warning is equivalent to the global Warning function, with the addition of prefix and data content from this Logger.
func (*Logger) Warningf ¶
Warningf is equivalent to the global Warningf function, with the addition of prefix and data content from this Logger.
func (*Logger) Warningln ¶
func (l *Logger) Warningln(args ...interface{})
Warningln is equivalent to the global Warningln function, with the addition of prefix and data content from this Logger.
func (*Logger) WithData ¶
WithData creates a Logger from an existing logger, using the specifed data. Any existing data on the input Logger will be replaced.
func (*Logger) WithPrefix ¶
WithPrefix creates a Logger from an existing logger with a specified prefix. Any prefix on the input Logger will be replaced.
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 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) Info ¶
func (v Verbose) Info(args ...interface{})
Info is equivalent to the global Info function, guarded by the value of v. See the documentation of V for usage.
func (Verbose) InfoWithDepth ¶
InfoWithDepth is equivalent to Info but with a specified extra depth (on the call stack). 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) InfofWithDepth ¶
InfofWithDepth is equivalent to Infof but with a specified extra depth (on the call stack). 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) InfolnWithDepth ¶
InfolnWithDepth is equivalent to Infoln but with a specified extra depth (on the call stack). See the documentation of V for usage.