| @@ 24-81 (lines=58) @@ | ||
| 21 | /** |
|
| 22 | * Class StartFormatter. |
|
| 23 | */ |
|
| 24 | class StartFormatter implements RequestStartInterface |
|
| 25 | { |
|
| 26 | use FormatterTrait; |
|
| 27 | ||
| 28 | /** |
|
| 29 | * @param callable|null $msg A callable used to create the message |
|
| 30 | * @param string $logLevel The level this should be logged at |
|
| 31 | * |
|
| 32 | * @return \Shrikeh\GuzzleMiddleware\TimerLogger\Formatter\StartFormatter |
|
| 33 | */ |
|
| 34 | public static function create( |
|
| 35 | callable $msg = null, |
|
| 36 | $logLevel = LogLevel::DEBUG |
|
| 37 | ) { |
|
| 38 | if (!$msg) { |
|
| 39 | $msg = new DefaultStartMessage(); |
|
| 40 | } |
|
| 41 | ||
| 42 | return new self($msg, $logLevel); |
|
| 43 | } |
|
| 44 | ||
| 45 | /** |
|
| 46 | * StartFormatter constructor. |
|
| 47 | * |
|
| 48 | * @param callable $msg A callable used to create the message |
|
| 49 | * @param callable|string $level The level this should be logged at |
|
| 50 | */ |
|
| 51 | private function __construct(callable $msg, $level = LogLevel::DEBUG) |
|
| 52 | { |
|
| 53 | $this->msg = $msg; |
|
| 54 | $this->level = $level; |
|
| 55 | } |
|
| 56 | ||
| 57 | /** |
|
| 58 | * {@inheritdoc} |
|
| 59 | */ |
|
| 60 | public function start(TimerInterface $timer, RequestInterface $request) |
|
| 61 | { |
|
| 62 | try { |
|
| 63 | return $this->msg($timer, $request); |
|
| 64 | } catch (Exception $e) { |
|
| 65 | $msg = 'Error attempting to parse for log'; |
|
| 66 | throw new FormatterStartException( |
|
| 67 | $msg, |
|
| 68 | FormatterStartException::MESSAGE_PARSE_EXCEPTION, |
|
| 69 | $e |
|
| 70 | ); |
|
| 71 | } |
|
| 72 | } |
|
| 73 | ||
| 74 | /** |
|
| 75 | * {@inheritdoc} |
|
| 76 | */ |
|
| 77 | public function levelStart(TimerInterface $timer, RequestInterface $request) |
|
| 78 | { |
|
| 79 | return $this->level($timer, $request); |
|
| 80 | } |
|
| 81 | } |
|
| 82 | ||
| @@ 25-88 (lines=64) @@ | ||
| 22 | /** |
|
| 23 | * Class StopFormatter. |
|
| 24 | */ |
|
| 25 | class StopFormatter implements RequestStopInterface |
|
| 26 | { |
|
| 27 | use FormatterTrait; |
|
| 28 | ||
| 29 | /** |
|
| 30 | * @param callable|null $msg A callable used to create the message |
|
| 31 | * @param string $logLevel The level this should be logged at |
|
| 32 | * |
|
| 33 | * @return \Shrikeh\GuzzleMiddleware\TimerLogger\Formatter\StopFormatter |
|
| 34 | */ |
|
| 35 | public static function create( |
|
| 36 | callable $msg = null, |
|
| 37 | $logLevel = LogLevel::DEBUG |
|
| 38 | ) { |
|
| 39 | if (!$msg) { |
|
| 40 | $msg = new DefaultStopMessage(); |
|
| 41 | } |
|
| 42 | ||
| 43 | return new self($msg, $logLevel); |
|
| 44 | } |
|
| 45 | ||
| 46 | /** |
|
| 47 | * StartFormatter constructor. |
|
| 48 | * |
|
| 49 | * @param callable $msg A callable to format the messages |
|
| 50 | * @param callable|string $level The log level for when the timer ends |
|
| 51 | */ |
|
| 52 | public function __construct(callable $msg, $level = LogLevel::DEBUG) |
|
| 53 | { |
|
| 54 | $this->msg = $msg; |
|
| 55 | $this->level = $level; |
|
| 56 | } |
|
| 57 | ||
| 58 | /** |
|
| 59 | * {@inheritdoc} |
|
| 60 | */ |
|
| 61 | public function stop( |
|
| 62 | TimerInterface $timer, |
|
| 63 | RequestInterface $request, |
|
| 64 | ResponseInterface $response |
|
| 65 | ) { |
|
| 66 | try { |
|
| 67 | return $this->msg($timer, $request, $response); |
|
| 68 | } catch (Exception $e) { |
|
| 69 | $msg = 'Error attempting to parse for log'; |
|
| 70 | throw new FormatterStopException( |
|
| 71 | $msg, |
|
| 72 | FormatterStopException::MESSAGE_PARSE_EXCEPTION, |
|
| 73 | $e |
|
| 74 | ); |
|
| 75 | } |
|
| 76 | } |
|
| 77 | ||
| 78 | /** |
|
| 79 | * {@inheritdoc} |
|
| 80 | */ |
|
| 81 | public function levelStop( |
|
| 82 | TimerInterface $timer, |
|
| 83 | RequestInterface $request, |
|
| 84 | ResponseInterface $response |
|
| 85 | ) { |
|
| 86 | return $this->level($timer, $request, $response); |
|
| 87 | } |
|
| 88 | } |
|
| 89 | ||