Reputation: 511
I'm using the github.com/golang/glog
, and want to change the flag stderrthreshold
or V-leveled logging at runtime, that is without re-compile the binary and re-run it. I had searched some resource, but all the answers are changes at compile time. Is there anyway to change it at runtime cause I don't want to stop my service?
Upvotes: 2
Views: 3636
Reputation: 297
glog package reads configuration from flags. You can modify the flags at runtime from your code to change glog's behaviour.
package main
import (
"flag"
"fmt"
"github.com/golang/glog"
)
func main() {
// glog needs flag.Parse() call
flag.Parse()
glog.Info("default level")
glog.V(3).Info("Level 3 log")
if glog.V(3) {
fmt.Println("Changing log level to 5")
flag.Lookup("v").Value.Set("5")
}
glog.V(4).Info("Level 4 log")
glog.V(5).Info("Level 5 log")
}
Results in
➜ ./main --alsologtostderr -v 2
I0223 23:42:26.661984 4587 main.go:12] default level
➜ ./main --alsologtostderr -v 3
I0223 23:42:28.209673 4600 main.go:12] default level
I0223 23:42:28.210174 4600 main.go:13] Level 3 log
Changing log level to 5
I0223 23:42:28.210210 4600 main.go:18] Level 4 log
I0223 23:42:28.210230 4600 main.go:20] Level 5 log
➜ ./main --alsologtostderr -v 4
I0223 23:42:30.927222 4614 main.go:12] default level
I0223 23:42:30.928018 4614 main.go:13] Level 3 log
Changing log level to 5
I0223 23:42:30.928072 4614 main.go:18] Level 4 log
I0223 23:42:30.928088 4614 main.go:20] Level 5 log
Upvotes: 2