Skip to content

Instantly share code, notes, and snippets.

@markomitranic
Forked from laverboy/LogUse.php
Last active February 16, 2020 12:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save markomitranic/406981111e95ad6102df5900de3d8a4e to your computer and use it in GitHub Desktop.
Save markomitranic/406981111e95ad6102df5900de3d8a4e to your computer and use it in GitHub Desktop.
A simple, static, logging singleton class
<?php
use Monolog\Handler\StreamHandler;
use Monolog\Logger;
use Psr\Log\LoggerInterface;
final class Log {
/** @var string */
private const OUTPUT_STREAM = 'php://stderr';
/**
* @var LoggerInterface
*/
protected static $instance;
public static function getLogger(): LoggerInterface
{
if (!self::$instance) {
self::configureInstance();
}
return self::$instance;
}
protected static function configureInstance(): LoggerInterface
{
$logger = new Logger('log');
$logger->pushHandler(new StreamHandler(self::OUTPUT_STREAM, self::getLoggingLevel()));
self::$instance = $logger;
return self::$instance;
}
public static function getLoggingLevel(): int
{
if (getenv('LOGGING_LEVEL') === 'debug') {
return Logger::DEBUG;
}
return Logger::ERROR;
}
public static function debug(string $message, array $context = []): void
{
self::getLogger()->debug($message, $context);
}
public static function info(string $message, array $context = []): void
{
self::getLogger()->info($message, $context);
}
public static function notice(string $message, array $context = []): void
{
self::getLogger()->notice($message, $context);
}
public static function warning(string $message, array $context = []): void
{
self::getLogger()->warning($message, $context);
}
public static function error(string $message, array $context = []): void
{
self::getLogger()->error($message, $context);
}
public static function critical(string $message, array $context = []): void
{
self::getLogger()->critical($message, $context);
}
public static function alert(string $message, array $context = []): void
{
self::getLogger()->alert($message, $context);
}
public static function emergency(string $message, array $context = []): void
{
self::getLogger()->emergency($message, $context);
}
}
<?php
Log::error("Error state.", ['exception' => $e, 'client' => $conn->resourceId]);
Log::info("Client left.", ['client' => $conn->resourceId]);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment