Last active
January 31, 2020 23:26
-
-
Save aarti/a454e376960c08ce1272d9c373f2a5f0 to your computer and use it in GitHub Desktop.
logging level in go at runtime
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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