@@ -14,8 +14,10 @@ import (
1414 "sync"
1515 "time"
1616
17- "github.com/gwos/tcg/log "
17+ "github.com/gwos/tcg/logger "
1818 "github.com/kelseyhightower/envconfig"
19+ "github.com/rs/zerolog"
20+ "github.com/rs/zerolog/log"
1921 "go.opentelemetry.io/otel/attribute"
2022 "go.opentelemetry.io/otel/exporters/trace/jaeger"
2123 "go.opentelemetry.io/otel/sdk/resource"
@@ -67,7 +69,7 @@ const (
6769 InstallationModeS = "STANDALONE"
6870)
6971
70- // LogLevel defines levels for logrus
72+ // LogLevel defines levels in logrus-style
7173type LogLevel int
7274
7375// Enum levels
@@ -117,6 +119,8 @@ type Connector struct {
117119 // If count is 0, old versions are removed rather than rotated.
118120 LogFileRotate int `yaml:"logFileRotate"`
119121 LogLevel LogLevel `yaml:"logLevel"`
122+ LogNoColor bool `yaml:"logNoColor"`
123+ LogTimeFormat string `yaml:"logTimeFormat"`
120124
121125 // NatsAckWait is the time the NATS server will wait before resending a message
122126 // Should be greater then the GWClient request duration
@@ -335,15 +339,17 @@ func defaults() Config {
335339 return Config {
336340 Connector : & Connector {
337341 ControllerAddr : ":8099" ,
338- ControllerReadTimeout : time .Duration ( time . Second * 10 ) ,
339- ControllerWriteTimeout : time .Duration ( time . Second * 20 ) ,
340- ControllerStartTimeout : time .Duration ( time . Second * 4 ) ,
341- ControllerStopTimeout : time .Duration ( time . Second * 4 ) ,
342+ ControllerReadTimeout : time .Second * 10 ,
343+ ControllerWriteTimeout : time .Second * 20 ,
344+ ControllerStartTimeout : time .Second * 4 ,
345+ ControllerStopTimeout : time .Second * 4 ,
342346 LogCondense : 0 ,
343347 LogFileMaxSize : 1024 * 1024 * 10 , // 10MB
344348 LogFileRotate : 5 ,
345349 LogLevel : 1 ,
346- NatsAckWait : time .Duration (time .Second * 30 ),
350+ LogNoColor : false ,
351+ LogTimeFormat : time .RFC3339 ,
352+ NatsAckWait : time .Second * 30 ,
347353 NatsMaxInflight : 1024 ,
348354 NatsMaxPubAcksInflight : 1024 ,
349355 NatsMaxPayload : 1024 * 1024 * 80 , // 80MB
@@ -352,10 +358,10 @@ func defaults() Config {
352358 NatsMonitorPort : 0 ,
353359 NatsStoreDir : "natsstore" ,
354360 NatsStoreType : "FILE" ,
355- NatsStoreMaxAge : time .Duration ( time . Hour * 24 * 10 ), // 10days
356- NatsStoreMaxBytes : 1024 * 1024 * 1024 * 50 , // 50GB
357- NatsStoreBufferSize : 1024 * 1024 * 2 , // 2MB
358- NatsStoreReadBufferSize : 1024 * 1024 * 2 , // 2MB
361+ NatsStoreMaxAge : time .Hour * 24 * 10 , // 10days
362+ NatsStoreMaxBytes : 1024 * 1024 * 1024 * 50 , // 50GB
363+ NatsStoreBufferSize : 1024 * 1024 * 2 , // 2MB
364+ NatsStoreReadBufferSize : 1024 * 1024 * 2 , // 2MB
359365 },
360366 DSConnection : & DSConnection {},
361367 Jaegertracing : & Jaegertracing {},
@@ -365,54 +371,54 @@ func defaults() Config {
365371// GetConfig implements Singleton pattern
366372func GetConfig () * Config {
367373 once .Do (func () {
368- logBuf := make (map [string ]interface {}, 3 )
374+ /* buffer the logging while configuring */
375+ logBuf := & logger.LogBuffer {
376+ Level : zerolog .TraceLevel ,
377+ Size : 16 ,
378+ }
379+ log .Logger = zerolog .New (logBuf ).
380+ With ().Timestamp ().Caller ().Logger ()
381+ log .Info ().Msgf ("Build info: %s / %s" , buildTag , buildTime )
382+
369383 c := defaults ()
370384 cfg = & c
371-
372385 if data , err := ioutil .ReadFile (cfg .configPath ()); err != nil {
373- logBuf ["ioutil.ReadFile" ] = err
386+ log .Warn ().Err (err ).
387+ Str ("configPath" , cfg .configPath ()).
388+ Msg ("could not read config" )
374389 } else {
375390 if err := yaml .Unmarshal (data , cfg ); err != nil {
376- logBuf ["yaml.Unmarshal" ] = err
391+ log .Err (err ).
392+ Str ("configPath" , cfg .configPath ()).
393+ Msg ("could not parse config" )
377394 }
378395 }
379-
380396 if err := envconfig .Process (EnvConfigPrefix , cfg ); err != nil {
381- logBuf ["envconfig.Process" ] = err
382- }
383-
384- log .Config (
385- cfg .Connector .LogFile ,
386- cfg .Connector .LogFileMaxSize ,
387- cfg .Connector .LogFileRotate ,
388- int (cfg .Connector .LogLevel ),
389- cfg .Connector .LogCondense ,
390- )
391- log .Info (fmt .Sprintf ("Build info: %s / %s" , buildTag , buildTime ))
392- if len (logBuf ) > 0 {
393- log .With (logBuf ).Warn ()
397+ log .Err (err ).
398+ Str ("EnvConfigPrefix" , EnvConfigPrefix ).
399+ Msg ("could not process config environment" )
394400 }
401+ cfg .initLogger ()
402+ logger .WriteLogBuffer (logBuf )
395403 })
396404 return cfg
397405}
398406
399407func (cfg Config ) configPath () string {
400408 configPath := os .Getenv (string (ConfigEnv ))
401409 if configPath == "" {
402- wd , err := os .Getwd ()
403- if err != nil {
404- log .Warn (err )
405- wd = ""
410+ configPath = ConfigName
411+ if wd , err := os .Getwd (); err == nil {
412+ configPath = path .Join (wd , ConfigName )
406413 }
407- configPath = path .Join (wd , ConfigName )
408414 }
409415 return configPath
410416}
411417
412418func (cfg * Config ) loadConnector (data []byte ) (* ConnectorDTO , error ) {
413419 var dto ConnectorDTO
414420 if err := json .Unmarshal (data , & dto ); err != nil {
415- log .Error ( "|config.go| : [loadConnector] : " , err . Error () )
421+ log .Err ( err ). Msg ( "could not parse connector" )
416422 return nil , err
417423 }
418424 cfg .Connector .AgentID = dto .AgentID
@@ -437,7 +443,7 @@ func (cfg *Config) loadAdvancedPrefixes(data []byte) error {
437443 } `json:"advanced,omitempty"`
438444 }
439445 if err := json .Unmarshal (data , & s ); err != nil {
440- log .Error ( "|config.go| : [loadAdvancedPrefixes] : " , err . Error () )
446+ log .Err ( err ). Msg ( "could not parse advanced" )
441447 return err
442448 }
443449 for _ , c := range cfg .GWConnections {
@@ -484,10 +490,14 @@ func (cfg *Config) LoadConnectorDTO(data []byte) (*ConnectorDTO, error) {
484490 newCfg := & c
485491 /* load config file */
486492 if data , err := ioutil .ReadFile (newCfg .configPath ()); err != nil {
487- log .Warn (err )
493+ log .Warn ().Err (err ).
494+ Str ("configPath" , cfg .configPath ()).
495+ Msg ("could not read config" )
488496 } else {
489497 if err := yaml .Unmarshal (data , newCfg ); err != nil {
490- log .Warn (err )
498+ log .Warn ().Err (err ).
499+ Str ("configPath" , newCfg .configPath ()).
500+ Msg ("could not parse config" )
491501 }
492502 }
493503 /* load as ConnectorDTO */
@@ -505,15 +515,20 @@ func (cfg *Config) LoadConnectorDTO(data []byte) (*ConnectorDTO, error) {
505515 }
506516 /* override config file */
507517 if output , err := yaml .Marshal (newCfg ); err != nil {
508- log .Warn (err )
518+ log .Err (err ).
519+ Msg ("could not prepare config for writing" )
509520 } else {
510521 if err := ioutil .WriteFile (newCfg .configPath (), output , 0644 ); err != nil {
511- log .Warn (err )
522+ log .Err (err ).
523+ Str ("configPath" , newCfg .configPath ()).
524+ Msg ("could not write config" )
512525 }
513526 }
514527 /* load environment */
515528 if err := envconfig .Process (EnvConfigPrefix , newCfg ); err != nil {
516- log .Warn (err )
529+ log .Err (err ).
530+ Str ("EnvConfigPrefix" , EnvConfigPrefix ).
531+ Msg ("could not process config environment" )
517532 }
518533 /* process PMC */
519534 if cfg .IsConfiguringPMC () {
@@ -526,13 +541,7 @@ func (cfg *Config) LoadConnectorDTO(data []byte) (*ConnectorDTO, error) {
526541 cfg .GWConnections = newCfg .GWConnections
527542
528543 /* update logger */
529- log .Config (
530- cfg .Connector .LogFile ,
531- cfg .Connector .LogFileMaxSize ,
532- cfg .Connector .LogFileRotate ,
533- int (cfg .Connector .LogLevel ),
534- cfg .Connector .LogCondense ,
535- )
544+ cfg .initLogger ()
536545
537546 return dto , nil
538547}
@@ -593,13 +602,13 @@ func (cfg Config) initJaegertracing() (*sdktrace.TracerProvider, error) {
593602 jaeger .WithAgentPort (port ),
594603 )
595604 } else {
596- log .Warn (err )
605+ log .Err (err ). Msg ( "could not parse the JaegerAgent" )
597606 return nil , err
598607 }
599608 case len (tcgJaegerCollector ) != 0 :
600609 endpointOption = jaeger .WithCollectorEndpoint (jaeger .WithEndpoint (tcgJaegerCollector ))
601610 default :
602- log .Debug (errNotConfigured )
611+ log .Debug (). Msg ( errNotConfigured . Error () )
603612 return nil , errNotConfigured
604613 }
605614
@@ -614,7 +623,7 @@ func (cfg Config) initJaegertracing() (*sdktrace.TracerProvider, error) {
614623
615624 exporter , err := jaeger .NewRawExporter (endpointOption )
616625 if err != nil {
617- log .Warn (err )
626+ log .Err (err ). Msg ( "could not create exporter" )
618627 return nil , err
619628 }
620629 tp := sdktrace .NewTracerProvider (
@@ -626,6 +635,24 @@ func (cfg Config) initJaegertracing() (*sdktrace.TracerProvider, error) {
626635 return tp , nil
627636}
628637
638+ func (cfg Config ) initLogger () {
639+ opts := []logger.Option {
640+ logger .WithCondense (cfg .Connector .LogCondense ),
641+ logger .WithLastErrors (10 ),
642+ logger .WithLevel ([... ]zerolog.Level {3 , 2 , 1 , 0 }[cfg .Connector .LogLevel ]),
643+ logger .WithNoColor (cfg .Connector .LogNoColor ),
644+ logger .WithTimeFormat (cfg .Connector .LogTimeFormat ),
645+ }
646+ if cfg .Connector .LogFile != "" {
647+ opts = append (opts , logger .WithLogFile (& logger.LogFile {
648+ FilePath : cfg .Connector .LogFile ,
649+ MaxSize : cfg .Connector .LogFileMaxSize ,
650+ Rotate : cfg .Connector .LogFileRotate ,
651+ }))
652+ }
653+ logger .SetLogger (opts ... )
654+ }
655+
629656// Decrypt decrypts small messages
630657// golang.org/x/crypto/nacl/secretbox
631658func Decrypt (message , secret []byte ) ([]byte , error ) {
0 commit comments