This is a creation in Article, where the information may have evolved or changed.
Objective
Production environment on the output of high log level is not conducive to positioning, you will encounter such a scenario, we change the configuration to modify the log level, and then restart to expect to locate the problem, however, the problem does not appear after restarting, so the hot load configuration is necessary to add in.
Practice
Directly on the code:
PackageMainImport("Encoding/json" "FMT" "Io/ioutil" "OS" "Os/signal" "Syscall" "Time")typeConfigstruct{Modestring ' JSON: ' Mode '}var(config *config)funcLoadconfig () {f, err: = Ioutil. ReadFile ("Config.json")ifErr! =Nil{FMT. Println ("[test_sigusr] Load config:", err)} err = json. Unmarshal (f, &config)ifErr! =Nil{FMT. Println ("[test_sigusr] Para config failed:", err)}}funcInit () {loadconfig () fmt. Println ("[test_sigusr] Load config; Mode: ", CONFIG. Mode) S: = Make(ChanOs. Signal,1) signal. Notify (S, Syscall. SIGUSR2)Go func() { for{<-s loadconfig () fmt. Println ("[test_sigusr] ReLoad config; Mode: ", CONFIG. Mode)}} ()}funcMain () {Q: = Make(ChanOs. Signal,1) signal. Notify (q, Syscall. SIGINT) for{Select{ Case<-q:fmt. Println ("[TEST_SIGUSR]---ibye----") OS. Exit(1)default:} fmt. Println ("[TEST_SIGUSR]---waiting to reload----") time. Sleep (time. Second *2) }}
The file directory is as follows:
Wenhao-ma: Test_sigusr wenhao$ Lltotal4944-RW- R--R-- 1Wenhao Staff - One A -: theConfig.Json-RWXR-XR- x 1Wenhao Staff2520352 One A -: ASigusr-RW- R--R-- 1Wenhao Staff973 One A -: -Sigusr.Go
wenhao-MA:test_sigusr wenhao$ cat config.json{"Mode":"Debug"}
After the compilation starts, modify config.json The median Mode , kill -USR2 $(pidof sigusr)
Results:
Wenhao-ma:test_sigusr wenhao$./sigusr [test_sigusr] Load config; Mode: Debug[test_sigusr] ---Waiting to Reload----[test_sigusr] ---Waiting to Reload----[test_sigusr] ReLoad config; Mode: ERROR