glog

package module
v0.0.0-...-7873419 Latest Latest
Warning

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

Go to latest
Published: Jul 2, 2018 License: Apache-2.0 Imports: 20 Imported by: 0

README

glog
====

Leveled execution logs for Go.

This is an efficient pure Go implementation of leveled logs in the
manner of the open source C++ package
	https://github.com/google/glog

By binding methods to booleans it is possible to use the log package
without paying the expense of evaluating the arguments to the log.
Through the -vmodule flag, the package also provides fine-grained
control over logging at the file level.

The comment from glog.go introduces the ideas:

	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")


The repository contains an open source version of the log package
used inside Google. The master copy of the source lives inside
Google, not here. The code in this repo is for export only and is not itself
under development. Feature requests will be ignored.

Send bug reports to golang-nuts@googlegroups.com.

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")

Json examples:

glog.Infos().Str("foo", "bar").Int("answer", 42).Msg("Prepare to repel boarders")

glog.Fatals().Error(err).Msgf("Initialization failed: %s", err)

See the documentation for the S function for an explanation of these examples:

glog.S(2).Bool("test", true).Int64("value", -1).Msg("hello world")

Std Log examples:

log := glog.ErrorLogger(0)
log.Print("i am here")

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 (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

Constants

This section is empty.

Variables

View Source
var Backups int = 7

Backups to keep around resulting from log file rotation.

View Source
var DailyRolling bool

DailyRolling to rotate log files daily.

View Source
var EscapeHTML bool

EscapeHTML specifies whether problematic HTML characters should be escaped inside JSON quoted strings.

View Source
var FieldPrefix string

FieldPrefix to add prefix to custom field.

View Source
var MaxSize uint64 = 1024 * 1024 * 1800

MaxSize is the maximum size of a log file in bytes.

View Source
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.

View Source
var Version string

Version is the `version' value of each log entry.

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 ErrorLogger

func ErrorLogger(depth int) *log.Logger

ErrorLogger acts as *log.Logger but uses depth to determine which call frame to log. ErrorLogger(0).Print("msg") is the same as Errors().Msg("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 Flush

func Flush()

Flush flushes all pending log I/O.

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 IsTerminal

func IsTerminal(fd uintptr) bool

func RedirectStderrTo

func RedirectStderrTo(file *os.File) error

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").

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 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 WriteFileWithColor

func WriteFileWithColor(file *os.File, data []byte, s severity)

Types

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 (*Level) Get

func (l *Level) Get() interface{}

Get is part of the flag.Value interface.

func (*Level) Set

func (l *Level) Set(value string) error

Set is part of the flag.Value interface.

func (*Level) String

func (l *Level) String() string

String is part of the flag.Value interface.

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 Struct

type Struct struct {
	// contains filtered or unexported fields
}

Struct is a struct type that implements Infos (like zerolog/zap) etc. See the documentation of S for more information.

func Errors

func Errors() *Struct

Info is equivalent to S(0), but use error level logger. See the documentation of V for usage.

func Fatals

func Fatals() *Struct

Info is equivalent to S(0), but use fatal level logger. See the documentation of V for usage.

func Infos

func Infos() *Struct

Info is equivalent to S(0). See the documentation of V for usage.

func S

func S(level Level) *Struct

S is a structed logging like V

glog.S(2).Int("answer", 42).Msg("log this")

Whether an individual call to S 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 S is at least the value of -v, or of -vmodule for the source file containing the call, the S call will log.

func Warnings

func Warnings() *Struct

Info is equivalent to S(0), but use warning level logger. See the documentation of V for usage.

func (*Struct) Bool

func (s *Struct) Bool(name string, b bool) *Struct

Bool adds the field key with val as a bool to the logger *Struct.

func (*Struct) Bytes

func (s *Struct) Bytes(name string, b []byte) *Struct

Bytes adds the field name with b as a string to the logger *Struct.

func (*Struct) Err

func (s *Struct) Err(err error) *Struct

Err adds the field "error" with err as a string to the logger *Struct. If err is nil, no field is added.

func (*Struct) Float32

func (s *Struct) Float32(name string, f float32) *Struct

Float32 adds the field name with i as a float32 to the logger *Struct.

func (*Struct) Float64

func (s *Struct) Float64(name string, f float64) *Struct

Float64 adds the field name with i as a float64 to the logger *Struct.

func (*Struct) Int

func (s *Struct) Int(name string, i int) *Struct

Int adds the field name with i as a int to the logger *Struct.

func (*Struct) Int16

func (s *Struct) Int16(name string, i int16) *Struct

Int16 adds the field name with i as a int16 to the logger *Struct.

func (*Struct) Int32

func (s *Struct) Int32(name string, i int32) *Struct

Int32 adds the field name with i as a int32 to the logger *Struct.

func (*Struct) Int64

func (s *Struct) Int64(name string, i int64) *Struct

Int64 adds the field name with i as a int64 to the logger *Struct.

func (*Struct) Int8

func (s *Struct) Int8(name string, i int8) *Struct

Int8 adds the field name with i as a int8 to the logger *Struct.

func (*Struct) Msg

func (s *Struct) Msg(msg string)

Msg sends the logger with msg added as the message field if not empty.

NOTICE: once this method is called, the logger *Struct should be disposed. Calling Msg twice can have unexpected result.

func (*Struct) Msgf

func (s *Struct) Msgf(format string, v ...interface{})

Msgf sends the logger with formated msg added as the message field if not empty.

NOTICE: once this methid is called, the logger *Struct should be disposed. Calling Msg twice can have unexpected result.

func (*Struct) Str

func (s *Struct) Str(name string, value string) *Struct

Str adds the field name with value as a string to the logger *Struct.

func (*Struct) Strs

func (s *Struct) Strs(name string, values []string) *Struct

Strs adds the field name with values as a []string to the logger *Struct.

func (*Struct) Uint

func (s *Struct) Uint(name string, i uint) *Struct

Uint adds the field name with i as a uint to the logger *Struct.

func (*Struct) Uint16

func (s *Struct) Uint16(name string, i uint16) *Struct

Uint16 adds the field name with i as a uint16 to the logger *Struct.

func (*Struct) Uint32

func (s *Struct) Uint32(name string, i uint32) *Struct

Uint32 adds the field name with i as a uint32 to the logger *Struct.

func (*Struct) Uint64

func (s *Struct) Uint64(name string, i uint64) *Struct

Uint64 adds the field name with i as a uint64 to the logger *Struct.

func (*Struct) Uint8

func (s *Struct) Uint8(name string, i uint8) *Struct

Uint8 adds the field name with i as a uint8 to the logger *Struct.

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

func V(level Level) Verbose

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) Infof

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

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.

Jump to

Keyboard shortcuts

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