diff --git a/assembly/locator.go b/assembly/locator.go index 60865b1..15f154d 100644 --- a/assembly/locator.go +++ b/assembly/locator.go @@ -47,7 +47,7 @@ func NewLocator( } } -func (l Locator) Handler(config conf.Remote, locations []conf.Location, redisCli redis.UniversalClient) (http.Handler, error) { +func (l Locator) Handler(config conf.Remote, locations []conf.Location, redisCli redis.UniversalClient) (http.Handler, error) { // nolint:funlen systemRepo := repository.NewSystem(l.systemCli) adminRepo := repository.NewAdmin(l.adminCli) @@ -93,8 +93,12 @@ func (l Locator) Handler(config conf.Remote, locations []conf.Location, redisCli handler := middleware.Chain( proxyFunc, middleware.RequestId(config.EnableClientRequestIdForwarding), - middleware.Logger(l.logger, config.Logging.RequestLogEnable, enableBodyLog, - config.Logging.SkipBodyLoggingEndpointPrefixes), + middleware.Logger( + l.logger, config.Logging.RequestLogEnable, + enableBodyLog, + config.Logging.SkipBodyLoggingEndpointPrefixes, + config.Logging.EnableForceUnescapingUnicode, + ), middleware.ErrorHandler(l.logger), middleware.Authenticate(authentication), middleware.AdminAuthenticate(adminService), @@ -107,8 +111,11 @@ func (l Locator) Handler(config conf.Remote, locations []conf.Location, redisCli handler = middleware.Chain( proxyFunc, middleware.RequestId(config.EnableClientRequestIdForwarding), - middleware.Logger(l.logger, config.Logging.RequestLogEnable, enableBodyLog, - config.Logging.SkipBodyLoggingEndpointPrefixes), + middleware.Logger(l.logger, config.Logging.RequestLogEnable, + enableBodyLog, + config.Logging.SkipBodyLoggingEndpointPrefixes, + config.Logging.EnableForceUnescapingUnicode, + ), middleware.ErrorHandler(l.logger), ) } diff --git a/conf/default_remote_config.json b/conf/default_remote_config.json index 8860eb2..7f0bd99 100644 --- a/conf/default_remote_config.json +++ b/conf/default_remote_config.json @@ -3,7 +3,8 @@ "logLevel": "debug", "requestLogEnable": true, "bodyLogEnable": false, - "skipBodyLoggingEndpointPrefixes": [] + "skipBodyLoggingEndpointPrefixes": [], + "enableForceUnescapingUnicode": false }, "caching": { "authenticationDataInSec": 60, diff --git a/conf/remote.go b/conf/remote.go index ca64eb9..b022d96 100644 --- a/conf/remote.go +++ b/conf/remote.go @@ -37,6 +37,7 @@ type Logging struct { RequestLogEnable bool `schema:"Включить логирование запросов"` BodyLogEnable bool `schema:"Включить логирование тел запросов и ответов,должно быть включено логирование запросов"` SkipBodyLoggingEndpointPrefixes []string `schema:"регулярные выражения для отключения логирования"` + EnableForceUnescapingUnicode bool `schema:"Включить перевод тел запроса из unicode в utf-8, должно быть включено логирование тел запросов и ответов"` } type Caching struct { diff --git a/middleware/logger.go b/middleware/logger.go index 2667c99..335b59f 100644 --- a/middleware/logger.go +++ b/middleware/logger.go @@ -3,6 +3,7 @@ package middleware import ( "bufio" "bytes" + "github.com/txix-open/isp-kit/json" "io" "net" "net/http" @@ -43,11 +44,27 @@ func (w *writerWrapper) WriteHeader(statusCode int) { w.ResponseWriter.WriteHeader(statusCode) } -func Logger( +func forceUnescapingUnicode(data []byte) []byte { + var body any + err := json.Unmarshal(data, &body) + if err != nil { + return data + } + + newData, err := json.Marshal(body) + if err != nil { + return data + } + + return newData +} + +func Logger( // nolint:gocognit logger log.Logger, enableRequestLogging bool, enableBodyLogging bool, skipBodyLoggingEndpointPrefixes []string, + enableForceUnescapingUnicode bool, ) Middleware { return func(next Handler) Handler { return HandlerFunc(func(ctx *request.Context) error { @@ -108,10 +125,13 @@ func Logger( } if logBodyFromCurrenRequest { - fields = append(fields, - log.ByteString("request", buf.RequestBody()), - log.ByteString("response", buf.ResponseBody()), - ) + if enableForceUnescapingUnicode && strings.Contains(string(buf.RequestBody()), "\\u") { + fields = append(fields, log.ByteString("request", forceUnescapingUnicode(buf.RequestBody()))) + } else { + fields = append(fields, log.ByteString("request", buf.RequestBody())) + } + + fields = append(fields, log.ByteString("response", buf.ResponseBody())) } logger.Debug(ctx.Context(), "log request", fields...)