llog

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

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

Go to latest
Published: Nov 30, 2018 License: Apache-2.0 Imports: 15 Imported by: 0

README

llog
====

Low level implementation of leveled execution logs for Go.

It is a fork of the github.com/golang/glog (c6f9652c7179652e2fd8ed7002330db089f4c9db) package that is itself an efficient pure Go implementation of leveled
logs in the manner of the open source C++ package

	http://code.google.com/p/google-glog

Changes:
- This version refactors code to not rely on a single global variable
  and associated log state.
- It separates out the flag parsing so that an application can choose
  to use whatever command-line flags they wish to configure the
  package.
- When built with the 'android' build tag (such as with gomobile),
  it uses the Android logging primitives instead of writing to files.

Documentation

Overview

Package llog implements support for logging analogous to the Google-internal C++ INFO/ERROR/V setup. It provides a low-level class (llog.T) that encapsulates all state and and methods of multi-leveled logging (i.e. Info, Warning, Error and Fatal) and formatting variants such as Infof. Each instance of T may be named and hence multiple sets of logs can be produced by a single process. It also provides V-style logging controlled by the -v and -vmodule=file=2 style flags. Although command line flags are not directly implemented, implementations of the Go 1.2 flags.Value interface are provided to make it easy for users to parse their command line flags correctly. The NewLogger factory function accepts positional parameters that correspond google command line flags.

l := NewLogger("system")
l.Print(infoLog, Info("Prepare to repel boarders")

l.Printf(fatalLog, "Initialization failed: %s", err)

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

if l.V(2) {
	l.Print(infoLog,"Starting transaction...")
}

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 methods that modify this behavior. These methods must be called before any logging is done.

	NewLogger(name)

	SetLogDir(logDir)
		log files will be written to this directory instead of the
		default temporary directory.
	SetLogToStderr(bool)
		If true, logs are written to standard error instead of to files.
	SetAlsoLogToStderr(bool)
		If true, logs are written to standard error as well as to files.
	SetStderrThreshold(level)
		Log events at or above this severity are logged to standard
		error as well as to files.
	SetMaxStackBufSize(size)
		Set the max size (bytes) of the byte buffer to use for stack
		traces. The default max is 4096K; use powers of 2 since the
		stack size will be grown exponentially until it exceeds the max.
             A min of 128K is enforced and any attempts to reduce this will
             be silently ignored.

	Other controls provide aids to debugging.

	SetLogBacktraceAt(location)
		When set to a file and line number holding a logging statement,
		such as
			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.)
	SetV(level)
		Enable V-leveled logging at the specified level.
	SetVModule(module)
		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,
			-gopher*=3
		sets the V level to 3 in all Go files whose names begin "gopher".

SetVFilepath(regexp)

The syntax of the argument is as per VModule, expect that regular
expressions on the entire file path path are used instead of glob
patterns on the file name component as SetVModule.

Index

Constants

This section is empty.

Variables

View Source
var MaxSize uint64 = 1024 * 1024 * 1800

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

Functions

This section is empty.

Types

type FilepathSpec

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

FilepathSpec represents the setting of the -vfilepath flag.

func (*FilepathSpec) Get

func (p *FilepathSpec) 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 (*FilepathSpec) Set

func (p *FilepathSpec) Set(value string) error

Syntax: foo/bar=2,foo/bar/.*=1,f*=3

func (*FilepathSpec) String

func (fp *FilepathSpec) String() string

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 Log

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

Log collects all the global state of the logging setup.

func NewLogger

func NewLogger(name string, skip int) *Log

NewLogger creates a new logger. name is a non-empty string that appears in the names of log files to distinguish between separate instances of the logger writing to the same directory. skip is the number of stack frames to skip in order to reach the call point to be logged. 0 will log the caller of the logging methods, 1 their caller etc.

func (*Log) Flush

func (l *Log) Flush()

func (*Log) Print

func (l *Log) Print(s Severity, args ...interface{})

func (*Log) PrintDepth

func (l *Log) PrintDepth(s Severity, depth int, args ...interface{})

func (*Log) Printf

func (l *Log) Printf(s Severity, format string, args ...interface{})

func (*Log) PrintfDepth

func (l *Log) PrintfDepth(s Severity, depth int, format string, args ...interface{})

func (*Log) Println

func (l *Log) Println(s Severity, args ...interface{})

func (*Log) PrintlnDepth

func (l *Log) PrintlnDepth(s Severity, depth int, args ...interface{})

func (*Log) SetAlsoLogToStderr

func (l *Log) SetAlsoLogToStderr(f bool)

SetAlsoLogToStderr sets the flag that, if true, logs to standard error as well as files

func (*Log) SetLogDir

func (l *Log) SetLogDir(logDir string)

logDir if non-empty, write log files to this directory.

func (*Log) SetLogToStderr

func (l *Log) SetLogToStderr(f bool)

SetLogToStderr sets the flag that, if true, logs to standard error instead of files

func (*Log) SetMaxStackBufSize

func (l *Log) SetMaxStackBufSize(max int)

func (*Log) SetStderrThreshold

func (l *Log) SetStderrThreshold(s Severity)

SetStderrThreshold sets the threshold for which logs at or above which go to stderr

func (*Log) SetTraceLocation

func (l *Log) SetTraceLocation(location TraceLocation)

SetTaceLocation sets the location, file:N, which when encountered will cause logging to emit a stack trace

func (*Log) SetV

func (l *Log) SetV(v Level)

SetV sets the log level for V logs

func (*Log) SetVFilepath

func (l *Log) SetVFilepath(spec FilepathSpec)

SetModuleSpec sets the comma-separated list of pattern=N settings for file-filtered logging

func (*Log) SetVModule

func (l *Log) SetVModule(spec ModuleSpec)

SetModuleSpec sets the comma-separated list of pattern=N settings for file-filtered logging

func (*Log) Stats

func (l *Log) Stats() Stats

func (*Log) String

func (l *Log) String() string

func (*Log) V

func (l *Log) V(level Level) bool

func (*Log) VDepth

func (l *Log) VDepth(depth int, level Level) bool

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.

type ModuleSpec

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

moduleSpec represents the setting of the -vmodule flag.

func (*ModuleSpec) Get

func (m *ModuleSpec) 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 (*ModuleSpec) Set

func (m *ModuleSpec) Set(value string) error

Syntax: recordio=2,file=1,gfs*=3

func (*ModuleSpec) String

func (m *ModuleSpec) String() string

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 Severity

type Severity int32 // sync/atomic int32

severity identifies the sort of log: info, warning etc. It also implements the flag.Value interface. The -stderrthreshold flag is of type severity and should be modified only through the flag.Value interface. The values match the corresponding constants in C++.

const (
	InfoLog Severity = iota
	WarningLog
	ErrorLog
	FatalLog
)

func (*Severity) Set

func (s *Severity) Set(value string) error

Set is part of the flag.Value interface.

func (*Severity) String

func (s *Severity) String() string

String is part of the flag.Value interface.

type Stats

type Stats struct {
	Info, Warning, Error OutputStats
}

Stats tracks the number of lines of output and number of bytes per severity level.

type TraceLocation

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

traceLocation represents the setting of the -log_backtrace_at flag.

func (*TraceLocation) Set

func (t *TraceLocation) Set(value string) error

Syntax: gopherflakes.go:234 Note that unlike vmodule the file extension is included here.

func (*TraceLocation) String

func (t *TraceLocation) String() string

Jump to

Keyboard shortcuts

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