2018-11-24 14:23:59 +00:00
|
|
|
<?php
|
|
|
|
|
|
2025-01-16 07:54:02 +00:00
|
|
|
namespace BlaxSoftware\LaravelWebSockets\Server\Loggers;
|
2018-11-24 14:23:59 +00:00
|
|
|
|
2026-04-16 06:17:39 +00:00
|
|
|
use Illuminate\Support\Facades\Log;
|
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
|
|
|
/**
|
2026-04-16 06:17:39 +00:00
|
|
|
* Whether the logger is enabled.
|
2020-08-18 17:21:22 +00:00
|
|
|
*
|
|
|
|
|
* @var bool
|
|
|
|
|
*/
|
2018-11-24 14:23:59 +00:00
|
|
|
protected $enabled = false;
|
|
|
|
|
|
2020-08-18 17:21:22 +00:00
|
|
|
/**
|
2026-04-16 06:17:39 +00:00
|
|
|
* Whether the verbose mode is on.
|
2020-08-18 17:21:22 +00:00
|
|
|
*
|
|
|
|
|
* @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');
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-16 06:17:39 +00:00
|
|
|
/**
|
|
|
|
|
* Write a message to the console and persist it to the websocket log file.
|
|
|
|
|
*/
|
2018-11-24 14:23:59 +00:00
|
|
|
protected function line(string $message, string $style)
|
|
|
|
|
{
|
2026-04-16 06:17:39 +00:00
|
|
|
// Console output (existing behavior)
|
2020-08-18 17:21:22 +00:00
|
|
|
$this->consoleOutput->writeln(
|
|
|
|
|
$style ? "<{$style}>{$message}</{$style}>" : $message
|
|
|
|
|
);
|
2026-04-16 06:17:39 +00:00
|
|
|
|
|
|
|
|
// Also persist to log file so errors are visible outside the console
|
|
|
|
|
$this->fileLog($style, $message);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Write a message to the websocket log channel.
|
|
|
|
|
* Uses the 'websocket' channel if available, falls back to the default.
|
|
|
|
|
*/
|
|
|
|
|
protected function fileLog(string $level, string $message): void
|
|
|
|
|
{
|
|
|
|
|
// Map console styles to log levels
|
|
|
|
|
$logLevel = match ($level) {
|
|
|
|
|
'error' => 'error',
|
|
|
|
|
'warning' => 'warning',
|
|
|
|
|
default => 'info',
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
$channel = config('logging.channels.websocket') ? 'websocket' : null;
|
|
|
|
|
Log::channel($channel)->log($logLevel, '[WebSocket] '.$message);
|
|
|
|
|
} catch (\Throwable) {
|
|
|
|
|
// Logging must never crash the WS server
|
|
|
|
|
}
|
2018-11-24 14:23:59 +00:00
|
|
|
}
|
2018-12-04 21:22:33 +00:00
|
|
|
}
|