Commit a5c9f33e authored by Dominik Hebeler's avatar Dominik Hebeler
Browse files

added central log writer

parent f255d654
<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Redis;
use Log;
class AppendLogs extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'logs:gather';
const LOGKEY = "metager.logs";
/**
* The console command description.
*
* @var string
*/
protected $description = 'Retrieves all Log Entries from Redis and writes them to file';
/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*
* @return mixed
*/
public function handle()
{
$redis = Redis::connection('cache');
$elements = [];
$reply = $redis->pipeline(function ($pipe) use ($elements) {
$pipe->lrange(\App\Console\Commands\AppendLogs::LOGKEY, 0, -1);
$pipe->del(\App\Console\Commands\AppendLogs::LOGKEY);
});
$elements = $reply[0];
if (!is_array($elements) || sizeof($elements) <= 0) {
return;
}
if (file_put_contents(\App\MetaGer::getMGLogFile(), implode(PHP_EOL, $elements) . PHP_EOL, FILE_APPEND) === false) {
Log::error("Konnte Log Zeile(n) nicht schreiben");
$redis->lpush(array_reverse($elements));
} else {
Log::info("Added " . sizeof($elements) . " lines to todays log!");
}
}
}
......@@ -27,6 +27,7 @@ class Kernel extends ConsoleKernel
{
$schedule->command('requests:gather')->everyFifteenMinutes();
$schedule->command('requests:useragents')->everyFiveMinutes();
$schedule->command('logs:gather')->everyMinute();
$schedule->call(function () {
DB::table('monthlyrequests')->truncate();
......
......@@ -1392,10 +1392,12 @@ class MetaGer
$logEntry = preg_replace("/\n+/", " ", $logEntry);
$logpath = \App\MetaGer::getMGLogFile();
if (file_put_contents($logpath, $logEntry . PHP_EOL, FILE_APPEND) === false) {
Log::error("Konnte Log Zeile nicht schreiben");
}
Redis::connection('cache')->rpush(\App\Console\Commands\AppendLogs::LOGKEY, $logEntry);
/*$logpath = \App\MetaGer::getMGLogFile();
if (file_put_contents($logpath, $logEntry . PHP_EOL, FILE_APPEND) === false) {
Log::error("Konnte Log Zeile nicht schreiben");
}*/
} catch (\Exception $e) {
Log::error($e->getMessage());
return;
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment