From 4202b7eb95246414cbe1d744a4b3594a55198e41 Mon Sep 17 00:00:00 2001 From: edwardro22 Date: Mon, 18 Sep 2017 15:32:46 +0000 Subject: [PATCH 1/3] Added stdout as an log option --- cmd/cgr-engine/cgr-engine.go | 14 ++++++++------ config/config.go | 11 ++++++++--- config/config_defaults.go | 1 + config/config_json_test.go | 1 + config/config_test.go | 3 +++ config/libconfig_json.go | 1 + utils/consts.go | 2 ++ utils/logger.go | 33 +++++++++++++++++++++++---------- 8 files changed, 47 insertions(+), 19 deletions(-) diff --git a/cmd/cgr-engine/cgr-engine.go b/cmd/cgr-engine/cgr-engine.go index f3ff1c867..0c2968f08 100644 --- a/cmd/cgr-engine/cgr-engine.go +++ b/cmd/cgr-engine/cgr-engine.go @@ -22,7 +22,6 @@ import ( "flag" "fmt" "log" - "log/syslog" "os" "runtime" "runtime/pprof" @@ -64,6 +63,7 @@ var ( cpuprofile = flag.String("cpuprofile", "", "write cpu profile to file") scheduledShutdown = flag.String("scheduled_shutdown", "", "shutdown the engine after this duration") singlecpu = flag.Bool("singlecpu", false, "Run on single CPU core") + syslogger = flag.String("logger", "", "logger <*syslog|*stdout>") logLevel = flag.Int("log_level", -1, "Log level (0-emergency to 7-debug)") cfg *config.CGRConfig @@ -635,11 +635,13 @@ func writePid() { // initLogger will initialize syslog writter, needs to be called after config init func initLogger(cfg *config.CGRConfig) error { - if l, err := syslog.New(syslog.LOG_INFO, - fmt.Sprintf("CGRateS <%s> ", cfg.InstanceID)); err != nil { + sylogger := cfg.Logger + if *syslogger != "" { // Modify the log level if provided by command arguments + sylogger = *syslogger + } + err := utils.Newlogger(sylogger) + if err != nil { return err - } else { - utils.Logger.SetSyslog(l) } return nil } @@ -728,7 +730,7 @@ func main() { loadDb = storDb.(engine.LoadStorage) cdrDb = storDb.(engine.CdrStorage) engine.SetCdrStorage(cdrDb) - if err := engine.CheckVersions(storDb); err != nil { + if err := engine.CheckVersions(storDb); err != nil { fmt.Println(err.Error()) return } diff --git a/config/config.go b/config/config.go index 9c9fd7efc..7e64c96ac 100755 --- a/config/config.go +++ b/config/config.go @@ -225,6 +225,7 @@ type CGRConfig struct { FailedPostsDir string // Directory path where we store failed http requests MaxCallDuration time.Duration // The maximum call duration (used by responder when querying DerivedCharging) // ToDo: export it in configuration file LockingTimeout time.Duration // locking mechanism timeout to avoid deadlocks + Logger string // dictates the way logs are displayed/stored LogLevel int // system wide log level, nothing higher than this will be logged RALsEnabled bool // start standalone server (no balancer) RALsCDRStatSConns []*HaPoolConfig // address where to reach the cdrstats service. Empty to disable stats gathering <""|internal|x.y.z.y:1234> @@ -714,6 +715,13 @@ func (self *CGRConfig) loadFromJsonCfg(jsnCfg *CgrJsonCfg) error { if jsnGeneralCfg.Instance_id != nil && *jsnGeneralCfg.Instance_id != "" { self.InstanceID = *jsnGeneralCfg.Instance_id } + if jsnGeneralCfg.Logger != nil { + self.Logger = *jsnGeneralCfg.Logger + } + if jsnGeneralCfg.Log_level != nil { + self.LogLevel = *jsnGeneralCfg.Log_level + } + if jsnGeneralCfg.Dbdata_encoding != nil { self.DBDataEncoding = *jsnGeneralCfg.Dbdata_encoding } @@ -775,9 +783,6 @@ func (self *CGRConfig) loadFromJsonCfg(jsnCfg *CgrJsonCfg) error { return err } } - if jsnGeneralCfg.Log_level != nil { - self.LogLevel = *jsnGeneralCfg.Log_level - } } if jsnCacheCfg != nil { diff --git a/config/config_defaults.go b/config/config_defaults.go index 1c1ff41da..0ce7301c9 100755 --- a/config/config_defaults.go +++ b/config/config_defaults.go @@ -29,6 +29,7 @@ const CGRATES_CFG_JSON = ` "general": { "instance_id": "", // identifier of this instance in the cluster, if empty it will be autogenerated + "logger":"*syslog", // controls the destination of logs <*syslog|*stdout> "log_level": 6, // control the level of messages logged (0-emerg to 7-debug) "http_skip_tls_verify": false, // if enabled Http Client will accept any TLS certificate "rounding_decimals": 5, // system level precision for floats diff --git a/config/config_json_test.go b/config/config_json_test.go index 62b45f12e..d266407cd 100755 --- a/config/config_json_test.go +++ b/config/config_json_test.go @@ -39,6 +39,7 @@ func TestDfNewdfCgrJsonCfgFromReader(t *testing.T) { func TestDfGeneralJsonCfg(t *testing.T) { eCfg := &GeneralJsonCfg{ Instance_id: utils.StringPointer(""), + SysLogger: utils.StringPointer(utils.LoggerSysLog) Log_level: utils.IntPointer(utils.LOGLEVEL_INFO), Http_skip_tls_verify: utils.BoolPointer(false), Rounding_decimals: utils.IntPointer(5), diff --git a/config/config_test.go b/config/config_test.go index aebec0614..a1a7a662b 100755 --- a/config/config_test.go +++ b/config/config_test.go @@ -193,6 +193,9 @@ func TestCgrCfgJSONDefaultsGeneral(t *testing.T) { if cgrCfg.LockingTimeout != 5*time.Second { t.Error(cgrCfg.LockingTimeout) } + if cgrCfg.Logger != utils.MetaSysLog { + t.Error(cgrCfg.Logger) + } if cgrCfg.LogLevel != 6 { t.Error(cgrCfg.LogLevel) } diff --git a/config/libconfig_json.go b/config/libconfig_json.go index 9773ac749..2f6ae15a6 100755 --- a/config/libconfig_json.go +++ b/config/libconfig_json.go @@ -21,6 +21,7 @@ package config // General config section type GeneralJsonCfg struct { Instance_id *string + Logger *string Log_level *int Http_skip_tls_verify *bool Rounding_decimals *int diff --git a/utils/consts.go b/utils/consts.go index 691d7ae87..0af5de2d1 100755 --- a/utils/consts.go +++ b/utils/consts.go @@ -445,6 +445,8 @@ const ( CacheEventQueues = "event_queues" CacheEventResources = "event_resources" EventResourcesPrefix = "ers_" + MetaSysLog = "*syslog" + MetaStdLog = "*stdout" ) func buildCacheInstRevPrefixes() { diff --git a/utils/logger.go b/utils/logger.go index 63601c68e..0c45a784f 100644 --- a/utils/logger.go +++ b/utils/logger.go @@ -22,25 +22,38 @@ import ( "fmt" "log" "log/syslog" + "reflect" "runtime" ) var Logger LoggerInterface func init() { - Logger = new(StdLogger) - - // Attempt to connect to syslog. We'll fallback to `log` otherwise. - var err error - var l *syslog.Writer - l, err = syslog.New(syslog.LOG_INFO, "CGRateS ") - if err != nil { - Logger.Err(fmt.Sprintf("Could not connect to syslog: %v", err)) - } else { - Logger.SetSyslog(l) + if Logger == nil || reflect.ValueOf(Logger).IsNil() { + err := Newlogger(MetaSysLog) + if err != nil { + Logger.Err(fmt.Sprintf("Could not connect to syslog: %v", err)) + } } } +//functie Newlogger (logger type) +func Newlogger(loggertype string) (err error) { + Logger = new(StdLogger) + var l *syslog.Writer + if loggertype == MetaSysLog { + if l, err = syslog.New(syslog.LOG_INFO, "CGRateS"); err != nil { + return err + } else { + Logger.SetSyslog(l) + } + return nil + } else if loggertype != MetaStdLog { + return fmt.Errorf("unsuported logger: <%s>", loggertype) + } + return nil +} + type LoggerInterface interface { SetSyslog(log *syslog.Writer) SetLogLevel(level int) From 170e0e039a3c2b2db1234f2eefc0d1d552621f27 Mon Sep 17 00:00:00 2001 From: edwardro22 Date: Mon, 18 Sep 2017 15:49:51 +0000 Subject: [PATCH 2/3] small fix --- config/config_json_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/config/config_json_test.go b/config/config_json_test.go index d266407cd..52c479dd1 100755 --- a/config/config_json_test.go +++ b/config/config_json_test.go @@ -39,7 +39,7 @@ func TestDfNewdfCgrJsonCfgFromReader(t *testing.T) { func TestDfGeneralJsonCfg(t *testing.T) { eCfg := &GeneralJsonCfg{ Instance_id: utils.StringPointer(""), - SysLogger: utils.StringPointer(utils.LoggerSysLog) + Logger: utils.StringPointer(utils.LoggerSysLog) Log_level: utils.IntPointer(utils.LOGLEVEL_INFO), Http_skip_tls_verify: utils.BoolPointer(false), Rounding_decimals: utils.IntPointer(5), From 38ebae73d22025617d66f05599af3d396bf20878 Mon Sep 17 00:00:00 2001 From: edwardro22 Date: Mon, 18 Sep 2017 16:18:40 +0000 Subject: [PATCH 3/3] Small fix --- config/config_json_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/config/config_json_test.go b/config/config_json_test.go index 52c479dd1..ce60eca24 100755 --- a/config/config_json_test.go +++ b/config/config_json_test.go @@ -39,7 +39,7 @@ func TestDfNewdfCgrJsonCfgFromReader(t *testing.T) { func TestDfGeneralJsonCfg(t *testing.T) { eCfg := &GeneralJsonCfg{ Instance_id: utils.StringPointer(""), - Logger: utils.StringPointer(utils.LoggerSysLog) + Logger: utils.StringPointer(utils.MetaSysLog), Log_level: utils.IntPointer(utils.LOGLEVEL_INFO), Http_skip_tls_verify: utils.BoolPointer(false), Rounding_decimals: utils.IntPointer(5),