Documentation ¶
Overview ¶
Package say is a logging and metrics-reporting library.
See https://github.com/go-say/say for a presentation of the project.
Introduction ¶
By default, Say prints all messages (logs and metrics) to standard output.
When a listener is set using SetListener(), messages are handler by the listener in a goroutine.
Logging functions ¶
Say provides 5 severity levels:
- Debug
- Info
- Warning
- Error
- Fatal
Metrics functions ¶
Say provides 4 metrics-reporting functions:
- Event: track the occurence of a particular event (user sign-up, query to the database)
- Value: measure a value associated with a particular event (number of items returned by a search)
- Timing.Say: measure a duration value (database query duration, webservice call duration)
- Gauge: capture the current value of something that changes over time (number of active goroutines, number of connected users)
These metrics are directly inspired from StatsD metrics:
- Event: counter
- Value: histogram / timing
- Timing.Say: timing
- Gauge: gauge
See the function's descriptions below for more info.
Links:
- StatsD metrics: https://github.com/etsy/statsd/blob/master/docs/metric_types.md
- Datadog documentation: http://docs.datadoghq.com/guides/metrics/#counters
Package-level or methods ¶
These functions can be called at a package-level or you can create a Logger and use the associated methods:
log := new(say.Logger) log.Info("Hello!")
Data ¶
The point of using a Logger is to associate key-value pairs to it:
log := new(say.Logger) log.AddData("request_id", requestID) log.Info("Hello!") // Output: INFO Hello! | request_id=3
All logging and metric-reporting functions also accept key-value pairs:
Info("Hello!", "name", "Bob", "age", 30) // Output: INFO Hello! | name="Bob" age=30
Example ¶
// Capture panics as FATAL. defer say.CapturePanic() say.Info("Getting list of users...") say.Value("user_found", 42)
Output: INFO Getting list of users... VALUE user_found:42
Index ¶
- func AddData(key string, value interface{})
- func CapturePanic()
- func CaptureStandardLog()
- func CheckError(v interface{}, data ...interface{})
- func Debug(msg string, data ...interface{})
- func DisableStackTraces(b bool)
- func Error(v interface{}, data ...interface{})
- func Event(name string, data ...interface{})
- func Events(name string, incr int, data ...interface{})
- func Fatal(v interface{}, data ...interface{})
- func Flush()
- func Gauge(name string, value interface{}, data ...interface{})
- func Info(msg string, data ...interface{})
- func Mute() io.Writer
- func Redirect(w io.Writer) (oldW io.Writer)
- func SetData(data ...interface{})
- func SetDebug(b bool)
- func SetListener(f func(*Message))
- func Time(name string, f func(), data ...interface{})
- func Value(name string, value interface{}, data ...interface{})
- func Warning(v interface{}, data ...interface{})
- type Data
- type Hook
- type KVPair
- type Logger
- func (l *Logger) AddData(key string, value interface{})
- func (l *Logger) CapturePanic()
- func (l *Logger) CaptureStandardLog()
- func (l *Logger) CheckError(v interface{}, data ...interface{})
- func (l *Logger) Debug(msg string, data ...interface{})
- func (l *Logger) Error(v interface{}, data ...interface{})
- func (l *Logger) Event(name string, data ...interface{})
- func (l *Logger) Events(name string, incr int, data ...interface{})
- func (l *Logger) Fatal(v interface{}, data ...interface{})
- func (l *Logger) Gauge(name string, value interface{}, data ...interface{})
- func (l *Logger) Info(msg string, data ...interface{})
- func (l *Logger) NewLogger(opts ...Option) *Logger
- func (l *Logger) NewTiming() Timing
- func (l *Logger) SetData(data ...interface{})
- func (l *Logger) Time(name string, f func(), data ...interface{})
- func (l *Logger) Value(name string, value interface{}, data ...interface{})
- func (l *Logger) Warning(v interface{}, data ...interface{})
- type Message
- func (m *Message) Duration() (time.Duration, bool)
- func (m *Message) Error() string
- func (m *Message) Float64() (float64, bool)
- func (m *Message) Int() (n int, ok bool)
- func (m *Message) Key() string
- func (m *Message) StackTrace() string
- func (m *Message) Value() string
- func (m *Message) WriteJSONTo(w io.Writer) (int, error)
- func (m *Message) WriteTo(w io.Writer) (int64, error)
- type Option
- type Timing
- type Type
Examples ¶
- Package
- CapturePanic
- CaptureStandardLog
- CheckError
- Debug
- DebugHook
- Error
- Event
- Events
- Fatal
- Gauge
- Hook
- Info
- Logger.AddData
- Logger.CapturePanic
- Logger.CaptureStandardLog
- Logger.CheckError
- Logger.Debug
- Logger.Error
- Logger.Event
- Logger.Events
- Logger.Fatal
- Logger.Gauge
- Logger.Info
- Logger.NewLogger
- Logger.SetData
- Logger.Time
- Logger.Value
- Logger.Warning
- NewLogger
- Time
- TimeHook
- Timing.Say
- Value
- Warning
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func AddData ¶
func AddData(key string, value interface{})
AddData adds a key-value pair that will be printed along with all messages sent with the package-level functions.
func CapturePanic ¶
func CapturePanic()
CapturePanic captures panic values as FATAL messages.
Example ¶
defer say.CapturePanic() panic("oops!") // The panic message will be printed with a FATAL severity.
Output:
func CaptureStandardLog ¶
func CaptureStandardLog()
CaptureStandardLog captures the log lines coming from the log package of the standard library. Captured lines are output with an INFO level.
Example ¶
say.CaptureStandardLog() log.Print("Hello from the standard library!")
Output: INFO Hello from the standard library!
func CheckError ¶
func CheckError(v interface{}, data ...interface{})
CheckError prints an ERROR message with the stack trace.
If v is nil, nothing is printed. If v is a func() error, then Error run v and prints an error only if v return a non-nil error.
Example ¶
f, err := os.Open("foo.txt") say.CheckError(err) // Print an error only if err is not nil. defer say.CheckError(f.Close) // Call Close and print the error if not nil.
Output:
func Debug ¶
func Debug(msg string, data ...interface{})
Debug prints a DEBUG message only if the debug mode is on.
Example ¶
say.SetDebug(false) say.Debug("foo") say.SetDebug(true) say.Debug("bar")
Output: DEBUG bar
func DisableStackTraces ¶
func DisableStackTraces(b bool)
DisableStackTraces disables printing the stack traces by default. This can still be
func Error ¶
func Error(v interface{}, data ...interface{})
Error prints an ERROR message with the stack trace.
Example ¶
_, err := os.Open("foo.txt") if err != nil { say.Error(err) // Print an error with the stack trace. }
Output:
func Event ¶
func Event(name string, data ...interface{})
Event prints an EVENT message. Use it to track the occurence of a particular event (e.g. a user signs up, a database query fails).
Example ¶
say.Event("new_user", "id", 7654)
Output: EVENT new_user | id=7654
func Events ¶
Events prints an EVENT message with an increment value. Use it to track the occurence of a batch of events (e.g. how many new files were uploaded).
Example ¶
say.Events("file_uploaded", 3)
Output: EVENT file_uploaded:3
func Fatal ¶
func Fatal(v interface{}, data ...interface{})
Fatal prints a FATAL message with the stack trace.
Example ¶
_, err := os.Open("foo.txt") if err != nil { say.Fatal(err) // Print a fatal error with the stack trace. }
Output:
func Flush ¶
func Flush()
Flush flushes the message queue. It is a no-op when SetListener has not been used.
func Gauge ¶
func Gauge(name string, value interface{}, data ...interface{})
Gauge prints a GAUGE message. Use it to capture the current value of something that changes over time (e.g. number of active goroutines, number of connected users)
Example ¶
say.Gauge("connected_users", 73)
Output: GAUGE connected_users:73
func Info ¶
func Info(msg string, data ...interface{})
Info prints an INFO message.
Example ¶
say.Info("Connecting to server...", "ip", "127.0.0.1")
Output: INFO Connecting to server... | ip="127.0.0.1"
func Redirect ¶
Redirect redirects the output to the given writer. It returns the writer where outputs were previously redirected to.
It is only effective when SetListener has not been used.
func SetData ¶
func SetData(data ...interface{})
SetData sets a key-value pair that will be printed along with all messages sent with the package-level functions.
func SetDebug ¶
func SetDebug(b bool)
SetDebug sets whether Say is in debug mode. The debug mode is off by default.
This function must not be called concurrently with the other functions of this package.
func SetListener ¶
func SetListener(f func(*Message))
SetListener sets the function that is applied to each message.
SetListener(nil) restores the default behavior wich is printing messages to the standard output.
func Time ¶
func Time(name string, f func(), data ...interface{})
Time prints a VALUE message with the duration in milliseconds of running f.
Example ¶
say.Time("duration", func() { // The code that needs to be timed. })
Output: VALUE duration:17ms
Types ¶
type Hook ¶
type Hook func() interface{}
A Hook is a function used to provide dynamic Data values.
Example ¶
goroutinesHook := say.Hook(func() interface{} { return runtime.NumGoroutine }) // Print the current number of goroutines with each message. say.SetData("num_goroutine", goroutinesHook)
Output:
func DebugHook ¶
func DebugHook(v interface{}) Hook
DebugHook allows printing a key-value pairs only when Say is in debug mode.
Example ¶
query := "SELECT * FROM users WHERE id = ?" say.SetDebug(true) say.Event("db.get_user", "query", say.DebugHook(query)) // Print the query. say.SetDebug(false) say.Event("db.get_user", "query", say.DebugHook(query)) // Omit the query.
Output: EVENT db.get_user | query="SELECT * FROM users WHERE id = ?" EVENT db.get_user
type KVPair ¶
type KVPair struct { Key string Value interface{} }
KVPair represents a key-value pair.
type Logger ¶
type Logger struct {
// contains filtered or unexported fields
}
Logger is the object that prints messages.
func NewLogger ¶
NewLogger creates a new Logger inherits the Data and SkipStackFrames values from the package-level Logger.
Example ¶
say.SetData("weather", "sunny") log := say.NewLogger() log.Info("hello") // INFO hello | weather="sunny"
Output:
func (*Logger) AddData ¶
AddData adds a key-value pair that will be printed along with all messages sent with this Logger.
Example ¶
log := new(say.Logger) log.AddData("id", 5) log.Info("hello") log.AddData("foo", "bar") log.Info("dear")
Output: INFO hello | id=5 INFO dear | id=5 foo="bar"
func (*Logger) CapturePanic ¶
func (l *Logger) CapturePanic()
CapturePanic captures panic values as FATAL messages.
Example ¶
log := new(say.Logger) defer log.CapturePanic() panic("oops!") // The panic message will be printed with a FATAL severity.
Output:
func (*Logger) CaptureStandardLog ¶
func (l *Logger) CaptureStandardLog()
CaptureStandardLog captures the log lines coming from the log package of the standard library. Captured lines are output with an INFO level.
Example ¶
l := new(say.Logger) l.CaptureStandardLog() log.Print("Hello from the standard library!")
Output: INFO Hello from the standard library!
func (*Logger) CheckError ¶
func (l *Logger) CheckError(v interface{}, data ...interface{})
CheckError prints an ERROR message with the stack trace.
If v is nil, nothing is printed. If v is a func() error, then Error run v and prints an error only if v return a non-nil error.
Example ¶
f, err := os.Open("foo.txt") log := new(say.Logger) log.CheckError(err) // Print an error only if err is not nil. defer log.CheckError(f.Close) // Call Close and print the error if not nil.
Output:
func (*Logger) Debug ¶
Debug prints a DEBUG message only if the debug mode is on.
Example ¶
log := new(say.Logger) say.SetDebug(false) log.Debug("foo") say.SetDebug(true) log.Debug("bar")
Output: DEBUG bar
func (*Logger) Error ¶
func (l *Logger) Error(v interface{}, data ...interface{})
Error prints an ERROR message with the stack trace.
Example ¶
log := new(say.Logger) _, err := os.Open("foo.txt") if err != nil { log.Error(err) // Print an error with the stack trace. }
Output:
func (*Logger) Event ¶
Event prints an EVENT message. Use it to track the occurence of a particular event (e.g. a user signs up, a database query fails).
Example ¶
log := new(say.Logger) log.Event("new_user", "id", 7654)
Output: EVENT new_user | id=7654
func (*Logger) Events ¶
Events prints an EVENT message with an increment value. Use it to track the occurence of a batch of events (e.g. how many new files were uploaded).
Example ¶
log := new(say.Logger) log.Events("file_uploaded", 3)
Output: EVENT file_uploaded:3
func (*Logger) Fatal ¶
func (l *Logger) Fatal(v interface{}, data ...interface{})
Fatal prints a FATAL message with the stack trace.
Example ¶
log := new(say.Logger) _, err := os.Open("foo.txt") if err != nil { log.Fatal(err) // Print a fatal error with the stack trace. }
Output:
func (*Logger) Gauge ¶
Gauge prints a GAUGE message. Use it to capture the current value of something that changes over time (e.g. number of active goroutines, number of connected users)
Example ¶
log := new(say.Logger) log.Gauge("connected_users", 73)
Output: GAUGE connected_users:73
func (*Logger) Info ¶
Info prints an INFO message.
Example ¶
log := new(say.Logger) log.Info("Connecting to server...", "ip", "127.0.0.1")
Output: INFO Connecting to server... | ip="127.0.0.1"
func (*Logger) NewLogger ¶
NewLogger creates a new Logger that inherits the Data and SkipStackFrames values from the parent Logger.
Example ¶
log := new(say.Logger) // Create a clean Logger. log.SetData("id", 5) log2 := log.NewLogger() // log2 inherits its parent settings. log2.AddData("age", 53) log2.Info("hello")
Output: INFO hello | id=5 age=53
func (*Logger) NewTiming ¶
NewTiming returns a new Timing with the same associated data than the Logger.
func (*Logger) SetData ¶
func (l *Logger) SetData(data ...interface{})
SetData sets a key-value pair that will be printed along with all messages sent with this Logger.
Example ¶
log := new(say.Logger) log.SetData("id", 5, "foo", "bar") log.Info("hello")
Output: INFO hello | id=5 foo="bar"
func (*Logger) Time ¶
Time prints a VALUE message with the duration in milliseconds of running f.
Example ¶
log := new(say.Logger) log.Time("duration", func() { // The code that needs to be timed. })
Output: VALUE duration:17ms
type Message ¶
A Message represents a log line or a metric.
func (*Message) Duration ¶
Duration returns the duration of a VALUE message. If the value is not a duration, ok is false.
func (*Message) Float64 ¶
Float64 returns the value as an float64. If the value is not a float64, ok is false. If the value is a duration in milliseconds, return the number of milliseconds. It returns 1 if the message is an EVENT without an increment.
func (*Message) Int ¶
Int returns the value as an integer. If the value is not an integer, ok is false. If the value is a duration in milliseconds, return the number of milliseconds. It returns 1 if the message is an EVENT without an increment.
func (*Message) StackTrace ¶
StackTrace returns the stack trace of an ERROR or FATAL message.
func (*Message) WriteJSONTo ¶
WriteJSONTo writes the JSON-encoded form of the Message to w.
type Option ¶
type Option func(*Logger)
An Option allows to customize a Logger.
func SkipStackFrames ¶
SkipStackFrames sets the number of stack frames to skip in the Error and Fatal methods. It is 0 by default.
A value of -1 disable printing the stack traces with this Logger.
type Timing ¶
type Timing struct {
// contains filtered or unexported fields
}
A Timing helps printing a duration.
func NewTiming ¶
func NewTiming() Timing
NewTiming returns a new Timing with the package-level data.
func (Timing) Say ¶
Say prints a VALUE message with the duration in milliseconds since the Timing has been created. Use it to measure a duration value (e.g. database query duration, webservice call duration).
Example ¶
t := say.NewTiming() // Do some stuff. t.Say("duration")
Output: VALUE duration:17ms