laravel-websockets/src/Server/Loggers/Logger.php

126 lines
2.5 KiB
PHP
Raw Normal View History

2018-11-24 14:23:59 +00:00
<?php
2020-09-10 19:59:26 +00:00
namespace BeyondCode\LaravelWebSockets\Server\Loggers;
2018-11-24 14:23:59 +00:00
2018-11-26 21:25:52 +00:00
use Symfony\Component\Console\Formatter\OutputFormatterStyle;
2020-03-04 09:58:39 +00:00
use Symfony\Component\Console\Output\OutputInterface;
2018-11-24 14:23:59 +00:00
class Logger
{
2020-08-18 17:21:22 +00:00
/**
* The console output interface.
*
* @var \Symfony\Component\Console\Output\OutputInterface
*/
2018-11-24 14:23:59 +00:00
protected $consoleOutput;
2020-08-18 17:21:22 +00:00
/**
* Wether the logger is enabled.
*
* @var bool
*/
2018-11-24 14:23:59 +00:00
protected $enabled = false;
2020-08-18 17:21:22 +00:00
/**
* Wether the verbose mode is on.
*
* @var bool
*/
2018-11-24 14:23:59 +00:00
protected $verbose = false;
2020-08-18 17:21:22 +00:00
/**
* Check if the logger is active.
*
* @return bool
*/
2018-11-24 14:23:59 +00:00
public static function isEnabled(): bool
{
2020-09-10 19:59:26 +00:00
$logger = app(WebSocketsLogger::class);
return $logger->enabled;
2018-11-24 14:23:59 +00:00
}
2020-08-18 17:21:22 +00:00
/**
* Create a new Logger instance.
*
* @param \Symfony\Component\Console\Output\OutputInterface $consoleOutput
* @return void
*/
2018-11-24 14:23:59 +00:00
public function __construct(OutputInterface $consoleOutput)
{
$this->consoleOutput = $consoleOutput;
}
2020-08-18 17:21:22 +00:00
/**
* Enable the logger.
*
* @param bool $enabled
* @return $this
*/
2018-11-24 14:23:59 +00:00
public function enable($enabled = true)
{
$this->enabled = $enabled;
return $this;
}
2020-08-18 17:21:22 +00:00
/**
* Enable the verbose mode.
*
* @param bool $verbose
* @return $this
*/
2018-11-24 14:23:59 +00:00
public function verbose($verbose = false)
{
$this->verbose = $verbose;
return $this;
}
2020-08-18 17:21:22 +00:00
/**
* Trigger an Info message.
*
* @param string $message
* @return void
*/
2018-11-24 14:23:59 +00:00
protected function info(string $message)
{
$this->line($message, 'info');
}
2020-08-18 17:21:22 +00:00
/**
* Trigger a Warning message.
*
* @param string $message
* @return void
*/
2018-11-24 14:23:59 +00:00
protected function warn(string $message)
{
2018-11-26 21:25:52 +00:00
if (! $this->consoleOutput->getFormatter()->hasStyle('warning')) {
$style = new OutputFormatterStyle('yellow');
$this->consoleOutput->getFormatter()->setStyle('warning', $style);
}
2018-11-24 14:23:59 +00:00
$this->line($message, 'warning');
}
2020-08-18 17:21:22 +00:00
/**
* Trigger an Error message.
*
* @param string $message
* @return void
*/
2018-11-24 14:23:59 +00:00
protected function error(string $message)
{
$this->line($message, 'error');
}
protected function line(string $message, string $style)
{
2020-08-18 17:21:22 +00:00
$this->consoleOutput->writeln(
$style ? "<{$style}>{$message}</{$style}>" : $message
);
2018-11-24 14:23:59 +00:00
}
2018-12-04 21:22:33 +00:00
}