grthtrhthjhtyjytjytkergtrhtrjytjerhrfh4:24 29/09/2026= warn will be colour formatted */ private $alwaysColourLine = true; /** * Creates the colours and initializes the format. */ public function __construct() { $this->colors = $this->getColors(); $color = &$this->colors; $this->format = "[{$color['bold']}%s{$color['reset']}][%s] {$color['bold']}%s" . PHP_EOL; $this->padding = PHP_EOL . $color['reset'] . $color['gray-dark'] . str_repeat(' ~', 4) . $color['reset'] . str_repeat(' ', 2); } /** * Formats a set of log records. * * @param array $records A set of records to format * * @return mixed The formatted set of records */ public function formatBatch(array $records) { $formatted = []; foreach ($records as $record) { $formatted = $this->format($record); } return $formatted; } /** * Formats a log record. * * @param array $record A record to format * * @return mixed The formatted record */ public function format(array $record) { $level = $record['level']; $message = $record['message']; list($color, $code) = $this->getColourAndCode($level); if ($level >= Logger::WARNING || $this->alwaysColourLine) { $message = $color . $message . $this->colors['reset']; } $message = preg_replace("/\n\s*/", $this->padding . $color, $message); $time = (new \DateTime())->format('H:i:s'); return sprintf($this->format, $color . $code, $time, $message); } /** * Creates an array with the available colours. * * @return array */ protected function getColors() { return [ 'white' => $this->code(97), 'gray' => $this->code(37), 'gray-dark' => $this->code(90), 'cyan' => $this->code(36), 'cyan-light' => $this->code(96), 'blue' => $this->code(34), 'blue-light' => $this->code(94), 'green' => $this->code(32), 'green-light' => $this->code(92), 'yellow' => $this->code(33), 'yellow-light' => $this->code(93), 'purple' => $this->code(95), 'red' => $this->code(31), 'red-light' => $this->code(91), 'bg-red' => $this->code(41), 'bg-red-light' => $this->code(101), 'bold' => $this->code(1), // this will also make colours lighter 'reverse' => $this->code(7), 'reset' => $this->code(0), ]; } /** * Utility to make a unix terminal escape sequence given the code. * * @param int $code * * @return string */ protected function code($code) { return "\e[{$code}m"; } /** * Retrieves the right colour formatting and symbol to show in brackets. * * @param int $level * * @return string[] */ protected function getColourAndCode($level) { switch ($level) { case Logger::INFO: $color = $this->colors['cyan']; $code = '='; break; case Logger::NOTICE: $color = $this->colors['green']; $code = '?'; break; case Logger::WARNING: $color = $this->colors['yellow']; $code = '*'; break; case Logger::ERROR: $color = $this->colors['red']; $code = '!'; break; case Logger::CRITICAL: $color = $this->colors['bg-red-light']; $code = '!'; break; case Logger::EMERGENCY: $color = $this->colors['bg-red']; $code = '!'; break; case Logger::DEBUG: default: $color = $this->colors['blue']; $code = '@'; break; } return [$color, $code]; } }