Go Agent v0.4.3
October 16, 2024
Fixed
Fixed the error collecting os.Stdout
and os.Stderr
logs.
-
It modified the
trace.Init
function to enable log collection. It changes to run before other log libraries if possible. -
The function has been added that allows to use io.Writer that wraps
os.Stdout
andos.Stderr
.
Below are instructions to use the changed log collection feature.
Configuring the agent
Set agent options in the whatap.conf file before running the application.
logsink_enabled=true
# Enable stdout collection
logsink_stdout_enabled=true
# Enable stderr collection
logsink_stderr_enabled=true
# Optional. This is a setting for compressing data.
logsin_zip_enabled=true
Before initializing the logging library
First, you have to call the trace.Init()
function. It internally wraps os.Stdout
and os.Stderr
. Afterwards, the log library automatically collects logs when os.Stdout
and os.Stderr
have been set.
Example of the zap library
If you call the trace.Init()
function before setting os.Stdout
, logs are collected automatically. It collects logs by using the WhaTap log while outputting to os.Stdout
.
import (
"github.com/whatap/go-api/trace"
"github.com/whatap/go-api/logsink"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)
func main() {
trace.Init(nil)
//It must be executed before closing the app.
defer trace.Shutdown()
// fmt.Println("Logger stdout=", os.Stdout, zapcore.AddSync(os.Stdout))
consoleCore := zapcore.NewCore(
zapcore.NewConsoleEncoder(consoleEncoderConfig),
zapcore.AddSync(os.Stdout),
zap.InfoLevel,
)
// Menggabungkan core file dan console
core := zapcore.NewTee(consoleCore)
Log = zap.New(core, zap.AddCaller(), zap.AddStacktrace(zapcore.ErrorLevel))
Log.Info("logger started")
...
}
After initializing the logging library
You can set the io.Writer wrapped through the configuration function in the logging library.
-
logsink.GetWriterHookStdout()
: It returns io.Writer that has wrappedos.Stderr
. It collects the log by using the WhaTap log while outputting toos.Stdout
. -
logsink.GetWriterHookStderr()
: It returns io.Writer that has wrappedos.Stderr
. The wrapped io.Writer outputs toos.Stderr
and collects the log contents by using the WhaTap log. -
The log package
sets
os.Stderr
at the import time (in theinit
function). Because thetrace.Init
function cannot be called first, set the wrapped io.Writer (os.Stderr
) through thelog.SetOutput
function.
The output using the print
function of the log package used afterwards is output to os.Stderr
through the wrapped io.Writer and the log contents are collected as WhaTap logs.
import (
"log"
"github.com/whatap/go-api/logsink"
)
...
if logsink.GetWriterHookStderr() != nil {
// set single writer
log.SetOutput(logsink.GetWriterHookStderr())
// set multi writer
multi := io.MultiWriter(file, logsink.GetWriterFromStderr())
log.SetOutput(logsink.GetWriterHookStderr())
}
//
log.Println("aaaaa")
...