MSP-650 use isp-kit logger
This commit is contained in:
1 parent
1fdc36d797
commit
0f002b5633
3 files changed
+30
-39
No files matched your search
+12
-28
@@ -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
|
||||
|
||||
@@ -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"))
|
||||
}
|
||||
|
||||
+1
-1
@@ -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"
|
||||
)
|
||||
|
||||
|
||||
Reference in new issue
Block a user