opcodesio / log-viewer

Fast and beautiful Log Viewer for Laravel
https://log-viewer.opcodes.io
MIT License
3.49k stars 250 forks source link

Malformed UTF-8 characters, possibly incorrectly encoded Error #318

Closed ThatCoolGuyyy closed 7 months ago

ThatCoolGuyyy commented 8 months ago

Discussed in https://github.com/opcodesio/log-viewer/discussions/317

Originally posted by **ThatCoolGuyyy** January 25, 2024 Hi everyone I'm having issues viewing logs on my Laravel app hosted on Elastic Beanstalk, Whenever I try to access my Nginx error logs. I get an error on my Laravel logs with a stack trace pointing to an encoding error on this package. The package version is * v3.1.11 Please help ``` Malformed UTF-8 characters, possibly incorrectly encoded Context: { "exception": "[object] (InvalidArgumentException(code: 0): Malformed UTF-8 characters, possibly incorrectly encoded at /var/app/current/vendor/laravel/framework/src/Illuminate/Http/JsonResponse.php:88) [stacktrace] #0 /var/app/current/vendor/symfony/http-foundation/JsonResponse.php(49): Illuminate\\Http\\JsonResponse->setData(Array) #1 /var/app/current/vendor/laravel/framework/src/Illuminate/Http/JsonResponse.php(32): Symfony\\Component\\HttpFoundation\\JsonResponse->__construct(Array, 200, Array, false) #2 /var/app/current/vendor/laravel/framework/src/Illuminate/Routing/ResponseFactory.php(101): Illuminate\\Http\\JsonResponse->__construct(Array, 200, Array, 0) #3 /var/app/current/vendor/opcodesio/log-viewer/src/Http/Controllers/LogsController.php(107): Illuminate\\Routing\\ResponseFactory->json(Array) #4 /var/app/current/vendor/laravel/framework/src/Illuminate/Routing/ControllerDispatcher.php(46): Opcodes\\LogViewer\\Http\\Controllers\\LogsController->index(Object(Illuminate\\Http\\Request)) #5 /var/app/current/vendor/laravel/framework/src/Illuminate/Routing/Route.php(260): Illuminate\\Routing\\ControllerDispatcher->dispatch(Object(Illuminate\\Routing\\Route), Object(Opcodes\\LogViewer\\Http\\Controllers\\LogsController), 'index') #6 /var/app/current/vendor/laravel/framework/src/Illuminate/Routing/Route.php(205): Illuminate\\Routing\\Route->runController() #7 /var/app/current/vendor/laravel/framework/src/Illuminate/Routing/Router.php(806): Illuminate\\Routing\\Route->run() #8 /var/app/current/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(144): Illuminate\\Routing\\Router->Illuminate\\Routing\\{closure}(Object(Illuminate\\Http\\Request)) #9 /var/app/current/vendor/opcodesio/log-viewer/src/Http/Middleware/JsonResourceWithoutWrappingMiddleware.php(17): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}(Object(Illuminate\\Http\\Request)) #10 /var/app/current/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(183): Opcodes\\LogViewer\\Http\\Middleware\\JsonResourceWithoutWrappingMiddleware->handle(Object(Illuminate\\Http\\Request), Object(Closure)) #11 /var/app/current/vendor/opcodesio/log-viewer/src/Http/Middleware/ForwardRequestToHostMiddleware.php(50): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}(Object(Illuminate\\Http\\Request)) #12 /var/app/current/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(183): Opcodes\\LogViewer\\Http\\Middleware\\ForwardRequestToHostMiddleware->handle(Object(Illuminate\\Http\\Request), Object(Closure)) #13 /var/app/current/vendor/opcodesio/log-viewer/src/Http/Middleware/AuthorizeLogViewer.php(13): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}(Object(Illuminate\\Http\\Request)) #14 /var/app/current/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(183): Opcodes\\LogViewer\\Http\\Middleware\\AuthorizeLogViewer->handle(Object(Illuminate\\Http\\Request), Object(Closure)) #15 /var/app/current/vendor/opcodesio/log-viewer/src/Http/Middleware/EnsureFrontendRequestsAreStateful.php(38): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}(Object(Illuminate\\Http\\Request)) #16 /var/app/current/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(144): Opcodes\\LogViewer\\Http\\Middleware\\EnsureFrontendRequestsAreStateful->Opcodes\\LogViewer\\Http\\Middleware\\{closure}(Object(Illuminate\\Http\\Request)) #17 /var/app/current/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(119): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}(Object(Illuminate\\Http\\Request)) #18 /var/app/current/vendor/opcodesio/log-viewer/src/Http/Middleware/EnsureFrontendRequestsAreStateful.php(39): Illuminate\\Pipeline\\Pipeline->then(Object(Closure)) #19 /var/app/current/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(183): Opcodes\\LogViewer\\Http\\Middleware\\EnsureFrontendRequestsAreStateful->handle(Object(Illuminate\\Http\\Request), Object(Closure)) #20 /var/app/current/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(119): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}(Object(Illuminate\\Http\\Request)) #21 /var/app/current/vendor/laravel/framework/src/Illuminate/Routing/Router.php(807): Illuminate\\Pipeline\\Pipeline->then(Object(Closure)) #22 /var/app/current/vendor/laravel/framework/src/Illuminate/Routing/Router.php(784): Illuminate\\Routing\\Router->runRouteWithinStack(Object(Illuminate\\Routing\\Route), Object(Illuminate\\Http\\Request)) #23 /var/app/current/vendor/laravel/framework/src/Illuminate/Routing/Router.php(748): Illuminate\\Routing\\Router->runRoute(Object(Illuminate\\Http\\Request), Object(Illuminate\\Routing\\Route)) #24 /var/app/current/vendor/laravel/framework/src/Illuminate/Routing/Router.php(737): Illuminate\\Routing\\Router->dispatchToRoute(Object(Illuminate\\Http\\Request)) #25 /var/app/current/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php(200): Illuminate\\Routing\\Router->dispatch(Object(Illuminate\\Http\\Request)) #26 /var/app/current/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(144): Illuminate\\Foundation\\Http\\Kernel->Illuminate\\Foundation\\Http\\{closure}(Object(Illuminate\\Http\\Request)) #27 /var/app/current/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/TransformsRequest.php(21): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}(Object(Illuminate\\Http\\Request)) #28 /var/app/current/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/ConvertEmptyStringsToNull.php(31): Illuminate\\Foundation\\Http\\Middleware\\TransformsRequest->handle(Object(Illuminate\\Http\\Request), Object(Closure)) #29 /var/app/current/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(183): Illuminate\\Foundation\\Http\\Middleware\\ConvertEmptyStringsToNull->handle(Object(Illuminate\\Http\\Request), Object(Closure)) #30 /var/app/current/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/TransformsRequest.php(21): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}(Object(Illuminate\\Http\\Request)) #31 /var/app/current/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/TrimStrings.php(40): Illuminate\\Foundation\\Http\\Middleware\\TransformsRequest->handle(Object(Illuminate\\Http\\Request), Object(Closure)) #32 /var/app/current/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(183): Illuminate\\Foundation\\Http\\Middleware\\TrimStrings->handle(Object(Illuminate\\Http\\Request), Object(Closure)) #33 /var/app/current/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/ValidatePostSize.php(27): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}(Object(Illuminate\\Http\\Request)) #34 /var/app/current/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(183): Illuminate\\Foundation\\Http\\Middleware\\ValidatePostSize->handle(Object(Illuminate\\Http\\Request), Object(Closure)) #35 /var/app/current/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/PreventRequestsDuringMaintenance.php(99): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}(Object(Illuminate\\Http\\Request)) #36 /var/app/current/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(183): Illuminate\\Foundation\\Http\\Middleware\\PreventRequestsDuringMaintenance->handle(Object(Illuminate\\Http\\Request), Object(Closure)) #37 /var/app/current/vendor/laravel/framework/src/Illuminate/Http/Middleware/HandleCors.php(49): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}(Object(Illuminate\\Http\\Request)) #38 /var/app/current/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(183): Illuminate\\Http\\Middleware\\HandleCors->handle(Object(Illuminate\\Http\\Request), Object(Closure)) #39 /var/app/current/vendor/laravel/framework/src/Illuminate/Http/Middleware/TrustProxies.php(39): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}(Object(Illuminate\\Http\\Request)) #40 /var/app/current/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(183): Illuminate\\Http\\Middleware\\TrustProxies->handle(Object(Illuminate\\Http\\Request), Object(Closure)) #41 /var/app/current/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(119): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}(Object(Illuminate\\Http\\Request)) #42 /var/app/current/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php(175): Illuminate\\Pipeline\\Pipeline->then(Object(Closure)) #43 /var/app/current/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php(144): Illuminate\\Foundation\\Http\\Kernel->sendRequestThroughRouter(Object(Illuminate\\Http\\Request)) #44 /var/app/current/public/index.php(52): Illuminate\\Foundation\\Http\\Kernel->handle(Object(Illuminate\\Http\\Request)) #45 {main} " } ```
arukompas commented 8 months ago

Hey @ThatCoolGuyyy

What is your configured cache driver? If you switch to file or redis for caching, does it help?

makao91 commented 7 months ago

I had the same problem and found a solution.

The most crucial thing in this story is that my logs have signs which has more than 1 byte (arabic signs).

The problem is here: Opcodes\LogViewer\Logs\LaravelLog.php line 38

$firstLineSplit = str_split($firstLine, 1000);

When last sign on splitted string represent non-1-byte sign, it throws this error.

Solution: Replace str_split to mb_str_split. Reason: str_split() will split into bytes, rather than characters when dealing with a multi-byte encoded string. Use mb_str_split() to split the string into code points

arukompas commented 7 months ago

hey @makao91 , that is super helpful! Working in mostly english language projects, I completely forgot about the multibyte string operation.

Thanks so much for finding the solution. Update pushed and tagged as v3.1.12.