Merge pull request #742 from Edwardro22/master

Added stdout as a log option, fixes #713
This commit is contained in:
Dan Christian Bogos
2017-09-18 17:55:47 +02:00
committed by GitHub
8 changed files with 47 additions and 19 deletions

View File

@@ -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
}

View File

@@ -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 {

View File

@@ -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

View File

@@ -39,6 +39,7 @@ func TestDfNewdfCgrJsonCfgFromReader(t *testing.T) {
func TestDfGeneralJsonCfg(t *testing.T) {
eCfg := &GeneralJsonCfg{
Instance_id: utils.StringPointer(""),
Logger: utils.StringPointer(utils.LoggerSysLog)
Log_level: utils.IntPointer(utils.LOGLEVEL_INFO),
Http_skip_tls_verify: utils.BoolPointer(false),
Rounding_decimals: utils.IntPointer(5),

View File

@@ -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)
}

View File

@@ -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

View File

@@ -445,6 +445,8 @@ const (
CacheEventQueues = "event_queues"
CacheEventResources = "event_resources"
EventResourcesPrefix = "ers_"
MetaSysLog = "*syslog"
MetaStdLog = "*stdout"
)
func buildCacheInstRevPrefixes() {

View File

@@ -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)