|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the smalex86\logger package. |
| 5 | + * |
| 6 | + * (c) Alexandr Smirnov <mail_er@mail.ru> |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view the LICENSE |
| 9 | + * file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | +namespace smalex86\logger\route; |
| 13 | + |
| 14 | +use smalex86\logger\Route; |
| 15 | + |
| 16 | +/** |
| 17 | + * ConsoleRoute with pid |
| 18 | + * |
| 19 | + * @author Alexandr Smirnov <mail_er@mail.ru> |
| 20 | + */ |
| 21 | +class ConsoleRouteWithPid extends Route { |
| 22 | + |
| 23 | + public $template = '{date} :: {pid} :: {level} :: {file}=>{line}' |
| 24 | + . ' :: {message} {context}'; |
| 25 | + |
| 26 | + /** |
| 27 | + * Method for suitable with PsrLogger. |
| 28 | + * Logs with an arbitrary level. |
| 29 | + * |
| 30 | + * @param mixed $level |
| 31 | + * @param string $message |
| 32 | + * @param array $context |
| 33 | + * @return bool |
| 34 | + */ |
| 35 | + public function log($level, $message, array $context = []): bool |
| 36 | + { |
| 37 | + // if level set in psrloglevel string |
| 38 | + if (is_string($level)) { |
| 39 | + $level = $this->getLogLevelFromRsrLogLevel($level); |
| 40 | + } |
| 41 | + // check for requirement of writing |
| 42 | + // it is determined by log maxLevel and msg level |
| 43 | + if ($this->maxLevel < $level) { |
| 44 | + return false; |
| 45 | + } |
| 46 | + // define msg status word |
| 47 | + $msgStatusWord = $this->getStatusWord($level); |
| 48 | + |
| 49 | + // pull file and line |
| 50 | + $fileLine = $this->getFileLine(); |
| 51 | + |
| 52 | + // put message to console |
| 53 | + echo strtr($this->template, [ |
| 54 | + '{date}' => $this->getDate(), |
| 55 | + '{pid}' => getmypid(), |
| 56 | + '{level}' => $msgStatusWord, |
| 57 | + '{file}' => $fileLine['file'], |
| 58 | + '{line}' => $fileLine['line'], |
| 59 | + '{message}' => $message, |
| 60 | + '{context}' => $this->contextStringify($context), |
| 61 | + ]) . PHP_EOL; |
| 62 | + return true; |
| 63 | + } |
| 64 | + |
| 65 | +} |
0 commit comments