From d123fc6cec0c235f3bca3e0b2e6052159426e91f Mon Sep 17 00:00:00 2001 From: noreabu Date: Mon, 25 Sep 2017 18:29:54 +0200 Subject: [PATCH 1/2] example for changing logging level by public function, called with HTTP GET handler in gin-gonic --- examples/setLogLevels.go | 118 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 118 insertions(+) create mode 100644 examples/setLogLevels.go diff --git a/examples/setLogLevels.go b/examples/setLogLevels.go new file mode 100644 index 0000000..633001f --- /dev/null +++ b/examples/setLogLevels.go @@ -0,0 +1,118 @@ +package logging + +import ( + "errors" + "io/ioutil" + "net/http" + "strconv" + "strings" + + "github.com/romana/rlog" + "gopkg.in/gin-gonic/gin.v1" +) + +// It is necessary to have a rlog configuration file with entries for both: RLOG_LOG_LEVEL and RLOG_TRACE_LEVEL +// the const variables were copied from rlog +// +// The known log levels +const ( + levelNone = iota + levelCrit + levelErr + levelWarn + levelInfo + levelDebug + levelTrace +) + +// Translation map from level to string representation +var levelStrings = map[int]string{ + levelTrace: "TRACE", + levelDebug: "DEBUG", + levelInfo: "INFO", + levelWarn: "WARN", + levelErr: "ERROR", + levelCrit: "CRITICAL", + levelNone: "NONE", +} + +// Translation from level string to number. +var levelNumbers = map[string]int{ + "TRACE": levelTrace, + "DEBUG": levelDebug, + "INFO": levelInfo, + "WARN": levelWarn, + "ERROR": levelErr, + "CRITICAL": levelCrit, + "NONE": levelNone, +} + +// Path to the config file. Defined here for standalone example purpose +const ( + rlogConfigFile = "/tmp/rlog.conf" + rlogConfigFileUmask = 0644 +) + +// LogConfHandler is a handler function for the gin-gonic framework to change trace and log level +func LogConfHandler(c *gin.Context) { + + // get HTTP GET params + level := c.Query("level") + trace := c.Query("trace") + + traceInt, err := strconv.Atoi(trace) + + // continue if trace is an integer + if err == nil { + err := setGlobalLogConf(level, traceInt) + + if err != nil { + c.String(http.StatusInternalServerError, err.Error()) + return + } + } + + c.String(http.StatusOK, "setting log level to "+level+" trace "+trace) +} + +// setGlobalLogConf to change logging settings while running +func setGlobalLogConf(level string, trace int) error { + + // check if specified log level is within allowed values + if _, ok := levelNumbers[level]; ok { + // check for config file + configFile, err := ioutil.ReadFile(rlogConfigFile) + if err != nil { + return errors.New("could not read config file: " + err.Error()) + } + + // replace config lines + lines := strings.Split(string(configFile), "\n") + + for i, line := range lines { + if strings.Contains(line, "RLOG_LOG_LEVEL") { + lines[i] = "RLOG_LOG_LEVEL = " + level + continue + } + + if strings.Contains(line, "RLOG_TRACE_LEVEL") { + lines[i] = "RLOG_TRACE_LEVEL = " + strconv.Itoa(trace) + } + } + output := strings.Join(lines, "\n") + err = ioutil.WriteFile(rlogConfigFile, []byte(output), rlogConfigFileUmask) + + if err != nil { + return errors.New("could not replace config file: " + err.Error()) + + } + + // set new config + rlog.SetConfFile(rlogConfigFile) + rlog.Debugf("[LOG] switching to log level %v with trace %v", level, trace) + + return nil + } + + return errors.New("invalid value for level, must be valid rlog log level") +} From b35fa761b7f559933f23c69237f8570fa34783a8 Mon Sep 17 00:00:00 2001 From: noreabu Date: Mon, 13 Nov 2017 20:40:30 +0100 Subject: [PATCH 2/2] now package main for testing, small improvment regarding missing start values in file --- examples/setLogLevels.go | 28 +++++++++++++++++++++++++--- 1 file changed, 25 insertions(+), 3 deletions(-) diff --git a/examples/setLogLevels.go b/examples/setLogLevels.go index 633001f..685b5a2 100644 --- a/examples/setLogLevels.go +++ b/examples/setLogLevels.go @@ -1,4 +1,4 @@ -package logging +package main import ( "errors" @@ -49,7 +49,7 @@ var levelNumbers = map[string]int{ // Path to the config file. Defined here for standalone example purpose const ( - rlogConfigFile = "/tmp/rlog.conf" + rlogConfigFile = "/tmp/rlog.conf" rlogConfigFileUmask = 0644 ) @@ -89,16 +89,31 @@ func setGlobalLogConf(level string, trace int) error { // replace config lines lines := strings.Split(string(configFile), "\n") + // check for pre-existing values in config, override or append + hasLogLevelEntry := false + hasTraceLevelEntry := false + for i, line := range lines { if strings.Contains(line, "RLOG_LOG_LEVEL") { lines[i] = "RLOG_LOG_LEVEL = " + level + hasLogLevelEntry = true continue } if strings.Contains(line, "RLOG_TRACE_LEVEL") { lines[i] = "RLOG_TRACE_LEVEL = " + strconv.Itoa(trace) + hasTraceLevelEntry = true } } + + // append new options if neccessary + if !hasLogLevelEntry { + lines = append(lines, "RLOG_LOG_LEVEL = "+level) + } + if !hasTraceLevelEntry { + lines = append(lines, "RLOG_TRACE_LEVEL = "+strconv.Itoa(trace)) + } + output := strings.Join(lines, "\n") err = ioutil.WriteFile(rlogConfigFile, []byte(output), rlogConfigFileUmask) @@ -109,10 +124,17 @@ func setGlobalLogConf(level string, trace int) error { // set new config rlog.SetConfFile(rlogConfigFile) - rlog.Debugf("[LOG] switching to log level %v with trace %v", level, trace) return nil } return errors.New("invalid value for level, must be valid rlog log level") } + +func main() { + // check file contents of rlogConfFile to see results e.g. with: "cat /tmp/rlog.conf" before and after + setGlobalLogConf("DEBUG", 1) + rlog.Trace(3, "this will not appear in the log") + setGlobalLogConf("DEBUG", 3) + rlog.Trace(3, "this will appear in the log") +}