diff --git a/pkg/factory/commonFactory.go b/pkg/factory/commonFactory.go index c5bc7b0..e5de84e 100644 --- a/pkg/factory/commonFactory.go +++ b/pkg/factory/commonFactory.go @@ -19,17 +19,14 @@ import ( "errors" "flag" "fmt" - - "os" - "path/filepath" - "reflect" - "strings" + "github.com/rs/zerolog" + "github.com/th2-net/th2-common-go/pkg/log" "github.com/magiconair/properties" - "github.com/rs/zerolog" - "github.com/rs/zerolog/log" "github.com/th2-net/th2-common-go/pkg/common" "github.com/th2-net/th2-common-go/pkg/modules/prometheus" + "path/filepath" + "reflect" ) const ( @@ -50,44 +47,6 @@ type commonFactory struct { boxConfig common.BoxConfig } -type ZerologConfig struct { - Level string `properties:"global_level,default=info"` - Sampling bool `properties:"disable_sampling,default=false"` - TimeField string `properties:"time_field,default=time"` - TimeFormat string `properties:"time_format, default=2006-01-02 15:04:05.000"` - LevelField string `properties:"level_field, default=level"` - MsgField string `properties:"message_field, default=message"` - ErrorField string `properties:"error_field, default=error"` -} - -func configureZerolog(cfg *ZerologConfig) { - switch level := strings.ToLower(cfg.Level); level { - case "trace": - zerolog.SetGlobalLevel(zerolog.TraceLevel) - case "debug": - zerolog.SetGlobalLevel(zerolog.DebugLevel) - case "info": - zerolog.SetGlobalLevel(zerolog.InfoLevel) - case "warn": - zerolog.SetGlobalLevel(zerolog.WarnLevel) - case "error": - zerolog.SetGlobalLevel(zerolog.ErrorLevel) - case "fatal": - zerolog.SetGlobalLevel(zerolog.FatalLevel) - default: - zerolog.SetGlobalLevel(zerolog.InfoLevel) - log.Warn().Msgf("'%s' log level is unknown. 'INFO' log level is used instead", level) - } - - zerolog.TimeFieldFormat = cfg.TimeFormat - zerolog.TimestampFieldName = cfg.TimeField - zerolog.LevelFieldName = cfg.LevelField - zerolog.MessageFieldName = cfg.MsgField - zerolog.ErrorFieldName = cfg.ErrorField - zerolog.DisableSampling(cfg.Sampling) - -} - func New() common.Factory { configPath := flag.String("config-file-path", configurationPath, "pass path to config files") extension := flag.String("config-file-extension", jsonExtension, "file extension") @@ -111,20 +70,18 @@ func NewFromConfig(config Config) (common.Factory, error) { } loadZeroLogConfig(config) - logger := zerolog.New(os.Stdout).With().Timestamp().Logger() provider := NewFileProvider( config.ConfigurationsDir, config.FileExtension, - logger.With().Str(common.ComponentLoggerKey, "file_provider").Logger(), + log.ForComponent("file_provider"), ) var boxConfig common.BoxConfig if err := provider.GetConfig("box", &boxConfig); err != nil { - log.Warn().Err(err).Msg("cannot read box configuration") + log.Global().Warn().Err(err).Msg("cannot read box configuration") } cf := &commonFactory{ modules: make(map[common.ModuleKey]common.Module), cfgProvider: provider, - zLogger: logger, boxConfig: boxConfig, } err := cf.Register(prometheus.NewModule) @@ -136,19 +93,19 @@ func NewFromConfig(config Config) (common.Factory, error) { } func loadZeroLogConfig(config Config) { - var cfg ZerologConfig + var cfg log.ZerologConfig p, pErr := properties.LoadFile(filepath.Join(config.ConfigurationsDir, "zerolog.properties"), properties.UTF8) if pErr != nil { - log.Error().Err(pErr).Msg("Can't get properties for zerolog") + log.Global().Debug().Err(pErr).Msg("Can't get properties for zerolog") return } if err := p.Decode(&cfg); err != nil { - log.Error().Err(pErr).Msg("Can't decode properties into zerolog configuration structure") + log.Global().Error().Err(pErr).Msg("Can't decode properties into zerolog configuration structure") return } - log.Info().Msg("Loggers will be configured via zerolog.properties file") + log.Global().Info().Msg("Loggers will be configured via zerolog.properties file") - configureZerolog(&cfg) + log.ConfigureZerolog(&cfg) } func (cf *commonFactory) Register(factories ...func(common.ConfigProvider) (common.Module, error)) error { diff --git a/pkg/log/logger.go b/pkg/log/logger.go new file mode 100644 index 0000000..f603d27 --- /dev/null +++ b/pkg/log/logger.go @@ -0,0 +1,73 @@ +/* + * Copyright 2023 Exactpro (Exactpro Systems Limited) + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package log + +import ( + "github.com/rs/zerolog" + "github.com/rs/zerolog/log" + "github.com/th2-net/th2-common-go/pkg/common" + "os" + "strings" +) + +var logger zerolog.Logger = zerolog.New(os.Stdout). + Level(zerolog.InfoLevel). + With().Timestamp().Logger() + +type ZerologConfig struct { + Level string `properties:"global_level,default=info"` + Sampling bool `properties:"disable_sampling,default=false"` + TimeField string `properties:"time_field,default=time"` + TimeFormat string `properties:"time_format, default=2006-01-02 15:04:05.000"` + LevelField string `properties:"level_field, default=level"` + MsgField string `properties:"message_field, default=message"` + ErrorField string `properties:"error_field, default=error"` +} + +func ConfigureZerolog(cfg *ZerologConfig) { + switch level := strings.ToLower(cfg.Level); level { + case "trace": + log.Level(zerolog.TraceLevel) + case "debug": + log.Level(zerolog.DebugLevel) + case "info": + log.Level(zerolog.InfoLevel) + case "warn": + log.Level(zerolog.WarnLevel) + case "error": + log.Level(zerolog.ErrorLevel) + case "fatal": + log.Level(zerolog.FatalLevel) + default: + log.Level(zerolog.InfoLevel) + log.Warn().Msgf("'%s' log level is unknown. 'INFO' log level is used instead", level) + } + + zerolog.TimeFieldFormat = cfg.TimeFormat + zerolog.TimestampFieldName = cfg.TimeField + zerolog.LevelFieldName = cfg.LevelField + zerolog.MessageFieldName = cfg.MsgField + zerolog.ErrorFieldName = cfg.ErrorField + zerolog.DisableSampling(cfg.Sampling) +} + +func ForComponent(name string) zerolog.Logger { + return logger.With().Str(common.ComponentLoggerKey, name).Logger() +} + +func Global() *zerolog.Logger { + return &logger +} diff --git a/pkg/logger/zerolog.properties b/pkg/log/zerolog.properties similarity index 100% rename from pkg/logger/zerolog.properties rename to pkg/log/zerolog.properties diff --git a/pkg/modules/grpc/module.go b/pkg/modules/grpc/module.go index cf91f50..a6efc4b 100644 --- a/pkg/modules/grpc/module.go +++ b/pkg/modules/grpc/module.go @@ -18,10 +18,9 @@ package grpc import ( "fmt" "github.com/th2-net/th2-common-go/pkg/grpc" - "os" + "github.com/th2-net/th2-common-go/pkg/log" "reflect" - "github.com/rs/zerolog" "github.com/th2-net/th2-common-go/pkg/common" ) @@ -55,7 +54,7 @@ var grpcModuleKey = common.ModuleKey(moduleKey) func NewModule(provider common.ConfigProvider) (common.Module, error) { - grpcConfiguration := grpc.Config{ZLogger: zerolog.New(os.Stdout).With().Timestamp().Logger()} + grpcConfiguration := grpc.Config{ZLogger: log.ForComponent("grpc_config")} err := provider.GetConfig(configFilename, &grpcConfiguration) if err != nil { return nil, err @@ -66,7 +65,7 @@ func NewModule(provider common.ConfigProvider) (common.Module, error) { func New(config grpc.Config) (Module, error) { router := grpc.NewRouter( config, - zerolog.New(os.Stdout).With().Timestamp().Logger(), + log.ForComponent("grpc_router"), ) return &impl{router: router}, nil } diff --git a/pkg/modules/prometheus/module.go b/pkg/modules/prometheus/module.go index 9ab3e71..20323e5 100644 --- a/pkg/modules/prometheus/module.go +++ b/pkg/modules/prometheus/module.go @@ -18,9 +18,8 @@ package prometheus import ( "errors" "fmt" - "github.com/rs/zerolog" + "github.com/th2-net/th2-common-go/pkg/log" "github.com/th2-net/th2-common-go/pkg/metrics/prometheus" - "os" "reflect" "github.com/th2-net/th2-common-go/pkg/common" @@ -66,9 +65,8 @@ func (p *module) Close() error { func NewModule(provider common.ConfigProvider) (common.Module, error) { promConfig := prometheus.Configuration{Host: "0.0.0.0", Port: 9752} if err := provider.GetConfig(configFileName, &promConfig); err != nil { - logger := zerolog.New(os.Stdout).With().Timestamp().Logger() + logger := log.ForComponent("prometheus") logger.Warn(). - Str(common.ComponentLoggerKey, "prometheus"). Err(err). Msg("cannot read config. create with default parameters") } diff --git a/pkg/queue/filter/default.go b/pkg/queue/filter/default.go index 9c6d05d..0857a35 100644 --- a/pkg/queue/filter/default.go +++ b/pkg/queue/filter/default.go @@ -19,8 +19,8 @@ import ( "github.com/IGLOU-EU/go-wildcard" "github.com/rs/zerolog" p_buff "github.com/th2-net/th2-common-go/pkg/common/grpc/th2_grpc_common" + "github.com/th2-net/th2-common-go/pkg/log" mqFilter "github.com/th2-net/th2-common-go/pkg/queue" - "os" ) type defaultFilterStrategy struct { @@ -29,7 +29,7 @@ type defaultFilterStrategy struct { logger zerolog.Logger } -var Default Strategy = defaultFilterStrategy{logger: zerolog.New(os.Stdout).With().Str("component", "default_filter_strategy").Timestamp().Logger()} +var Default Strategy = defaultFilterStrategy{logger: log.ForComponent("default_filter_strategy")} func (dfs defaultFilterStrategy) Verify(messages *p_buff.MessageGroupBatch, filters []mqFilter.FilterConfiguration) bool { // returns true if MessageGroupBatch entirely matches at least one filter(any) from list of filters in the queueConfig, diff --git a/pkg/queue/rabbitmq/factory.go b/pkg/queue/rabbitmq/factory.go index c207f8f..5ceddba 100644 --- a/pkg/queue/rabbitmq/factory.go +++ b/pkg/queue/rabbitmq/factory.go @@ -17,7 +17,7 @@ package rabbitmq import ( "github.com/rs/zerolog" - "github.com/th2-net/th2-common-go/pkg/common" + "github.com/th2-net/th2-common-go/pkg/log" "github.com/th2-net/th2-common-go/pkg/queue" "github.com/th2-net/th2-common-go/pkg/queue/event" "github.com/th2-net/th2-common-go/pkg/queue/message" @@ -26,25 +26,18 @@ import ( eventImpl "github.com/th2-net/th2-common-go/pkg/queue/rabbitmq/internal/event" messageImpl "github.com/th2-net/th2-common-go/pkg/queue/rabbitmq/internal/message" "io" - "os" ) func NewRouters( connection connection.Config, config *queue.RouterConfig, ) (messageRouter message.Router, eventRouter event.Router, closer io.Closer, err error) { - manager, err := internal.NewConnectionManager(connection, zerolog.New(os.Stdout).With(). - Timestamp(). - Str(common.ComponentLoggerKey, "connection_manager").Logger()) + manager, err := internal.NewConnectionManager(connection, log.ForComponent("connection_manager")) if err != nil { return } - messageRouter = newMessageRouter(&manager, config, zerolog.New(os.Stdout).With(). - Timestamp(). - Str(common.ComponentLoggerKey, "message_router").Logger()) - eventRouter = newEventRouter(&manager, config, zerolog.New(os.Stdout).With(). - Timestamp(). - Str(common.ComponentLoggerKey, "event_router").Logger()) + messageRouter = newMessageRouter(&manager, config, log.ForComponent("message_router")) + eventRouter = newEventRouter(&manager, config, log.ForComponent("event_router")) closer = &manager return } diff --git a/pkg/queue/rabbitmq/internal/connection/connectionManager.go b/pkg/queue/rabbitmq/internal/connection/connectionManager.go index 6d99ca0..6cc170b 100644 --- a/pkg/queue/rabbitmq/internal/connection/connectionManager.go +++ b/pkg/queue/rabbitmq/internal/connection/connectionManager.go @@ -18,8 +18,8 @@ package connection import ( "fmt" "github.com/rs/zerolog" + "github.com/th2-net/th2-common-go/pkg/log" "github.com/th2-net/th2-common-go/pkg/queue/rabbitmq/connection" - "os" ) type Manager struct { @@ -36,11 +36,11 @@ func NewConnectionManager(connConfiguration connection.Config, logger zerolog.Lo connConfiguration.Host, connConfiguration.Port, connConfiguration.VHost) - publisher, err := NewPublisher(url, zerolog.New(os.Stdout).With().Str("component", "publisher").Timestamp().Logger()) + publisher, err := NewPublisher(url, log.ForComponent("publisher")) if err != nil { return Manager{}, err } - consumer, err := NewConsumer(url, zerolog.New(os.Stdout).With().Str("component", "consumer").Timestamp().Logger()) + consumer, err := NewConsumer(url, log.ForComponent("consumer")) if err != nil { if pubErr := publisher.Close(); pubErr != nil { logger.Err(pubErr). diff --git a/pkg/queue/rabbitmq/internal/event/router.go b/pkg/queue/rabbitmq/internal/event/router.go index d7e2ad6..3c5505b 100644 --- a/pkg/queue/rabbitmq/internal/event/router.go +++ b/pkg/queue/rabbitmq/internal/event/router.go @@ -18,15 +18,14 @@ package event import ( "errors" "fmt" + "github.com/rs/zerolog" p_buff "github.com/th2-net/th2-common-go/pkg/common/grpc/th2_grpc_common" + "github.com/th2-net/th2-common-go/pkg/log" "github.com/th2-net/th2-common-go/pkg/queue" - "github.com/th2-net/th2-common-go/pkg/queue/rabbitmq/internal" - "github.com/th2-net/th2-common-go/pkg/queue/rabbitmq/internal/connection" - "os" - - "github.com/rs/zerolog" "github.com/th2-net/th2-common-go/pkg/queue/common" "github.com/th2-net/th2-common-go/pkg/queue/event" + "github.com/th2-net/th2-common-go/pkg/queue/rabbitmq/internal" + "github.com/th2-net/th2-common-go/pkg/queue/rabbitmq/internal/connection" ) type CommonEventRouter struct { @@ -180,7 +179,7 @@ func (cer *CommonEventRouter) getSender(pin string) *CommonEventSender { return result } result = &CommonEventSender{ConnManager: cer.connManager, exchangeName: queueConfig.Exchange, - sendQueue: queueConfig.RoutingKey, th2Pin: pin, Logger: zerolog.New(os.Stdout).With().Timestamp().Logger()} + sendQueue: queueConfig.RoutingKey, th2Pin: pin, Logger: log.ForComponent("event_sender")} cer.senders[pin] = result cer.Logger.Trace().Str("Pin", pin).Msg("Created sender") return result diff --git a/pkg/queue/rabbitmq/internal/event/subscriber.go b/pkg/queue/rabbitmq/internal/event/subscriber.go index ebf2712..28ee672 100644 --- a/pkg/queue/rabbitmq/internal/event/subscriber.go +++ b/pkg/queue/rabbitmq/internal/event/subscriber.go @@ -18,16 +18,14 @@ package event import ( "errors" "fmt" - "github.com/th2-net/th2-common-go/pkg/common" + "github.com/prometheus/client_golang/prometheus" + "github.com/prometheus/client_golang/prometheus/promauto" + "github.com/rs/zerolog" p_buff "github.com/th2-net/th2-common-go/pkg/common/grpc/th2_grpc_common" + "github.com/th2-net/th2-common-go/pkg/log" "github.com/th2-net/th2-common-go/pkg/queue" "github.com/th2-net/th2-common-go/pkg/queue/rabbitmq/internal" "github.com/th2-net/th2-common-go/pkg/queue/rabbitmq/internal/connection" - "os" - - "github.com/prometheus/client_golang/prometheus" - "github.com/prometheus/client_golang/prometheus/promauto" - "github.com/rs/zerolog" "github.com/streadway/amqp" "github.com/th2-net/th2-common-go/pkg/metrics" @@ -53,10 +51,7 @@ func newSubscriber( pinName string, subscriberType internal.SubscriberType, ) (internal.Subscriber, error) { - logger := zerolog.New(os.Stdout).With(). - Str(common.ComponentLoggerKey, "rabbitmq_event_subscriber"). - Timestamp(). - Logger() + logger := log.ForComponent("rabbitmq_event_subscriber") baseHandler := baseEventHandler{&logger, pinName} switch subscriberType { case internal.AutoSubscriberType: @@ -163,7 +158,7 @@ func (cs *confirmationEventHandler) Handle(msgDelivery amqp.Delivery, timer *pro } th2EventSubscribeTotal.WithLabelValues(cs.th2Pin).Add(float64(len(result.Events))) delivery := queue.Delivery{Redelivered: msgDelivery.Redelivered} - deliveryConfirm := internal.DeliveryConfirmation{Delivery: &msgDelivery, Logger: zerolog.New(os.Stdout).With().Timestamp().Logger(), Timer: timer} + deliveryConfirm := internal.DeliveryConfirmation{Delivery: &msgDelivery, Logger: log.ForComponent("confirmation"), Timer: timer} var confirmation queue.Confirmation = &deliveryConfirm handleErr := listener.Handle(delivery, result, confirmation) diff --git a/pkg/queue/rabbitmq/internal/message/router.go b/pkg/queue/rabbitmq/internal/message/router.go index 51204c9..cdaae8f 100644 --- a/pkg/queue/rabbitmq/internal/message/router.go +++ b/pkg/queue/rabbitmq/internal/message/router.go @@ -19,16 +19,14 @@ import ( "errors" "fmt" "github.com/rs/zerolog" - "github.com/rs/zerolog/log" p_buff "github.com/th2-net/th2-common-go/pkg/common/grpc/th2_grpc_common" + "github.com/th2-net/th2-common-go/pkg/log" "github.com/th2-net/th2-common-go/pkg/queue" + "github.com/th2-net/th2-common-go/pkg/queue/common" "github.com/th2-net/th2-common-go/pkg/queue/filter" + "github.com/th2-net/th2-common-go/pkg/queue/message" "github.com/th2-net/th2-common-go/pkg/queue/rabbitmq/internal" "github.com/th2-net/th2-common-go/pkg/queue/rabbitmq/internal/connection" - "os" - - "github.com/th2-net/th2-common-go/pkg/queue/common" - "github.com/th2-net/th2-common-go/pkg/queue/message" ) type CommonMessageRouter struct { @@ -69,7 +67,7 @@ func (cmr *CommonMessageRouter) SendAll(msgBatch *p_buff.MessageGroupBatch, attr } for pin, config := range pinsFoundByAttrs { if !cmr.filterStrategy.Verify(msgBatch, config.Filters) { - if e := log.Debug(); e.Enabled() { + if e := cmr.Logger.Debug(); e.Enabled() { e.Str("Pin", pin). Interface("Metadata", filter.FirstIDFromMsgBatch(msgBatch)). Msg("First ID of message batch didn't match filter") @@ -87,7 +85,7 @@ func (cmr *CommonMessageRouter) SendAll(msgBatch *p_buff.MessageGroupBatch, attr cmr.Logger.Error().Err(err).Send() return err } - if e := log.Debug(); e.Enabled() { + if e := cmr.Logger.Debug(); e.Enabled() { e.Str("sending to pin", pin). Interface("Metadata", filter.FirstIDFromMsgBatch(msgBatch)). Msg("First ID of sent Message batch") @@ -272,7 +270,7 @@ func (cmr *CommonMessageRouter) getSender(pin string) *CommonMessageSender { return result } result = &CommonMessageSender{ConnManager: cmr.connManager, exchangeName: queueConfig.Exchange, - sendQueue: queueConfig.RoutingKey, th2Pin: pin, Logger: zerolog.New(os.Stdout).With().Str("component", "rabbitmq_message_sender").Timestamp().Logger()} + sendQueue: queueConfig.RoutingKey, th2Pin: pin, Logger: log.ForComponent("rabbitmq_message_sender")} cmr.senders[pin] = result cmr.Logger.Trace().Str("Pin", pin).Msg("Created sender") return result diff --git a/pkg/queue/rabbitmq/internal/message/subscriber.go b/pkg/queue/rabbitmq/internal/message/subscriber.go index e95551d..be57e16 100644 --- a/pkg/queue/rabbitmq/internal/message/subscriber.go +++ b/pkg/queue/rabbitmq/internal/message/subscriber.go @@ -18,17 +18,15 @@ package message import ( "errors" "fmt" - "github.com/th2-net/th2-common-go/pkg/common" + "github.com/prometheus/client_golang/prometheus" + "github.com/prometheus/client_golang/prometheus/promauto" + "github.com/rs/zerolog" p_buff "github.com/th2-net/th2-common-go/pkg/common/grpc/th2_grpc_common" + "github.com/th2-net/th2-common-go/pkg/log" "github.com/th2-net/th2-common-go/pkg/queue" "github.com/th2-net/th2-common-go/pkg/queue/filter" "github.com/th2-net/th2-common-go/pkg/queue/rabbitmq/internal" "github.com/th2-net/th2-common-go/pkg/queue/rabbitmq/internal/connection" - "os" - - "github.com/prometheus/client_golang/prometheus" - "github.com/prometheus/client_golang/prometheus/promauto" - "github.com/rs/zerolog" "github.com/streadway/amqp" "github.com/th2-net/th2-common-go/pkg/metrics" @@ -63,10 +61,7 @@ func newSubscriber( subscriberType internal.SubscriberType, contentType contentType, ) (internal.Subscriber, error) { - logger := zerolog.New(os.Stdout).With(). - Str(common.ComponentLoggerKey, "rabbitmq_message_subscriber"). - Timestamp(). - Logger() + logger := log.ForComponent("rabbitmq_message_subscriber") baseHandler := baseMessageHandler{&logger, pinName} switch subscriberType { case internal.AutoSubscriberType: @@ -205,7 +200,7 @@ func (cs *confirmationMessageHandler) Handle(msgDelivery amqp.Delivery, timer *p return nil } delivery := queue.Delivery{Redelivered: msgDelivery.Redelivered} - deliveryConfirm := internal.DeliveryConfirmation{Delivery: &msgDelivery, Logger: zerolog.New(os.Stdout).With().Timestamp().Logger(), Timer: timer} + deliveryConfirm := internal.DeliveryConfirmation{Delivery: &msgDelivery, Logger: log.ForComponent("confirmation"), Timer: timer} metrics.UpdateMessageMetrics(result, th2MessageSubscribeTotal, cs.th2Pin) handleErr := listener.Handle(delivery, result, &deliveryConfirm) diff --git a/test/modules/internal/shared.go b/test/modules/internal/shared.go index 943fe00..f38ae35 100644 --- a/test/modules/internal/shared.go +++ b/test/modules/internal/shared.go @@ -20,8 +20,8 @@ import ( "errors" "github.com/rs/zerolog" "github.com/th2-net/th2-common-go/pkg/common" + "github.com/th2-net/th2-common-go/pkg/log" "io/fs" - "os" ) func CreateTestFactory(fileSystem fs.FS) common.Factory { @@ -72,7 +72,7 @@ func (d *dummyFactory) Get(key common.ModuleKey) (common.Module, error) { } func (d *dummyFactory) GetLogger(name string) zerolog.Logger { - return zerolog.New(os.Stdout).With().Str("name", name).Logger() + return log.ForComponent(name) } func (d *dummyFactory) GetCustomConfiguration(any any) error {