Skip to content

Instantly share code, notes, and snippets.

@aarti
Last active January 31, 2020 23:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aarti/a454e376960c08ce1272d9c373f2a5f0 to your computer and use it in GitHub Desktop.
Save aarti/a454e376960c08ce1272d9c373f2a5f0 to your computer and use it in GitHub Desktop.
logging level in go at runtime
// Change the logging level curl -X PUT -d '{"level":"debug"}' localhost:8080/logger
package main
import (
"log"
"net/http"
"os"
"time"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)
func main() {
atom := zap.NewAtomicLevel()
// To keep the example deterministic, disable timestamps in the output.
encoderCfg := zap.NewProductionEncoderConfig()
encoderCfg.TimeKey = ""
logger := zap.New(zapcore.NewCore(
zapcore.NewJSONEncoder(encoderCfg),
zapcore.Lock(os.Stdout),
atom,
))
defer logger.Sync()
go func() {
for {
logger.Info("Testing Info")
logger.Debug("Testing Debug")
logger.Warn("Testing Warn")
time.Sleep(5 * time.Second)
}
}()
http.Handle("/logger", atom)
log.Fatal(http.ListenAndServe(":8080", nil))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment