diff --git a/.golangci.yml b/.golangci.yml index e666f2e37..ad71ed60d 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -348,6 +348,7 @@ issues: # excluded by default patterns execute `golangci-lint run --help` exclude: - Using the variable on range scope .* in function literal + - should have a package comment # Excluding configuration per-path, per-linter, per-text and per-source exclude-rules: diff --git a/README.md b/README.md index 057621c12..33612c46e 100644 --- a/README.md +++ b/README.md @@ -126,7 +126,6 @@ This repository was created using [MrZ's `go-template`](https://github.com/mrz18 - [BitcoinSchema/go-bitcoin](https://github.com/BitcoinSchema/go-bitcoin) - [BuxOrg/bux](https://github.com/BuxOrg/bux) - [mrz1836/go-api-router](https://github.com/mrz1836/go-api-router) -- [mrz1836/go-logger](https://github.com/mrz1836/go-logger) - [mrz1836/go-sanitize](https://github.com/mrz1836/go-sanitize) - [stretchr/testify](https://github.com/stretchr/testify) - [tonicpow/go-paymail](https://github.com/tonicpow/go-paymail) diff --git a/actions/graphql/routes.go b/actions/graphql/routes.go index 40d3dc607..f4c7c63c7 100644 --- a/actions/graphql/routes.go +++ b/actions/graphql/routes.go @@ -19,7 +19,6 @@ import ( "github.com/gofrs/uuid" "github.com/julienschmidt/httprouter" apirouter "github.com/mrz1836/go-api-router" - "github.com/mrz1836/go-logger" "github.com/vektah/gqlparser/v2/gqlerror" ) @@ -62,13 +61,13 @@ func RegisterRoutes(router *apirouter.Router, appConfig *config.AppConfig, servi "variables": oc.Variables, } // LogParamsFormat "request_id=\"%s\" method=\"%s\" path=\"%s\" ip_address=\"%s\" user_agent=\"%s\" params=\"%v\"\n" - logger.NoFilePrintf(apirouter.LogParamsFormat, reqInfo.id, reqInfo.method, reqInfo.path, reqInfo.ip, reqInfo.userAgent, params) + services.Logger.Info().Msgf(apirouter.LogParamsFormat, reqInfo.id, reqInfo.method, reqInfo.path, reqInfo.ip, reqInfo.userAgent, params) return next(ctx) }) srv.SetErrorPresenter(func(ctx context.Context, err error) *gqlerror.Error { // LogErrorFormat "request_id=\"%s\" ip_address=\"%s\" type=\"%s\" internal_message=\"%s\" code=%d\n" reqInfo := ctx.Value(config.GraphRequestInfo).(requestInfo) - logger.NoFilePrintf(apirouter.LogErrorFormat, reqInfo.id, reqInfo.ip, "GraphQL", err.Error(), 500) + services.Logger.Info().Msgf(apirouter.LogErrorFormat, reqInfo.id, reqInfo.ip, "GraphQL", err.Error(), 500) return &gqlerror.Error{ Message: "presented: " + err.Error(), Path: graphql.GetPath(ctx), @@ -109,16 +108,16 @@ func RegisterRoutes(router *apirouter.Router, appConfig *config.AppConfig, servi ), ) if appConfig.Debug { - logger.Data(2, logger.DEBUG, "started graphql playground server on "+playgroundPath) + services.Logger.Debug().Msgf("started graphql playground server on %s", playgroundPath) } } else { - logger.Data(2, logger.ERROR, "Failed starting graphql playground server directory equals playground directory "+serverPath+" = "+playgroundPath) + services.Logger.Error().Msgf("Failed starting graphql playground server directory equals playground directory %s = %s", serverPath, playgroundPath) } } // Success on the routes if appConfig.Debug { - logger.Data(2, logger.DEBUG, "registered graphql routes on "+serverPath) + services.Logger.Debug().Msg("registered graphql routes on " + serverPath) } } diff --git a/actions/paymail/routes.go b/actions/paymail/routes.go index 4d34935a7..d4cc2a2f6 100644 --- a/actions/paymail/routes.go +++ b/actions/paymail/routes.go @@ -4,7 +4,6 @@ import ( "github.com/BuxOrg/bux-server/actions" "github.com/BuxOrg/bux-server/config" apirouter "github.com/mrz1836/go-api-router" - "github.com/mrz1836/go-logger" ) // Action is an extension of actions.Action for this package @@ -29,6 +28,6 @@ func RegisterRoutes(router *apirouter.Router, appConfig *config.AppConfig, servi router.HTTPRouter.DELETE("/"+config.CurrentMajorVersion+"/paymail", action.Request(router, requireAdmin.Wrap(action.delete))) if appConfig.Debug { - logger.Data(2, logger.DEBUG, "registered paymail routes and model") + services.Logger.Debug().Msg("registered paymail routes and model") } } diff --git a/cmd/server/main.go b/cmd/server/main.go index cb76ec3b6..564c926f8 100644 --- a/cmd/server/main.go +++ b/cmd/server/main.go @@ -5,7 +5,6 @@ package main import ( "context" - "fmt" "os" "os/signal" "time" @@ -14,7 +13,7 @@ import ( "github.com/BuxOrg/bux-server/dictionary" _ "github.com/BuxOrg/bux-server/docs" "github.com/BuxOrg/bux-server/server" - "github.com/mrz1836/go-logger" + "github.com/BuxOrg/bux/logging" ) // main method starts everything for the BUX Server @@ -25,17 +24,18 @@ import ( // @name bux-auth-xpub func main() { + defaultLogger := logging.GetDefaultLogger() // Load the Application Configuration appConfig, err := config.Load("") if err != nil { - logger.Fatalf(dictionary.GetInternalMessage(dictionary.ErrorLoadingConfig), err.Error()) + defaultLogger.Fatal().Msgf(dictionary.GetInternalMessage(dictionary.ErrorLoadingConfig), err.Error()) return } // Load the Application Services var services *config.AppServices if services, err = appConfig.LoadServices(context.Background()); err != nil { - logger.Fatalf(dictionary.GetInternalMessage(dictionary.ErrorLoadingService), config.ApplicationName, err.Error()) + defaultLogger.Fatal().Msgf(dictionary.GetInternalMessage(dictionary.ErrorLoadingService), config.ApplicationName, err.Error()) return } @@ -44,22 +44,21 @@ func main() { // Validate configuration (after services have been loaded) if err = appConfig.Validate(txn); err != nil { - logger.Fatalf(dictionary.GetInternalMessage(dictionary.ErrorLoadingConfig), err.Error()) + services.Logger.Fatal().Msgf(dictionary.GetInternalMessage(dictionary.ErrorLoadingConfig), err.Error()) return } // (debugging: show services that are enabled or not) if appConfig.Debug { - logger.Data(2, logger.DEBUG, - fmt.Sprintf("datastore: %s | cachestore: %s | taskmanager: %s [%s] | new_relic: %t | paymail: %t | graphql: %t", - appConfig.Datastore.Engine.String(), - appConfig.Cachestore.Engine.String(), - appConfig.TaskManager.Engine.String(), - appConfig.TaskManager.Factory.String(), - appConfig.NewRelic.Enabled, - appConfig.Paymail.Enabled, - appConfig.GraphQL.Enabled, - ), + services.Logger.Debug().Msgf( + "datastore: %s | cachestore: %s | taskmanager: %s [%s] | new_relic: %t | paymail: %t | graphql: %t", + appConfig.Datastore.Engine.String(), + appConfig.Cachestore.Engine.String(), + appConfig.TaskManager.Engine.String(), + appConfig.TaskManager.Factory.String(), + appConfig.NewRelic.Enabled, + appConfig.Paymail.Enabled, + appConfig.GraphQL.Enabled, ) } @@ -76,7 +75,7 @@ func main() { ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) defer cancel() if err = appServer.Shutdown(ctx); err != nil { - logger.Fatalf("error shutting down: %s", err.Error()) + services.Logger.Fatal().Msgf("error shutting down: %s", err.Error()) } close(idleConnectionsClosed) @@ -86,10 +85,7 @@ func main() { txn.End() // Listen and serve - logger.Data(2, logger.DEBUG, - "starting ["+appConfig.Environment+"] "+config.ApplicationName+" server...", - logger.MakeParameter("port", appConfig.Server.Port), - ) + services.Logger.Debug().Msgf("starting [%s] %s server at port %s...", appConfig.Environment, config.ApplicationName, appConfig.Server.Port) appServer.Serve() <-idleConnectionsClosed diff --git a/config/config.go b/config/config.go index 1b2cb8f00..ffb840cbc 100644 --- a/config/config.go +++ b/config/config.go @@ -76,6 +76,7 @@ type ( BroadcastClientAPIs []string `json:"broadcast_client_apis" mapstructure:"broadcast_client_apis"` UseBeef bool `json:"use_beef" mapstructure:"use_beef"` Pulse PulseConfig `json:"pulse" mapstructure:"pulse"` + Logging *LoggingConfig `json:"logging" mapstructure:"logging"` } // AuthenticationConfig is the configuration for Authentication @@ -182,6 +183,14 @@ type ( PulseURL string `json:"pulse_url" mapstructure:"pulse_url"` PulseAuthToken string `json:"pulse_auth_token" mapstructure:"pulse_auth_token"` } + + // LoggingConfig is a configuration for logging + LoggingConfig struct { + Level string `json:"level" mapstructure:"level"` + Format string `json:"format" mapstructure:"format"` + InstanceName string `json:"instance_name" mapstructure:"instance_name"` + LogOrigin bool `json:"log_origin" mapstructure:"log_origin"` + } ) // GetUserAgent will return the outgoing user agent diff --git a/config/envs/development.json b/config/envs/development.json index 56247b057..8be69c970 100644 --- a/config/envs/development.json +++ b/config/envs/development.json @@ -112,5 +112,11 @@ "pulse": { "pulse_url": "http://localhost:8000/api/v1/chain/merkleroot/verify", "pulse_auth_token": "asd" + }, + "logging": { + "level": "debug", + "format": "console", + "instance_name": "bux-instance-development", + "log_origin": false } } diff --git a/config/envs/docker-compose.json b/config/envs/docker-compose.json index 56a95c7cc..712df5b30 100644 --- a/config/envs/docker-compose.json +++ b/config/envs/docker-compose.json @@ -103,5 +103,11 @@ "pulse": { "pulse_url": "http://localhost:8000/api/v1/chain/merkleroot/verify", "pulse_auth_token": "asd" + }, + "logging": { + "level": "debug", + "format": "console", + "instance_name": "bux-instance-docker", + "log_origin": false } } diff --git a/config/envs/production.json b/config/envs/production.json index 75d172692..cd228867c 100644 --- a/config/envs/production.json +++ b/config/envs/production.json @@ -104,5 +104,11 @@ "pulse": { "pulse_url": "http://localhost:8000/api/v1/chain/merkleroot/verify", "pulse_auth_token": "asd" + }, + "logging": { + "level": "info", + "format": "json", + "instance_name": "bux-instance-prod", + "log_origin": true } } diff --git a/config/envs/staging.json b/config/envs/staging.json index d50bff405..96dc0f720 100644 --- a/config/envs/staging.json +++ b/config/envs/staging.json @@ -104,5 +104,11 @@ "pulse": { "pulse_url": "http://localhost:8000/api/v1/chain/merkleroot/verify", "pulse_auth_token": "asd" + }, + "logging": { + "level": "info", + "format": "json", + "instance_name": "bux-instance-staging", + "log_origin": true } } diff --git a/config/envs/test.json b/config/envs/test.json index d54b1d0f5..51947c9ac 100644 --- a/config/envs/test.json +++ b/config/envs/test.json @@ -103,5 +103,11 @@ "pulse": { "pulse_url": "http://localhost:8000/api/v1/chain/merkleroot/verify", "pulse_auth_token": "asd" + }, + "logging": { + "level": "info", + "format": "json", + "instance_name": "bux-instance-test", + "log_origin": true } } diff --git a/config/load.go b/config/load.go index 68527c4e6..3a9c0d066 100644 --- a/config/load.go +++ b/config/load.go @@ -8,7 +8,6 @@ import ( "github.com/BuxOrg/bux-server/dictionary" "github.com/mrz1836/go-datastore" - "github.com/mrz1836/go-logger" "github.com/spf13/viper" ) @@ -37,12 +36,10 @@ var viperLock sync.Mutex // Load all environment variables func Load(customWorkingDirectory string) (_appConfig *AppConfig, err error) { - // Check the environment we are running environment := os.Getenv(EnvironmentKey) if !isValidEnvironment(environment) { err = fmt.Errorf(dictionary.GetInternalMessage(dictionary.ErrorInvalidEnv), environment) - logger.Data(2, logger.ERROR, err.Error()) return } @@ -72,13 +69,9 @@ func Load(customWorkingDirectory string) (_appConfig *AppConfig, err error) { // Read the configuration if err = viper.ReadInConfig(); err != nil { err = fmt.Errorf(dictionary.GetInternalMessage(dictionary.ErrorReadingConfig), err.Error()) - logger.Data(2, logger.ERROR, err.Error()) return } - // Log the configuration that was detected and where it was loaded from - logger.Data(2, logger.INFO, environment+" configuration env file processed in dir "+workingDirectory) - // Initialize _appConfigVal := AppConfig{ Authentication: AuthenticationConfig{}, @@ -100,7 +93,6 @@ func Load(customWorkingDirectory string) (_appConfig *AppConfig, err error) { // Unmarshal into values struct if err = viper.Unmarshal(&_appConfigVal); err != nil { err = fmt.Errorf(dictionary.GetInternalMessage(dictionary.ErrorViper), err.Error()) - logger.Data(2, logger.ERROR, err.Error()) return } diff --git a/config/services.go b/config/services.go index 00babfa8e..8f62dcbbf 100644 --- a/config/services.go +++ b/config/services.go @@ -10,6 +10,7 @@ import ( "time" "github.com/BuxOrg/bux" + "github.com/BuxOrg/bux-server/logging" "github.com/BuxOrg/bux/chainstate" "github.com/BuxOrg/bux/cluster" "github.com/BuxOrg/bux/taskmanager" @@ -18,8 +19,8 @@ import ( "github.com/go-redis/redis/v8" "github.com/mrz1836/go-cachestore" "github.com/mrz1836/go-datastore" - "github.com/mrz1836/go-logger" "github.com/newrelic/go-agent/v3/newrelic" + "github.com/rs/zerolog" "github.com/tonicpow/go-minercraft/v2" ) @@ -28,6 +29,7 @@ type ( AppServices struct { Bux bux.ClientInterface NewRelic *newrelic.Application + Logger *zerolog.Logger } ) @@ -48,8 +50,15 @@ func (a *AppConfig) LoadServices(ctx context.Context) (*AppServices, error) { ctx = newrelic.NewContext(ctx, txn) defer txn.End() + logger, err := logging.CreateLogger(a.Logging.InstanceName, a.Logging.Format, a.Logging.Level, a.Logging.LogOrigin) + if err != nil { + return nil, err + } + + _services.Logger = logger + // Load BUX - if err = _services.loadBux(ctx, a, false); err != nil { + if err = _services.loadBux(ctx, a, false, logger); err != nil { return nil, err } @@ -74,7 +83,7 @@ func (a *AppConfig) LoadTestServices(ctx context.Context) (*AppServices, error) defer txn.End() // Load bux for testing - if err = _services.loadBux(ctx, a, true); err != nil { + if err = _services.loadBux(ctx, a, true, _services.Logger); err != nil { return nil, err } @@ -124,11 +133,13 @@ func (s *AppServices) CloseAll(ctx context.Context) { } // All services closed! - logger.Data(2, logger.DEBUG, "all services have been closed") + if s.Logger != nil { + s.Logger.Debug().Msg("all services have been closed") + } } // loadBux will load the bux client (including CacheStore and DataStore) -func (s *AppServices) loadBux(ctx context.Context, appConfig *AppConfig, testMode bool) (err error) { +func (s *AppServices) loadBux(ctx context.Context, appConfig *AppConfig, testMode bool, logger *zerolog.Logger) (err error) { var options []bux.ClientOps // Set new relic if enabled @@ -149,7 +160,10 @@ func (s *AppServices) loadBux(ctx context.Context, appConfig *AppConfig, testMod options = append(options, bux.WithImportBlockHeaders(appConfig.ImportBlockHeaders)) } - // todo: customize the logger + if logger != nil { + buxLogger := logger.With().Str("service", "bux").Logger() + options = append(options, bux.WithLogger(&buxLogger)) + } // todo: feature: override the config from JSON env (side-load your own /envs/custom-config.json diff --git a/go.mod b/go.mod index 4c8496afb..03558898c 100644 --- a/go.mod +++ b/go.mod @@ -4,37 +4,28 @@ go 1.19 require ( github.com/99designs/gqlgen v0.17.41 - github.com/BuxOrg/bux v0.8.0 + github.com/BuxOrg/bux v0.8.1 github.com/BuxOrg/bux-models v0.2.1 github.com/bitcoin-sv/go-broadcast-client v0.9.0 github.com/go-ozzo/ozzo-validation v3.6.0+incompatible github.com/go-redis/redis/v8 v8.11.5 github.com/gofrs/uuid v4.4.0+incompatible github.com/julienschmidt/httprouter v1.3.0 - github.com/mrz1836/go-api-router v0.7.0 + github.com/mrz1836/go-api-router v0.6.2 github.com/mrz1836/go-cachestore v0.3.3 github.com/mrz1836/go-datastore v0.5.8 - github.com/mrz1836/go-logger v0.3.2 github.com/mrz1836/go-parameters v0.4.1 github.com/mrz1836/go-sanitize v1.3.1 github.com/mrz1836/go-validate v0.2.0 github.com/newrelic/go-agent/v3 v3.28.1 github.com/newrelic/go-agent/v3/integrations/nrhttprouter v1.0.2 + github.com/rs/zerolog v1.31.0 github.com/spf13/viper v1.18.1 github.com/stretchr/testify v1.8.4 github.com/swaggo/swag v1.16.2 github.com/vektah/gqlparser/v2 v2.5.10 ) -require ( - github.com/jackc/puddle/v2 v2.2.1 // indirect - github.com/libsv/go-p2p v0.1.4 // indirect - github.com/mattn/go-colorable v0.1.13 // indirect - github.com/mattn/go-isatty v0.0.20 // indirect - github.com/rs/zerolog v1.31.0 // indirect - go.elastic.co/ecszerolog v0.2.0 // indirect -) - require ( github.com/KyleBanks/depth v1.2.1 // indirect github.com/agnivade/levenshtein v1.1.1 // indirect @@ -68,7 +59,7 @@ require ( github.com/golang/protobuf v1.5.3 // indirect github.com/golang/snappy v0.0.4 // indirect github.com/gomodule/redigo v2.0.0+incompatible // indirect - github.com/google/uuid v1.4.0 // indirect + github.com/google/uuid v1.5.0 // indirect github.com/gorilla/mux v1.8.1 // indirect github.com/gorilla/websocket v1.5.1 // indirect github.com/hashicorp/golang-lru v1.0.2 // indirect @@ -77,7 +68,8 @@ require ( github.com/iancoleman/strcase v0.3.0 // indirect github.com/jackc/pgpassfile v1.0.0 // indirect github.com/jackc/pgservicefile v0.0.0-20231201235250-de7065d80cb9 // indirect - github.com/jackc/pgx/v5 v5.5.0 // indirect + github.com/jackc/pgx/v5 v5.5.1 // indirect + github.com/jackc/puddle/v2 v2.2.1 // indirect github.com/jinzhu/inflection v1.0.0 // indirect github.com/jinzhu/now v1.1.5 // indirect github.com/josharian/intern v1.0.0 // indirect @@ -88,14 +80,18 @@ require ( github.com/libsv/go-bk v0.1.6 // indirect github.com/libsv/go-bt v1.0.8 // indirect github.com/libsv/go-bt/v2 v2.2.5 // indirect + github.com/libsv/go-p2p v0.1.4 // indirect github.com/magiconair/properties v1.8.7 // indirect github.com/mailru/easyjson v0.7.7 // indirect github.com/matryer/respond v1.0.1 // indirect + github.com/mattn/go-colorable v0.1.13 // indirect + github.com/mattn/go-isatty v0.0.20 // indirect github.com/mattn/go-sqlite3 v1.14.18 // indirect github.com/miekg/dns v1.1.57 // indirect github.com/mitchellh/mapstructure v1.5.0 // indirect github.com/montanaflynn/stats v0.7.1 // indirect github.com/mrz1836/go-cache v0.9.2 // indirect + github.com/mrz1836/go-logger v0.3.2 // indirect github.com/newrelic/go-agent/v3/integrations/nrmongo v1.1.2 // indirect github.com/pelletier/go-toml/v2 v2.1.0 // indirect github.com/pkg/errors v0.9.1 // indirect @@ -126,6 +122,7 @@ require ( github.com/xdg-go/scram v1.1.2 // indirect github.com/xdg-go/stringprep v1.0.4 // indirect github.com/youmark/pkcs8 v0.0.0-20201027041543-1326539a0a0a // indirect + go.elastic.co/ecszerolog v0.2.0 go.mongodb.org/mongo-driver v1.13.1 // indirect go.uber.org/multierr v1.11.0 // indirect golang.org/x/crypto v0.16.0 // indirect @@ -135,9 +132,9 @@ require ( golang.org/x/sync v0.5.0 // indirect golang.org/x/sys v0.15.0 // indirect golang.org/x/text v0.14.0 // indirect - golang.org/x/tools v0.16.0 // indirect - google.golang.org/genproto/googleapis/rpc v0.0.0-20231127180814-3a041ad873d4 // indirect - google.golang.org/grpc v1.59.0 // indirect + golang.org/x/tools v0.16.1 // indirect + google.golang.org/genproto/googleapis/rpc v0.0.0-20231212172506-995d672761c0 // indirect + google.golang.org/grpc v1.60.0 // indirect google.golang.org/protobuf v1.31.0 // indirect gopkg.in/ini.v1 v1.67.0 // indirect gopkg.in/yaml.v3 v3.0.1 // indirect diff --git a/go.sum b/go.sum index 8276b804a..23602ddc4 100644 --- a/go.sum +++ b/go.sum @@ -1,7 +1,7 @@ github.com/99designs/gqlgen v0.17.41 h1:C1/zYMhGVP5TWNCNpmZ9Mb6CqT1Vr5SHEWoTOEJ3v3I= github.com/99designs/gqlgen v0.17.41/go.mod h1:GQ6SyMhwFbgHR0a8r2Wn8fYgEwPxxmndLFPhU63+cJE= -github.com/BuxOrg/bux v0.8.0 h1:i/oZMSZYJV5BWDiNEEQ21SVUSwJPcy2PaiKUf4zI3Pc= -github.com/BuxOrg/bux v0.8.0/go.mod h1:9h8+Sdw907Cf2IzVrlH16O/K6kyxUGfpp3wqO+ous4k= +github.com/BuxOrg/bux v0.8.1 h1:YEMK1LXOEJyFqyqafH7ZI0jm19a+y5HiNFQh5ZRCdbk= +github.com/BuxOrg/bux v0.8.1/go.mod h1:9h8+Sdw907Cf2IzVrlH16O/K6kyxUGfpp3wqO+ous4k= github.com/BuxOrg/bux-models v0.2.1 h1:wgNTYCqisH3M+Z87hb0Jpbxc8MMdOEJ35mdw/WoZ73k= github.com/BuxOrg/bux-models v0.2.1/go.mod h1:nH3MOdsIPPerBPOiEvwA01yTeArYtBk+PtDo7E+vPCk= github.com/DATA-DOG/go-sqlmock v1.5.1 h1:FK6RCIUSfmbnI/imIICmboyQBkOckutaa6R5YYlLZyo= @@ -141,8 +141,8 @@ github.com/google/go-cmp v0.5.2/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/ github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= github.com/google/go-cmp v0.5.6/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38= -github.com/google/uuid v1.4.0 h1:MtMxsa51/r9yyhkyLsVeVt0B+BGQZzpQiTQ4eHZ8bc4= -github.com/google/uuid v1.4.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= +github.com/google/uuid v1.5.0 h1:1p67kYwdtXjb0gL0BPiP1Av9wiZPo5A8z2cWkTZ+eyU= +github.com/google/uuid v1.5.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= github.com/gopherjs/gopherjs v0.0.0-20181103185306-d547d1d9531e/go.mod h1:wJfORRmW1u3UXTncJ5qlYoELFm8eSnnEO6hX4iZ3EWY= github.com/gorilla/mux v1.8.1 h1:TuBL49tXwgrFYWhqrNgrUNEY92u81SPhu7sTdzQEiWY= github.com/gorilla/mux v1.8.1/go.mod h1:AKf9I4AEqPTmMytcMc0KkNouC66V3BtZ4qD5fmWSiMQ= @@ -163,8 +163,8 @@ github.com/jackc/pgpassfile v1.0.0 h1:/6Hmqy13Ss2zCq62VdNG8tM1wchn8zjSGOBJ6icpsI github.com/jackc/pgpassfile v1.0.0/go.mod h1:CEx0iS5ambNFdcRtxPj5JhEz+xB6uRky5eyVu/W2HEg= github.com/jackc/pgservicefile v0.0.0-20231201235250-de7065d80cb9 h1:L0QtFUgDarD7Fpv9jeVMgy/+Ec0mtnmYuImjTz6dtDA= github.com/jackc/pgservicefile v0.0.0-20231201235250-de7065d80cb9/go.mod h1:5TJZWKEWniPve33vlWYSoGYefn3gLQRzjfDlhSJ9ZKM= -github.com/jackc/pgx/v5 v5.5.0 h1:NxstgwndsTRy7eq9/kqYc/BZh5w2hHJV86wjvO+1xPw= -github.com/jackc/pgx/v5 v5.5.0/go.mod h1:Ig06C2Vu0t5qXC60W8sqIthScaEnFvojjj9dSljmHRA= +github.com/jackc/pgx/v5 v5.5.1 h1:5I9etrGkLrN+2XPCsi6XLlV5DITbSL/xBZdmAxFcXPI= +github.com/jackc/pgx/v5 v5.5.1/go.mod h1:Ig06C2Vu0t5qXC60W8sqIthScaEnFvojjj9dSljmHRA= github.com/jackc/puddle/v2 v2.2.1 h1:RhxXJtFG022u4ibrCSMSiu5aOq1i77R3OHKNJj77OAk= github.com/jackc/puddle/v2 v2.2.1/go.mod h1:vriiEXHvEE654aYKXXjOvZM39qJ0q+azkZFrfEOc3H4= github.com/jarcoal/httpmock v1.3.1 h1:iUx3whfZWVf3jT01hQTO/Eo5sAYtB2/rqaUuOtpInww= @@ -231,8 +231,8 @@ github.com/mitchellh/mapstructure v1.5.0/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RR github.com/montanaflynn/stats v0.0.0-20171201202039-1bf9dbcd8cbe/go.mod h1:wL8QJuTMNUDYhXwkmfOly8iTdp5TEcJFWZD2D7SIkUc= github.com/montanaflynn/stats v0.7.1 h1:etflOAAHORrCC44V+aR6Ftzort912ZU+YLiSTuV8eaE= github.com/montanaflynn/stats v0.7.1/go.mod h1:etXPPgVO6n31NxCd9KQUMvCM+ve0ruNzt6R8Bnaayow= -github.com/mrz1836/go-api-router v0.7.0 h1:RTnU4g92IiTLDEHBUuftnTMfGRIc9/Q7y1wDUYJEV5E= -github.com/mrz1836/go-api-router v0.7.0/go.mod h1:zIj4Fe4mLx+VZjtWvxvPcfJMo9l8J/xEB5mDWh2oGQU= +github.com/mrz1836/go-api-router v0.6.2 h1:6IhpqUf42scot4rcPO7q+vkWFMJMDbqZqbGrlUjKwxc= +github.com/mrz1836/go-api-router v0.6.2/go.mod h1:g/swptRngTcD7ro4DSOpbHA+sYPtmvWyoSZ4i4aTsFA= github.com/mrz1836/go-cache v0.9.2 h1:pQK3SJg6kfbn43oKW68R3qCWRILKSvSHs3qvwn8OzHc= github.com/mrz1836/go-cache v0.9.2/go.mod h1:IphLH8lmLdSI5N+8MswFEaqP1bJQ8800CxXre5nJD4M= github.com/mrz1836/go-cachestore v0.3.3 h1:E9f1frV38iAfpJ7vYR8bJ/F3GEdmPUTTFwxDf8DZP/A= @@ -479,16 +479,16 @@ golang.org/x/tools v0.0.0-20200530233709-52effbd89c51/go.mod h1:EkVYQZoAsY45+roY golang.org/x/tools v0.0.0-20201224043029-2b0845dc783e/go.mod h1:emZCQorbCU4vsT4fOWvOPXz4eW1wZW4PmDk9uLelYpA= golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc= golang.org/x/tools v0.6.0/go.mod h1:Xwgl3UAJ/d3gWutnCtw505GrjyAbvKui8lOU390QaIU= -golang.org/x/tools v0.16.0 h1:GO788SKMRunPIBCXiQyo2AaexLstOrVhuAL5YwsckQM= -golang.org/x/tools v0.16.0/go.mod h1:kYVVN6I1mBNoB1OX+noeBjbRk4IUEPa7JJ+TJMEooJ0= +golang.org/x/tools v0.16.1 h1:TLyB3WofjdOEepBHAU20JdNC1Zbg87elYofWYAY5oZA= +golang.org/x/tools v0.16.1/go.mod h1:kYVVN6I1mBNoB1OX+noeBjbRk4IUEPa7JJ+TJMEooJ0= golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= -google.golang.org/genproto/googleapis/rpc v0.0.0-20231127180814-3a041ad873d4 h1:DC7wcm+i+P1rN3Ff07vL+OndGg5OhNddHyTA+ocPqYE= -google.golang.org/genproto/googleapis/rpc v0.0.0-20231127180814-3a041ad873d4/go.mod h1:eJVxU6o+4G1PSczBr85xmyvSNYAKvAYgkub40YGomFM= -google.golang.org/grpc v1.59.0 h1:Z5Iec2pjwb+LEOqzpB2MR12/eKFhDPhuqW91O+4bwUk= -google.golang.org/grpc v1.59.0/go.mod h1:aUPDwccQo6OTjy7Hct4AfBPD1GptF4fyUjIkQ9YtF98= +google.golang.org/genproto/googleapis/rpc v0.0.0-20231212172506-995d672761c0 h1:/jFB8jK5R3Sq3i/lmeZO0cATSzFfZaJq1J2Euan3XKU= +google.golang.org/genproto/googleapis/rpc v0.0.0-20231212172506-995d672761c0/go.mod h1:FUoWkonphQm3RhTS+kOEhF8h0iDpm4tdXolVCeZ9KKA= +google.golang.org/grpc v1.60.0 h1:6FQAR0kM31P6MRdeluor2w2gPaS4SVNrD/DNTxrQ15k= +google.golang.org/grpc v1.60.0/go.mod h1:OlCHIeLYqSSsLi6i49B5QGdzaMZK9+M7LXN2FKz4eGM= google.golang.org/protobuf v0.0.0-20200109180630-ec00e32a8dfd/go.mod h1:DFci5gLYBciE7Vtevhsrf46CRTquxDuWsQurQQe4oz8= google.golang.org/protobuf v0.0.0-20200221191635-4d8936d0db64/go.mod h1:kwYJMbMJ01Woi6D6+Kah6886xMZcty6N08ah7+eCXa0= google.golang.org/protobuf v0.0.0-20200228230310-ab0ca4ff8a60/go.mod h1:cfTl7dwQJ+fmap5saPgwCLgHXTUD7jkjRqWcaiX5VyM= diff --git a/logging/logging.go b/logging/logging.go new file mode 100644 index 000000000..3f2e1f67b --- /dev/null +++ b/logging/logging.go @@ -0,0 +1,71 @@ +package logging + +import ( + "io" + "os" + "time" + + "github.com/rs/zerolog" + "go.elastic.co/ecszerolog" +) + +const ( + consoleLogFormat = "console" + jsonLogFormat = "json" +) + +// CreateLogger create and configure zerolog logger based on app config. +func CreateLogger(instanceName, format, level string, logOrigin bool) (*zerolog.Logger, error) { + var writer io.Writer + if format == consoleLogFormat { + writer = zerolog.ConsoleWriter{ + Out: os.Stdout, + TimeFormat: "2006-01-02 15:04:05.000", + } + } else { + writer = os.Stdout + } + + parsedLevel, err := zerolog.ParseLevel(level) + if err != nil { + return nil, err + } + + logLevel := ecszerolog.Level(parsedLevel) + origin := ecszerolog.Origin() + var logger zerolog.Logger + + if logOrigin { + logger = ecszerolog.New(writer, logLevel, origin). + With(). + Timestamp(). + Str("application", instanceName). + Str("service", "bux-server"). + Logger() + } else { + logger = ecszerolog.New(writer, logLevel). + With(). + Timestamp(). + Str("application", instanceName). + Str("service", "bux-server"). + Logger() + } + + zerolog.TimestampFunc = func() time.Time { + return time.Now().In(time.Local) //nolint:gosmopolitan // We want local time inside logger. + } + + return &logger, nil +} + +// GetDefaultLogger create and configure default zerolog logger. It should be used before config is loaded +func GetDefaultLogger() *zerolog.Logger { + logger := ecszerolog.New(os.Stdout, ecszerolog.Level(zerolog.DebugLevel)). + With(). + Timestamp(). + Caller(). + Str("application", "bux-default"). + Logger() + + return &logger +} diff --git a/server/server.go b/server/server.go index 8a5b95dbf..d7d30bd9e 100644 --- a/server/server.go +++ b/server/server.go @@ -17,7 +17,6 @@ import ( "github.com/BuxOrg/bux-server/actions/xpubs" "github.com/BuxOrg/bux-server/config" apirouter "github.com/mrz1836/go-api-router" - "github.com/mrz1836/go-logger" "github.com/newrelic/go-agent/v3/integrations/nrhttprouter" httpSwagger "github.com/swaggo/http-swagger" ) @@ -70,11 +69,7 @@ func (s *Server) Serve() { // Listen and serve if err := s.WebServer.ListenAndServe(); err != nil { - logger.Data( - 2, - logger.DEBUG, - "shutting down "+config.ApplicationName+" server ["+err.Error()+"]...", - logger.MakeParameter("port", s.AppConfig.Server.Port)) + s.Services.Logger.Debug().Msgf("shutting down %s server [%s] on port %s...", config.ApplicationName, err.Error(), s.AppConfig.Server.Port) } }