From 0f002b563360624f681c1e7ad40764eb2e4ee868 Mon Sep 17 00:00:00 2001 From: DeniskaRediska Date: Fri, 29 Apr 2022 16:38:10 +0400 Subject: [PATCH] MSP-650 use isp-kit logger --- handler/handler.go | 40 ++++++++++++---------------------------- main.go | 27 +++++++++++++++++---------- server/http.go | 2 +- 3 files changed, 30 insertions(+), 39 deletions(-) diff --git a/handler/handler.go b/handler/handler.go index 84b5dad..75bce01 100644 --- a/handler/handler.go +++ b/handler/handler.go @@ -1,22 +1,19 @@ package handler import ( - "bytes" - "encoding/json" "fmt" "net/http" "time" + "github.com/integration-system/isp-kit/log" "github.com/integration-system/isp-lib/v2/config" "github.com/pkg/errors" "github.com/valyala/fasthttp" - "go.uber.org/zap" "google.golang.org/grpc/codes" "isp-gate-service/accounting" "isp-gate-service/authenticate" "isp-gate-service/conf" "isp-gate-service/domain" - "isp-gate-service/log" "isp-gate-service/proxy" "isp-gate-service/routing" "isp-gate-service/service" @@ -52,25 +49,23 @@ func (h Handler) CompleteRequest(ctx *fasthttp.RequestCtx) { service.Metrics.UpdateMethodResponseTime(path, executionTime) } - reqContentType := ctx.Request.Header.ContentType() - resContentType := ctx.Response.Header.ContentType() logEnable := config.GetRemote().(*conf.RemoteConfig).JournalSetting.Journal.Enable // nolintlint if logEnable && matcher.JournalMethods.Match(path) { requestBody, responseBody, err := resp.Get() - fields := []zap.Field{ - zap.ByteString("http_method", ctx.Method()), - zap.String("remote_addr", ctx.RemoteAddr().String()), - zap.ByteString("x_forwarded_for", ctx.Request.Header.Peek("X-Forwarded-For")), - zap.Int("status_code", ctx.Response.StatusCode()), - zap.String("path", path), - zap.Int32("application_id", appId), - zap.Int64("admin_id", adminId), - zap.Any("request", castData(requestBody, reqContentType)), - zap.Any("response", castData(responseBody, resContentType)), - zap.Error(err), + fields := []log.Field{ + log.ByteString("http_method", ctx.Method()), + log.String("remote_addr", ctx.RemoteAddr().String()), + log.ByteString("x_forwarded_for", ctx.Request.Header.Peek("X-Forwarded-For")), + log.Int("status_code", ctx.Response.StatusCode()), + log.String("path", path), + log.Int32("application_id", appId), + log.Int64("admin_id", adminId), + log.ByteString("request", requestBody), + log.ByteString("response", responseBody), } if err != nil { + fields = append(fields, log.Any("error", err)) h.logger.Error(ctx, "unsuccessful request", fields...) } else { h.logger.Info(ctx, "successful request", fields...) @@ -78,17 +73,6 @@ func (h Handler) CompleteRequest(ctx *fasthttp.RequestCtx) { } } -var jsonContentType = []byte("application/json") - -func castData(data []byte, contentType []byte) interface{} { - switch { - case bytes.Contains(contentType, jsonContentType): - return json.RawMessage(data) - default: - return fmt.Sprintf("%s", data) - } -} - func (h Handler) authenticateAccountingProxy(ctx *fasthttp.RequestCtx) (int32, int64, string, domain.ProxyResponse) { var ( appId int32 = -1 diff --git a/main.go b/main.go index d7c540f..1f70048 100644 --- a/main.go +++ b/main.go @@ -5,6 +5,7 @@ import ( "os" "github.com/integration-system/go-cmp/cmp" + "github.com/integration-system/isp-kit/log" "github.com/integration-system/isp-lib/v2/bootstrap" "github.com/integration-system/isp-lib/v2/config" "github.com/integration-system/isp-lib/v2/config/schema" @@ -17,7 +18,6 @@ import ( "isp-gate-service/accounting" "isp-gate-service/authenticate" "isp-gate-service/conf" - "isp-gate-service/log" "isp-gate-service/proxy" "isp-gate-service/redis" "isp-gate-service/repository" @@ -133,16 +133,23 @@ func setLogger(loggerCfg, oldLoggerCfg conf.JorunalConfig) *log.Adapter { var err error oldLogger := logger - if utils.DEV { - logger, err = log.New() - } else { - logger, err = log.New(log.WithFileRotation(log.Rotation{ - File: loggerCfg.Filename, - MaxSizeMb: loggerCfg.MaxSizeMb, - MaxBackups: maxBackups, - Compress: loggerCfg.Compress, - })) + loggerOpts := []log.Option{log.WithDevelopmentMode(), log.WithLevel(log.DebugLevel)} + isDev := utils.DEV + if !isDev { + loggerOpts = []log.Option{log.WithLevel(log.InfoLevel)} + logFilePath := loggerCfg.Filename + if logFilePath != "" { + rotation := log.Rotation{ + File: logFilePath, + MaxSizeMb: loggerCfg.MaxSizeMb, + MaxDays: 0, + MaxBackups: maxBackups, + Compress: loggerCfg.Compress, + } + loggerOpts = append(loggerOpts, log.WithFileRotation(rotation)) + } } + logger, err = log.New(loggerOpts...) if err != nil { logrus.Fatal(stdcodes.ModuleRunFatalError, errors.WithMessage(err, "set logger")) } diff --git a/server/http.go b/server/http.go index ffe3e09..1021b9a 100644 --- a/server/http.go +++ b/server/http.go @@ -4,12 +4,12 @@ import ( "sync" "time" + "github.com/integration-system/isp-kit/log" "github.com/integration-system/isp-lib/v2/config" logrus "github.com/integration-system/isp-log" "github.com/valyala/fasthttp" "isp-gate-service/conf" "isp-gate-service/handler" - "isp-gate-service/log" "isp-gate-service/log_code" )