Skip to content

Commit ede9a15

Browse files
authored
Merge pull request #19 from smalex86/develop
Develop
2 parents 18c67cb + 0f31acc commit ede9a15

File tree

2 files changed

+70
-0
lines changed

2 files changed

+70
-0
lines changed

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ Simple logging system suitable with PsrLog.
77
* route:
88
* CachedFileRoute - route for file logging with cache using
99
* ConsoleRoute - route for output log info console (for cli mode)
10+
* ConsoleRouteWithPid - route for output log info console with process id info (for cli mode)
1011
* DatabaseRoute - route for database logging
1112
* FileRoute - ex SimpleLogger, route for file logging
1213
* FileRouteWithPid - route for file logging with print of process id into log message
@@ -55,6 +56,10 @@ $logger->routeList->attach(new smalex86\logger\route\ConsoleRoute([
5556
'isEnabled' => true,
5657
'maxLevel' => 7
5758
]));
59+
$logger->routeList->attach(new smalex86\logger\route\ConsoleRouteWithPid([
60+
'isEnabled' => true,
61+
'maxLevel' => 7
62+
]));
5863
$logger->routeList->attach(new smalex86\logger\route\DatabaseRoute([
5964
'isEnabled' => true,
6065
'maxLevel' => 6,

src/route/ConsoleRouteWithPid.php

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
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

Comments
 (0)