Skip to content

Commit

Permalink
Use internal logger instead of global
Browse files Browse the repository at this point in the history
  • Loading branch information
OptimumCode committed Oct 20, 2023
1 parent 6b5f3cb commit 4871b01
Show file tree
Hide file tree
Showing 13 changed files with 123 additions and 116 deletions.
65 changes: 11 additions & 54 deletions pkg/factory/commonFactory.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand All @@ -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")
Expand All @@ -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)
Expand All @@ -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 {
Expand Down
73 changes: 73 additions & 0 deletions pkg/log/logger.go
Original file line number Diff line number Diff line change
@@ -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
}
File renamed without changes.
7 changes: 3 additions & 4 deletions pkg/modules/grpc/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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
Expand All @@ -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
}
Expand Down
6 changes: 2 additions & 4 deletions pkg/modules/prometheus/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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")
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/queue/filter/default.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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,
Expand Down
15 changes: 4 additions & 11 deletions pkg/queue/rabbitmq/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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
}
Expand Down
6 changes: 3 additions & 3 deletions pkg/queue/rabbitmq/internal/connection/connectionManager.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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).
Expand Down
11 changes: 5 additions & 6 deletions pkg/queue/rabbitmq/internal/event/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
Expand Down
17 changes: 6 additions & 11 deletions pkg/queue/rabbitmq/internal/event/subscriber.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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:
Expand Down Expand Up @@ -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)
Expand Down
Loading

0 comments on commit 4871b01

Please sign in to comment.