-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Logging
Elastic has three kinds of logs. All of them use the default log.Logger
from the standard library.
First, there's the error log. It will contain critical messages, e.g. nodes leaving or joining the cluster or connections that fail. You can set the error log with elastic.SetErrorLog(*log.Logger)
. It is nil
by default, i.e. Elastic is silent.
Second, there's the info log. It will contain informational messages, e.g. request and response time. You can set the info log with elastic.SetInfoLog(*log.Logger)
. It is nil
by default, i.e. Elastic is silent.
Third, there's the trace log. It is suitable for debugging what's going on on the wire. It is nil
by default. You can specify a trace log with elastic.SetTraceLog(*log.Logger)
.
This is an example where the error log is set to os.Stderr
and info log is set to os.Stdout
:
client, err := elastic.NewClient(
elastic.SetErrorLog(log.New(os.Stderr, "ELASTIC ", log.LstdFlags)),
elastic.SetInfoLog(log.New(os.Stdout, "", log.LstdFlags)))
If you want to log to a file, use something like this:
file, err := os.OpenFile("elastic.log", os.O_RDWR|os.O_CREATE|os.O_APPEND, 0664)
if err != nil {
panic(err)
}
client, err := elastic.NewClient(
elastic.SetErrorLog(log.New(file, "ELASTIC ", log.LstdFlags)))