Skip to content

Commit

Permalink
UPdate log
Browse files Browse the repository at this point in the history
  • Loading branch information
oleksandr-diudiun committed Jul 4, 2022
1 parent 28ae703 commit 8938691
Showing 1 changed file with 51 additions and 24 deletions.
75 changes: 51 additions & 24 deletions src/Log.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
<?php

//TODO: push
declare(strict_types=1);

namespace Memcrab\Log;

use Monolog\Logger;
use Monolog\Handler\SqsHandler;
use Monolog\Handler\StreamHandler;
use Monolog\Handler\RotatingFileHandler;
use Monolog\Formatter\LineFormatter;
use Monolog\Formatter\JsonFormatter;

class Log extends Logger
{
Expand All @@ -16,6 +21,16 @@ public static function obj(): self
{
if (!isset(self::$instance) || !(self::$instance instanceof Log)) {
self::$instance = new self('log');

\register_shutdown_function("\Memcrab\Log\Log::shutdown");

# this part need to be implement with Monolog Signals Registration as a tool in monolog reop [14.06.22]
// if(function_exists('pcntl_signal')) {
// pcntl_signal(SIGTERM, "\Memcrab\Log\Log::shutdown");
// pcntl_signal(SIGUSR1, "\Memcrab\Log\Log::shutdown");
// } else {
// error_log("pcntl_signal not available please install pcntl php Module");
// }
}
return self::$instance;
}
Expand All @@ -32,39 +47,51 @@ public static function contextProcessor($record)
return $record;
}

public static function registerShutdownFunction(array $additionalShutdownFunctions = []): void
{
foreach ($additionalShutdownFunctions as $function) {
register_shutdown_function($function);
}
register_shutdown_function("\Memcrab\Log\Log::shutdown");

# this part need to be implement with Monolog Signals Registration as a tool in monolog reop [14.06.22]
// if(function_exists('pcntl_signal')) {
// pcntl_signal(SIGTERM, "\Memcrab\Log\Log::shutdown");
// pcntl_signal(SIGUSR1, "\Memcrab\Log\Log::shutdown");
// } else {
// error_log("pcntl_signal not available please install pcntl php Module");
// }
}

public static function setServiceContext(
string $project,
string $service,
string $environment,
bool $DEBUG_MODE,
string $hostname,
string $ip,
string $os
bool $DEBUG_MODE
): void {
self::$context = [
'project' => $project,
'service' => $service,
'environment' => $environment,
'DEBUG_MODE' => $DEBUG_MODE,
'hostname' => $hostname,
'ip' => $ip,
'os' => $os
'hostname' => gethostname(),
'ip' => gethostbyname(gethostname()),
'os' => php_uname('s') . " " . php_uname('r')
];
}

private function registerHandler($type, $value): void
{
switch ($type) {
case 'StreamHandler':
$Handler = new StreamHandler($value, Log::DEBUG);
$Handler->setFormatter(new LineFormatter(null, null, true, true));
break;
case 'RotatingFileHandler':
$Handler = new RotatingFileHandler($value, 5, Log::DEBUG);
$Handler->setFormatter(new LineFormatter(null, null, true, true));
break;
case 'SqsHandler':
$value->registerQueue('logs', ['ReceiveMessageWaitTimeSeconds' => 20]);
$Handler = new SqsHandler($value->client(), $value->getQueueUrl('logs'));
$Handler->setFormatter(new JsonFormatter());
$Handler->pushProcessor('\Memcrab\Log\Log::contextProcessor');
break;
default:
throw new \Exception($type . ' Handler scenario undefined. Please provide your own logic.', 500);
break;
}
$this->pushHandler($Handler);
}

public function setLogHandlersBasedOnDebugMode(bool $DEBUG_MODE, array $debugHandlers, array $releaseHandler): void
{
foreach (($DEBUG_MODE) ? $debugHandlers : $releaseHandler as $key => $value) {
$this->registerHandler($key, $value);
}
}
}

0 comments on commit 8938691

Please sign in to comment.