Documentation ¶
Index ¶
- Variables
- func GetFlushFrequencyFromEnvironment() time.Duration
- type BunyanConverter
- type Converter
- type FileStream
- type Level
- type Logger
- func Create(name string, parameters ...interface{}) *Logger
- func CreateIfNil(logger *Logger, name string) *Logger
- func CreateWithDestination(name string, destinations ...string) *Logger
- func CreateWithStream(name string, streams ...Streamer) *Logger
- func FromContext(context context.Context) (*Logger, error)
- func Must(log *Logger, err error) *Logger
- func (log *Logger) Child(topic, scope interface{}, params ...interface{}) *Logger
- func (log *Logger) Close()
- func (log *Logger) Debugf(msg string, args ...interface{})
- func (log *Logger) Errorf(msg string, args ...interface{})
- func (log *Logger) Fatalf(msg string, args ...interface{})
- func (log *Logger) Flush()
- func (log *Logger) GetRecord(key string) interface{}
- func (l *Logger) HttpHandler() func(http.Handler) http.Handler
- func (log *Logger) Infof(msg string, args ...interface{})
- func (log *Logger) Record(key string, value interface{}) *Logger
- func (log *Logger) Recordf(key, value string, args ...interface{}) *Logger
- func (log *Logger) Records(params ...interface{}) *Logger
- func (log *Logger) Scope(value interface{}) *Logger
- func (log *Logger) SetFilterLevel(level Level) Streamer
- func (log *Logger) ShouldWrite(level Level) bool
- func (log Logger) String() string
- func (l *Logger) ToContext(parent context.Context) context.Context
- func (log *Logger) Topic(value interface{}) *Logger
- func (log *Logger) Tracef(msg string, args ...interface{})
- func (log *Logger) Warnf(msg string, args ...interface{})
- func (log *Logger) Write(record Record) error
- func (log *Logger) Writer() io.Writer
- type MultiStream
- type NilStream
- type PinoConverter
- type Record
- type StackDriverConverter
- type StackDriverStream
- func (stream *StackDriverStream) Close()
- func (stream *StackDriverStream) Flush()
- func (stream *StackDriverStream) SetFilterLevel(level Level) Streamer
- func (stream *StackDriverStream) ShouldWrite(level Level) bool
- func (stream *StackDriverStream) String() string
- func (stream *StackDriverStream) Write(record Record) (err error)
- type StderrStream
- type StdoutStream
- type Streamer
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var VERSION = "1.3.6" + commit
VERSION is the version of this application
Functions ¶
func GetFlushFrequencyFromEnvironment ¶
GetFlushFrequencyFromEnvironment fetches the flush frequency from the environment
the frequency should be like https://golang.org/pkg/time/#ParseDuration or an ISO8601 duration.
If not set, the frequency will be 5 minutes
Types ¶
type BunyanConverter ¶ added in v1.3.0
type BunyanConverter struct { }
BunyanConverter is the default Converter
func (*BunyanConverter) Convert ¶ added in v1.3.0
func (converter *BunyanConverter) Convert(record Record) Record
Convert converts the Record into a Bunyan compatible Record
type Converter ¶ added in v1.3.0
Converter is used to convert a Record into another format
func GetConverterFromEnvironment ¶ added in v1.3.0
func GetConverterFromEnvironment() Converter
GetConverterFromEnvironment fetches the Converter from the environment
type FileStream ¶
type FileStream struct { *json.Encoder Path string Converter Converter FilterLevel Level Unbuffered bool // contains filtered or unexported fields }
FileStream is the Stream that writes to a file
Any record with a level < FilterLevel will be written
func (*FileStream) Close ¶ added in v1.3.0
func (stream *FileStream) Close()
Close closes the stream
func (*FileStream) Flush ¶
func (stream *FileStream) Flush()
Flush flushes the stream (makes sure records are actually written)
func (*FileStream) SetFilterLevel ¶ added in v1.3.0
func (stream *FileStream) SetFilterLevel(level Level) Streamer
SetFilterLevel sets the filter level
func (*FileStream) ShouldWrite ¶
func (stream *FileStream) ShouldWrite(level Level) bool
ShouldWrite tells if the given level should be written to this stream
func (*FileStream) Write ¶
func (stream *FileStream) Write(record Record) (err error)
Write writes the given Record
implements logger.Stream
type Level ¶
type Level byte
Level represents the log Level The higher the value the more chances to write in the Stream
const ( // UNSET level means the level is not yet set UNSET Level = iota * 10 // TRACE level should be used for entries that should be used by the developer of the app/package only TRACE // DEBUG level should be used for detailed logging as they tend to be noisy DEBUG // INFO level should be used as the standard level. Entries that really mean something to most people should go there INFO // WARN level should be used when the code considers a situation as not optimal but it can live with it WARN // ERROR level should be used when the code encounters an issue and normal flow is disrupted ERROR // FATAL level should be used when the code eoncounters an issue it cannot recover from FATAL // ALWAYS level should be used for entries that should always be logged, like app version, etc. ALWAYS Level = 255 // NEVER level should be used for entries that should never be logged NEVER Level = 1 )
func GetLevelFromEnvironment ¶
func GetLevelFromEnvironment() Level
GetLevelFromEnvironment retrieves the level from the environment LOG_LEVEL
func GetLevelFromRecord ¶
GetLevelFromRecord retrieves the level from the given Record
func (Level) ShouldWrite ¶
ShouldWrite tells if the current level is writeable when compared to the given filter level
type Logger ¶
type Logger struct {
// contains filtered or unexported fields
}
Logger is a Logger that creates Bunyan's compatible logs (see: https://github.com/trentm/node-bunyan)
func CreateIfNil ¶
CreateIfNil creates a new Logger if the given Logger is nil, otherwise return the said Logger
func CreateWithDestination ¶
CreateWithDestination creates a new Logger streaming to the given destination(s)
func CreateWithStream ¶
CreateWithStream creates a new Logger streaming to the given stream or list of streams
func FromContext ¶
FromContext retrieves the Logger stored in the context
func (*Logger) Errorf ¶
Errorf traces a message at the ERROR Level If the last argument is an error, a Record is added and the error string is added to the message
func (*Logger) Fatalf ¶
Fatalf traces a message at the FATAL Level If the last argument is an error, a Record is added and the error string is added to the message
func (*Logger) Flush ¶
func (log *Logger) Flush()
Flush flushes the stream (makes sure records are actually written)
func (*Logger) HttpHandler ¶
HttpHandler function will wrap an http handler with extra logging information
func (*Logger) Records ¶
Records adds key, value pairs as Record objects E.g.: log.Records("key1", value1, "key2", value2)
The key should be castable to a string If the last value is missing, its key is ignored
func (*Logger) SetFilterLevel ¶ added in v1.3.0
SetFilterLevel sets the filter level of the streamer
func (*Logger) ShouldWrite ¶
ShouldWrite tells if the given level should be written to this stream
type MultiStream ¶
type MultiStream struct {
// contains filtered or unexported fields
}
MultiStream is the Stream that writes to several streams
func (*MultiStream) Close ¶ added in v1.3.0
func (stream *MultiStream) Close()
Close closes the stream
func (*MultiStream) Flush ¶
func (stream *MultiStream) Flush()
Flush flushes the stream (makes sure records are actually written)
func (*MultiStream) SetFilterLevel ¶ added in v1.3.0
func (stream *MultiStream) SetFilterLevel(level Level) Streamer
SetFilterLevel sets the filter level of all streams
func (*MultiStream) ShouldWrite ¶
func (stream *MultiStream) ShouldWrite(level Level) bool
ShouldWrite tells if the given level should be written to this stream
func (*MultiStream) Write ¶
func (stream *MultiStream) Write(record Record) error
Write writes the given Record
type NilStream ¶
type NilStream struct { }
NilStream is the Stream that writes nowhere
Example ¶
stream := &logger.NilStream{} if err := stream.Write(logger.NewRecord().Set("bello", "banana").Set("だれ", "Me")); err != nil { os.Stdout.WriteString(err.Error() + "\n") } if stream.ShouldWrite(logger.ALWAYS) { os.Stdout.WriteString("This should not be seen\n") } stream.Flush()
Output:
func (*NilStream) Flush ¶
func (stream *NilStream) Flush()
Flush flushes the stream (makes sure records are actually written)
func (*NilStream) SetFilterLevel ¶ added in v1.3.0
SetFilterLevel sets the filter level of all streams
func (*NilStream) ShouldWrite ¶
ShouldWrite tells if the given level should be written to this stream
type PinoConverter ¶ added in v1.3.0
type PinoConverter struct { }
PinoConverter is used to convert Records to be read by pino
func (*PinoConverter) Convert ¶ added in v1.3.0
func (converter *PinoConverter) Convert(record Record) Record
Convert converts the Record into a Pino compatible Record
type Record ¶
type Record map[string]interface{}
Record is the map that contains all records of a log entry
If the value at a key is a func() interface the func will be called when the record is marshaled
func (Record) MarshalJSON ¶
MarshalJSON marshals this into JSON
func (Record) Merge ¶
Merge merges a source Record into this Record
values already set in this record cannot be overridden
func (*Record) UnmarshalJSON ¶
UnmarshalJSON unmarshals JSON into this
type StackDriverConverter ¶ added in v1.3.0
type StackDriverConverter struct { }
StackDriverConverter is used to convert a Record for StackDriver
func (*StackDriverConverter) Convert ¶ added in v1.3.0
func (converter *StackDriverConverter) Convert(record Record) Record
Convert converts the Record into a StackDriver compatible Record
type StackDriverStream ¶
type StackDriverStream struct { LogID string Parent string KeyFilename string Key interface{} Converter Converter FilterLevel Level // contains filtered or unexported fields }
StackDriverStream is the Stream that writes to the standard output
func (*StackDriverStream) Close ¶ added in v1.3.0
func (stream *StackDriverStream) Close()
Close closes the stream
func (*StackDriverStream) Flush ¶
func (stream *StackDriverStream) Flush()
Flush flushes the stream (makes sure records are actually written)
func (*StackDriverStream) SetFilterLevel ¶ added in v1.3.0
func (stream *StackDriverStream) SetFilterLevel(level Level) Streamer
SetFilterLevel sets the filter level
func (*StackDriverStream) ShouldWrite ¶
func (stream *StackDriverStream) ShouldWrite(level Level) bool
ShouldWrite tells if the given level should be written to this stream
func (*StackDriverStream) String ¶
func (stream *StackDriverStream) String() string
String gets a string version
func (*StackDriverStream) Write ¶
func (stream *StackDriverStream) Write(record Record) (err error)
Write writes the given Record
type StderrStream ¶
type StderrStream struct { *json.Encoder Converter Converter FilterLevel Level // contains filtered or unexported fields }
StderrStream is the Stream that writes to the standard output
Example ¶
output := CaptureStderr(func() { stream := &logger.StderrStream{} if err := stream.Write(logger.NewRecord().Set("bello", "banana").Set("だれ", "Me")); err != nil { os.Stderr.WriteString(err.Error() + "\n") } if stream.ShouldWrite(logger.TRACE) { os.Stderr.WriteString("This should not be seen, stream Filter: " + stream.FilterLevel.String() + "\n") } stream.Flush() }) fmt.Println(output)
Output: {"bello":"banana","だれ":"Me"}
func (*StderrStream) Close ¶ added in v1.3.0
func (stream *StderrStream) Close()
Close closes the stream
func (*StderrStream) Flush ¶
func (stream *StderrStream) Flush()
Flush flushes the stream (makes sure records are actually written)
func (*StderrStream) SetFilterLevel ¶ added in v1.3.0
func (stream *StderrStream) SetFilterLevel(level Level) Streamer
SetFilterLevel sets the filter level
func (*StderrStream) ShouldWrite ¶
func (stream *StderrStream) ShouldWrite(level Level) bool
ShouldWrite tells if the given level should be written to this stream
func (*StderrStream) String ¶
func (stream *StderrStream) String() string
String gets a string version
func (*StderrStream) Write ¶
func (stream *StderrStream) Write(record Record) error
Write writes the given Record
type StdoutStream ¶
type StdoutStream struct { *json.Encoder Converter Converter FilterLevel Level Unbuffered bool // contains filtered or unexported fields }
StdoutStream is the Stream that writes to the standard output
Example ¶
os.Setenv("LOG_FLUSHFREQUENCY", "10ms") defer os.Unsetenv("LOG_FLUSHFREQUENCY") stream := &logger.StdoutStream{} if err := stream.Write(logger.NewRecord().Set("bello", "banana").Set("だれ", "Me")); err != nil { os.Stdout.WriteString(err.Error() + "\n") } if stream.ShouldWrite(logger.TRACE) { os.Stdout.WriteString("This should not be seen, stream Filter: " + stream.FilterLevel.String() + "\n") } if err := stream.Write(logger.NewRecord().Set("bello", "banana").Set("level", logger.ERROR)); err != nil { os.Stdout.WriteString(err.Error() + "\n") } stream.Flush() time.Sleep(11 * time.Millisecond) stream.Close()
Output: {"bello":"banana","だれ":"Me"} {"bello":"banana","level":50}
func (*StdoutStream) Close ¶ added in v1.3.0
func (stream *StdoutStream) Close()
Close closes the stream
func (*StdoutStream) Flush ¶
func (stream *StdoutStream) Flush()
Flush flushes the stream (makes sure records are actually written)
func (*StdoutStream) SetFilterLevel ¶ added in v1.3.0
func (stream *StdoutStream) SetFilterLevel(level Level) Streamer
SetFilterLevel sets the filter level
func (*StdoutStream) ShouldWrite ¶
func (stream *StdoutStream) ShouldWrite(level Level) bool
ShouldWrite tells if the given level should be written to this stream
func (*StdoutStream) String ¶
func (stream *StdoutStream) String() string
String gets a string version
func (*StdoutStream) Write ¶
func (stream *StdoutStream) Write(record Record) error
Write writes the given Record
type Streamer ¶
type Streamer interface { Write(record Record) error ShouldWrite(level Level) bool SetFilterLevel(level Level) Streamer Flush() Close() }
Streamer is the interface a Logger writes to
func CreateMultiStream ¶ added in v1.1.1
CreateMultiStream creates a MultiStream that contains all given Streamer objects
func CreateStreamWithDestination ¶ added in v1.1.0
CreateStreamWithDestination creates a new Streamer from a list of strings
"stdout" will create a StdoutStream
"stderr" will create a StderrStream
"nil", "null" will create a NilStream
"stackdriver" will create a StackDriverStream
"gcp", "googlecloud", "google" will create a StdoutStream, unbuffered, with the StackDriverConverter
"file:///path/to/file" or "path/to/file", "/path/to/file" will create a FileStream on the given location
If more than one string is given, a MultiStream of all Streams from strings is created.
If the environment variable DEBUG is set to 1, all Streams are created unbuffered.
If the list is empty, the environment variable LOG_DESTINATION is used.
Source Files ¶
- converter.go
- converter_bunyan.go
- converter_pino.go
- converter_stackdriver.go
- doc.go
- gettid-linux.go
- level.go
- logger-context.go
- logger-http.go
- logger.go
- record.go
- stream-file.go
- stream-logger.go
- stream-multistream.go
- stream-nil.go
- stream-stackdriver.go
- stream-stderr.go
- stream-stdout.go
- stream.go
- version.go
- writer.go