Documentation ¶
Overview ¶
Package log 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 and adaptions for use as a structured logging engine.
Basic examples:
log.Info("Prepare to repel boarders") log.Fatal("Initialization failed", err) // Log with context. log.Infoc(context, "client error: %s", err)
See the documentation for the V function for an explanation of these examples:
if log.V(2) { log.Info("Starting transaction...") }
Log output is buffered and written periodically using Flush. Programs should call Flush before exiting to guarantee all log output is written.
Log output is encoded using the proto.LogEntry message format. Each log entry is preceded by a 32-bit encoded length. Use the log command to display logs in human-readable form:
% cockroach log <log1> [<log2> ...]
By default, all log statements write to files in a temporary directory. This package provides several flags that modify this behavior. These are provided via the pflags library; see InitFlags.
--logtostderr=true Logs are written to standard error instead of to files. --alsologtostderr=false Logs are written 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.) --verbosity=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". Package log is a generated protocol buffer package. It is generated from these files: cockroach/util/log/log.proto It has these top-level messages: LogEntry
Index ¶
- Variables
- func Add(ctx context.Context, kvs ...interface{}) context.Context
- func AddStructured(ctx context.Context, s Severity, depth int, format string, args []interface{})
- func CopyStandardLogTo(name string)
- func DisableLogFileOutput()
- func EnableLogFileOutput(dir string)
- func Error(args ...interface{})
- func ErrorDepth(depth int, args ...interface{})
- func Errorc(ctx context.Context, format string, args ...interface{})
- func Errorf(format string, args ...interface{})
- func Fatal(args ...interface{})
- func FatalDepth(depth int, args ...interface{})
- func FatalOnPanic()
- func Fatalc(ctx context.Context, format string, args ...interface{})
- func Fatalf(format string, args ...interface{})
- func Flush()
- func GetLogReader(filename string, allowAbsolute bool) (io.ReadCloser, error)
- func Info(args ...interface{})
- func InfoDepth(depth int, args ...interface{})
- func Infoc(ctx context.Context, format string, args ...interface{})
- func Infof(format string, args ...interface{})
- func NewJSONEntryReader(reader io.Reader) io.Reader
- func NewTermEntryReader(reader io.Reader) io.Reader
- func V(level level) bool
- func VDepth(level level, depth int) bool
- func Warning(args ...interface{})
- func WarningDepth(depth int, args ...interface{})
- func Warningc(ctx context.Context, format string, args ...interface{})
- func Warningf(format string, args ...interface{})
- type EntryDecoder
- type Field
- type FileDetails
- type FileInfo
- type LogEntry
- func (m *LogEntry) GetArgs() []LogEntry_Arg
- func (m *LogEntry) GetFile() string
- func (m *LogEntry) GetFormat() string
- func (m *LogEntry) GetKey() github_com_cockroachdb_cockroach_proto.Key
- func (m *LogEntry) GetLine() int32
- func (m *LogEntry) GetMethod() github_com_cockroachdb_cockroach_proto.Method
- func (m *LogEntry) GetNodeID() github_com_cockroachdb_cockroach_proto.NodeID
- func (m *LogEntry) GetRangeID() github_com_cockroachdb_cockroach_proto.RangeID
- func (m *LogEntry) GetSeverity() int32
- func (m *LogEntry) GetStacks() []byte
- func (m *LogEntry) GetStoreID() github_com_cockroachdb_cockroach_proto.StoreID
- func (m *LogEntry) GetThreadID() int32
- func (m *LogEntry) GetTime() int64
- func (m *LogEntry) Marshal() (data []byte, err error)
- func (m *LogEntry) MarshalTo(data []byte) (n int, err error)
- func (*LogEntry) ProtoMessage()
- func (m *LogEntry) Reset()
- func (m *LogEntry) Size() (n int)
- func (m *LogEntry) String() string
- func (m *LogEntry) Unmarshal(data []byte) error
- type LogEntry_Arg
- func (m *LogEntry_Arg) GetJson() []byte
- func (m *LogEntry_Arg) GetStr() string
- func (m *LogEntry_Arg) GetType() string
- func (m *LogEntry_Arg) Marshal() (data []byte, err error)
- func (m *LogEntry_Arg) MarshalTo(data []byte) (n int, err error)
- func (*LogEntry_Arg) ProtoMessage()
- func (m *LogEntry_Arg) Reset()
- func (m *LogEntry_Arg) Size() (n int)
- func (m *LogEntry_Arg) String() string
- func (m *LogEntry_Arg) Unmarshal(data []byte) error
- type Severity
Constants ¶
This section is empty.
Variables ¶
var (
ErrInvalidLengthLog = fmt.Errorf("proto: negative length found during unmarshaling")
)
var MaxSize uint64 = 1024 * 1024 * 10
MaxSize is the maximum size of a log file in bytes.
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 Add ¶
Add takes a context and an additional even number of arguments, interpreted as key-value pairs. These are added on top of the supplied context and the resulting context returned.
func AddStructured ¶
AddStructured creates a structured log entry to be written to the specified facility of the logger.
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 DisableLogFileOutput ¶
func DisableLogFileOutput()
DisableLogFileOutput turns off logging. For unittesting only.
func EnableLogFileOutput ¶
func EnableLogFileOutput(dir string)
EnableLogFileOutput turns on logging using the specified directory. For unittesting only.
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.
func ErrorDepth ¶
func ErrorDepth(depth int, args ...interface{})
ErrorDepth logs to the ERROR, WARNING, and INFO logs, offsetting the caller's stack frame by 'depth'.
func Errorc ¶
Errorc logs to the ERROR, WARNING, and INFO logs. It extracts values from Field keys specified in this package and logs them along with the given message and any additional pairs specified as consecutive elements in kvs.
func Errorf ¶
func Errorf(format string, args ...interface{})
Errorf logs to the ERROR, WARNING, and INFO logs. Don't use it; use Error Info or Errorc instead. Arguments are handled in the manner of fmt.Printf; a newline is appended if missing.
func Fatal ¶
func Fatal(args ...interface{})
Fatal logs to the INFO, WARNING, ERROR, and FATAL 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.
func FatalDepth ¶
func FatalDepth(depth int, args ...interface{})
FatalDepth logs to the INFO, WARNING, ERROR, and FATAL logs, including a stack trace of all running goroutines, then calls os.Exit(255), offsetting the caller's stack frame by 'depth'.
func FatalOnPanic ¶
func FatalOnPanic()
FatalOnPanic recovers from a panic and exits the process with a Fatal log. This is useful for avoiding a panic being caught through a CGo exported function or preventing HTTP handlers from recovering panics and ignoring them.
func Fatalc ¶
Fatalc logs to the INFO, WARNING, ERROR, and FATAL logs, including a stack trace of all running goroutines, then calls os.Exit(255). It extracts values from the context using the Field keys specified in this package and logs them along with the given message and any additional pairs specified as consecutive elements in kvs.
func Fatalf ¶
func Fatalf(format string, args ...interface{})
Fatalf logs to the INFO, WARNING, ERROR, and FATAL 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.
func GetLogReader ¶
func GetLogReader(filename string, allowAbsolute bool) (io.ReadCloser, error)
GetLogReader returns a reader for the specified filename. Any external requests (say from the admin UI via HTTP) must specify allowAbsolute as false to prevent leakage of non-log files. Absolute filenames are allowed for the case of the cockroach "log" command, which provides human readable output from an arbitrary file, and is intended to be run locally in a terminal.
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.
func InfoDepth ¶
func InfoDepth(depth int, args ...interface{})
InfoDepth logs to the INFO log, offsetting the caller's stack frame by 'depth'.
func Infoc ¶
Infoc logs to the WARNING and INFO logs. It extracts values from the context using the Field keys specified in this package and logs them along with the given message and any additional pairs specified as consecutive elements in kvs.
func Infof ¶
func Infof(format string, args ...interface{})
Infof logs to the INFO log. Don't use it; use Info or Infoc instead. Arguments are handled in the manner of fmt.Printf; a newline is appended if missing.
func NewJSONEntryReader ¶
NewJSONEntryReader returns a reader for log files containing encoded entries in JSON format.
func NewTermEntryReader ¶
NewTermEntryReader returns a reader for log files containing encoded entries for use from a terminal. If the --color flag is set, and the terminal supports colors, then log output will be colorized.
func V ¶
func V(level level) bool
V returns true if the logging verbosity is set to the specified level or higher.
func Warning ¶
func Warning(args ...interface{})
Warning logs to the WARNING and INFO logs. Warningf logs to the WARNING and INFO logs. Don't use it; use Warning or Arguments are handled in the manner of fmt.Print; a newline is appended.
func WarningDepth ¶
func WarningDepth(depth int, args ...interface{})
WarningDepth logs to the WARNING and INFO logs, offsetting the caller's stack frame by 'depth'.
Types ¶
type EntryDecoder ¶
type EntryDecoder struct {
// contains filtered or unexported fields
}
EntryDecoder reads successive encoded log entries from the input buffer. Each entry is preceded by a single big-ending uint32 describing the next entry's length.
func NewEntryDecoder ¶
func NewEntryDecoder(in io.Reader) *EntryDecoder
NewEntryDecoder creates a new instance of EntryDecoder.
func (*EntryDecoder) Decode ¶
func (lr *EntryDecoder) Decode(entry *LogEntry) error
Decode decodes the next log entry into the provided protobuf message.
type Field ¶
type Field int
A Field is an integer used to enumerate allowed field names in structured log output.
type FileDetails ¶
type FileDetails struct { Program string Host string UserName string Severity Severity Time time.Time PID uint }
A FileDetails holds all of the particulars that can be parsed by the name of a log file.
type FileInfo ¶
type FileInfo struct { Name string // base name SizeBytes int64 ModTimeNanos int64 // most recent mode time in unix nanos Details FileDetails }
A FileInfo holds the filename and size of a log file.
func ListLogFiles ¶
ListLogFiles returns a slice of FileInfo structs for each log file on the local node, in any of the configured log directories.
type LogEntry ¶
type LogEntry struct { // Log message severity. Severity int32 `protobuf:"varint,1,opt,name=severity" json:"severity"` // Time, measured in nanoseconds since the epoch. Time int64 `protobuf:"varint,2,opt,name=time" json:"time"` // Thread id of logging routine. ThreadID int32 `protobuf:"varint,3,opt,name=thread_id" json:"thread_id"` // File which generated log statement. File string `protobuf:"bytes,4,opt,name=file" json:"file"` // Line in file which generated log statement. Line int32 `protobuf:"varint,5,opt,name=line" json:"line"` // Log format message. Format string `protobuf:"bytes,6,opt,name=format" json:"format"` Args []LogEntry_Arg `protobuf:"bytes,7,rep,name=args" json:"args"` // Optional parameters which may be set with log entry. NodeID *github_com_cockroachdb_cockroach_proto.NodeID `protobuf:"varint,8,opt,name=node_id,casttype=github.com/cockroachdb/cockroach/proto.NodeID" json:"node_id,omitempty"` StoreID *github_com_cockroachdb_cockroach_proto.StoreID `protobuf:"varint,9,opt,name=store_id,casttype=github.com/cockroachdb/cockroach/proto.StoreID" json:"store_id,omitempty"` RangeID *github_com_cockroachdb_cockroach_proto.RangeID `protobuf:"varint,10,opt,name=range_id,casttype=github.com/cockroachdb/cockroach/proto.RangeID" json:"range_id,omitempty"` Method *github_com_cockroachdb_cockroach_proto.Method `protobuf:"varint,11,opt,name=method,casttype=github.com/cockroachdb/cockroach/proto.Method" json:"method,omitempty"` Key github_com_cockroachdb_cockroach_proto.Key `protobuf:"bytes,12,opt,name=key,casttype=github.com/cockroachdb/cockroach/proto.Key" json:"key,omitempty"` // Stack traces if requested. Stacks []byte `protobuf:"bytes,13,opt,name=stacks" json:"stacks,omitempty"` XXX_unrecognized []byte `json:"-"` }
Log represents a cockroach structured log entry.
func FetchEntriesFromFiles ¶
func FetchEntriesFromFiles(severity Severity, startTimestamp, endTimestamp int64, maxEntries int, pattern *regexp.Regexp) ([]LogEntry, error)
FetchEntriesFromFiles fetches all available log entires on disk that match the log 'severity' (or worse) and are between the 'startTimestamp' and 'endTimestamp'. It will stop reading new files if the number of entries exceeds 'maxEntries'. Log entries are further filtered by the regexp 'pattern' if provided. The logs entries are returned in reverse chronological order.
func (*LogEntry) GetArgs ¶
func (m *LogEntry) GetArgs() []LogEntry_Arg
func (*LogEntry) GetKey ¶
func (m *LogEntry) GetKey() github_com_cockroachdb_cockroach_proto.Key
func (*LogEntry) GetMethod ¶
func (m *LogEntry) GetMethod() github_com_cockroachdb_cockroach_proto.Method
func (*LogEntry) GetNodeID ¶
func (m *LogEntry) GetNodeID() github_com_cockroachdb_cockroach_proto.NodeID
func (*LogEntry) GetRangeID ¶
func (m *LogEntry) GetRangeID() github_com_cockroachdb_cockroach_proto.RangeID
func (*LogEntry) GetSeverity ¶
func (*LogEntry) GetStoreID ¶
func (m *LogEntry) GetStoreID() github_com_cockroachdb_cockroach_proto.StoreID
func (*LogEntry) GetThreadID ¶
func (*LogEntry) ProtoMessage ¶
func (*LogEntry) ProtoMessage()
type LogEntry_Arg ¶
type LogEntry_Arg struct { Type string `protobuf:"bytes,1,opt,name=type" json:"type"` Str string `protobuf:"bytes,2,opt,name=str" json:"str"` // Optional json representation. Json []byte `protobuf:"bytes,3,opt,name=json" json:"json,omitempty"` XXX_unrecognized []byte `json:"-"` }
Log format arguments.
func (*LogEntry_Arg) GetJson ¶
func (m *LogEntry_Arg) GetJson() []byte
func (*LogEntry_Arg) GetStr ¶
func (m *LogEntry_Arg) GetStr() string
func (*LogEntry_Arg) GetType ¶
func (m *LogEntry_Arg) GetType() string
func (*LogEntry_Arg) Marshal ¶
func (m *LogEntry_Arg) Marshal() (data []byte, err error)
func (*LogEntry_Arg) ProtoMessage ¶
func (*LogEntry_Arg) ProtoMessage()
func (*LogEntry_Arg) Reset ¶
func (m *LogEntry_Arg) Reset()
func (*LogEntry_Arg) Size ¶
func (m *LogEntry_Arg) Size() (n int)
func (*LogEntry_Arg) String ¶
func (m *LogEntry_Arg) String() string
func (*LogEntry_Arg) Unmarshal ¶
func (m *LogEntry_Arg) Unmarshal(data []byte) error
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++.
These constants identify the log levels in order of increasing Severity. A message written to a high-Severity log file is also written to each lower-Severity log file.
func SeverityByName ¶
SeverityByName attempts to parse the passed in string into a severity. (i.e. ERROR, INFO). If it succeeds, the returned bool is set to true.