Skip to content

Commit

Permalink
stream application logs to ES
Browse files Browse the repository at this point in the history
  • Loading branch information
moitran committed Jun 6, 2024
1 parent 8f37aa7 commit de7c4e0
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 7 deletions.
3 changes: 2 additions & 1 deletion .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -68,4 +68,5 @@ ELASTICSEARCH_PORT=9200
SCOUT_DRIVER=elastic
EXPLORER_ELASTIC_LOGGER_ENABLED=true

BOOK_DUMMY_DATA_TOTAL_RECORDS=1
BOOK_DUMMY_DATA_TOTAL_RECORDS=1000
ENABLE_REDIS_CACHE=true
8 changes: 8 additions & 0 deletions app/Http/Middleware/CacheMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Closure;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Log;
use Symfony\Component\HttpFoundation\Response;

class CacheMiddleware extends AbstractCacheMiddleware
Expand All @@ -19,6 +20,13 @@ class CacheMiddleware extends AbstractCacheMiddleware
public function handle(Request $request, Closure $next): Response
{
$response = $next($request);

if (!config('app.enable_redis_cache')) {
Log::warning('Disabled Redis cache');

return $response;
}

if ($response->getStatusCode() !== Response::HTTP_OK) {
return $response;
}
Expand Down
9 changes: 8 additions & 1 deletion app/Http/Middleware/CheckCacheMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@

namespace App\Http\Middleware;

use Cache;
use Closure;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Log;
use Symfony\Component\HttpFoundation\Response;

class CheckCacheMiddleware extends AbstractCacheMiddleware
Expand All @@ -16,6 +17,12 @@ class CheckCacheMiddleware extends AbstractCacheMiddleware
*/
public function handle(Request $request, Closure $next): Response
{
if (!config('app.enable_redis_cache')) {
Log::warning('Disabled Redis cache');

return $next($request);
}

$cacheKey = $this->generateCacheKey($request);
if (Cache::has($cacheKey)) {
return response(Cache::get($cacheKey));
Expand Down
30 changes: 30 additions & 0 deletions app/Logging/CreateElasticsearchLogger.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

namespace App\Logging;

use Elasticsearch\ClientBuilder;
use Monolog\Handler\ElasticsearchHandler;
use Monolog\Level;
use Monolog\Logger;

class CreateElasticsearchLogger
{
public function __invoke(array $config): Logger
{
$logger = new Logger('elasticsearch');
//create the client
$client = ClientBuilder::create()
->setHosts([$config['endpoint']])
->build();
//create the handler
$options = [
'index' => $config['index'],
'type' => '_doc',
];
$handler = new ElasticsearchHandler($client, $options, Level::Info, true);

$logger->setHandlers([$handler]);

return $logger;
}
}
1 change: 1 addition & 0 deletions config/app.php
Original file line number Diff line number Diff line change
Expand Up @@ -123,4 +123,5 @@
'store' => env('APP_MAINTENANCE_STORE', 'database'),
],

'enable_redis_cache' => env('ENABLE_REDIS_CACHE', true),
];
8 changes: 4 additions & 4 deletions config/logging.php
Original file line number Diff line number Diff line change
Expand Up @@ -128,10 +128,10 @@
],

'elasticsearch' => [
'driver' => 'daily',
'path' => storage_path('logs/elasticsearch.log'),
'level' => env('LOG_LEVEL', 'debug'),
'days' => 14,
'driver' => 'custom',
'via' => App\Logging\CreateElasticsearchLogger::class,
'index' => 'book_logs',
'endpoint' => env('ELASTICSEARCH_ENDPOINT', null),
],
],

Expand Down
1 change: 0 additions & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@ services:
- "ES_JAVA_OPTS=-Xms1g -Xmx1g"
- xpack.security.enabled=false
- xpack.security.enrollment.enabled=false
- "index.max_result_window=1000000"
ports:
- 9200:9200
volumes:
Expand Down

0 comments on commit de7c4e0

Please sign in to comment.