Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 19 |
|
0.00% |
0 / 3 |
CRAP | |
0.00% |
0 / 1 |
StreamHandler | |
0.00% |
0 / 19 |
|
0.00% |
0 / 3 |
72 | |
0.00% |
0 / 1 |
__construct | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
12 | |||
getScriptParameter | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
6 | |||
getLogLevelParameter | |
0.00% |
0 / 12 |
|
0.00% |
0 / 1 |
12 |
1 | <?php |
2 | |
3 | /** |
4 | * This program is free software; you can redistribute it and/or |
5 | * modify it under the terms of the GNU General Public License |
6 | * as published by the Free Software Foundation; under version 2 |
7 | * of the License (non-upgradable). |
8 | * |
9 | * This program is distributed in the hope that it will be useful, |
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
12 | * GNU General Public License for more details. |
13 | * |
14 | * You should have received a copy of the GNU General Public License |
15 | * along with this program; if not, write to the Free Software |
16 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
17 | * |
18 | * Copyright (c) 2021 (original work) Open Assessment Technologies SA (under the project TAO-PRODUCT); |
19 | * |
20 | */ |
21 | |
22 | declare(strict_types=1); |
23 | |
24 | namespace oat\oatbox\log; |
25 | |
26 | use Monolog\Handler\StreamHandler as MonologStreamHandler; |
27 | use Monolog\Logger; |
28 | |
29 | /** |
30 | * Stores to any stream resource |
31 | * |
32 | * Provides the ability to use command line parameters --log-file and --log-level: |
33 | * |
34 | * if the --log-file parameter is specified, the path to the file for logging is taken from this parameter, |
35 | * if this parameter is not specified, the path to the file is taken from the system configuration |
36 | * for example: |
37 | * php index.php 'oat\taoTaskQueue\scripts\tools\RunWorker' --log-file /var/www/html/data/tao/log/tao-nccer.log |
38 | * |
39 | * if the --log-level parameter is specified, the minimum logging level is taken from this parameter, |
40 | * if this parameter is not specified, the minimum logging level is taken from the system configuration |
41 | * can take values DEBUG, INFO, NOTICE, WARNING, ERROR, CRITICAL, ALERT or EMERGENCY |
42 | * for example: |
43 | * php index.php 'oat\taoTaskQueue\scripts\tools\RunWorker' --log-level DEBUG |
44 | * |
45 | * @author Andrey Niahrou, <andrei.niahrou@taotesting.com> |
46 | */ |
47 | class StreamHandler extends MonologStreamHandler |
48 | { |
49 | public const PARAM_LOG_FILE = '--log-file'; |
50 | public const PARAM_LOG_LEVEL = '--log-level'; |
51 | |
52 | /** |
53 | * @param resource|string $defaultStream (Unless this parameter is specified on the command line as --log-file, |
54 | * otherwise it is ignored) resource where data will be output |
55 | * @param int $defaultLevel (Unless this parameter is specified on the command line as --log-level, otherwise it is |
56 | * ignored) The minimum logging level at which this handler will be triggered |
57 | * @param bool $bubble Whether the messages that are handled can bubble up the stack or not |
58 | * @param int|null $filePermission Optional file permissions (default (0644) are only for owner read/write) |
59 | * @param bool $useLocking Try to lock log file before doing any writes |
60 | * |
61 | * @throws \Exception If a missing directory is not buildable |
62 | * @throws \InvalidArgumentException If stream is not a resource or string |
63 | */ |
64 | public function __construct( |
65 | $defaultStream, |
66 | int $defaultLevel = Logger::DEBUG, |
67 | bool $bubble = true, |
68 | $filePermission = null, |
69 | bool $useLocking = false |
70 | ) { |
71 | $stream = $this->getScriptParameter(self::PARAM_LOG_FILE) ?: $defaultStream; |
72 | $logLevel = $this->getLogLevelParameter() ?: $defaultLevel; |
73 | parent::__construct($stream, $logLevel, $bubble, $filePermission, $useLocking); |
74 | } |
75 | |
76 | /** |
77 | * @param string $parameter |
78 | * @return string|null |
79 | */ |
80 | private function getScriptParameter(string $parameter): ?string |
81 | { |
82 | if (!in_array($parameter, $_SERVER['argv'])) { |
83 | return null; |
84 | } |
85 | |
86 | return $_SERVER['argv'][array_search($parameter, $_SERVER['argv']) + 1]; |
87 | } |
88 | |
89 | /** |
90 | * @return int|null |
91 | * @throws \Exception |
92 | */ |
93 | private function getLogLevelParameter(): ?int |
94 | { |
95 | $logLevelParameter = $this->getScriptParameter(self::PARAM_LOG_LEVEL); |
96 | if (!$logLevelParameter) { |
97 | return null; |
98 | } |
99 | |
100 | $errorLevels = Logger::getLevels(); |
101 | if (!isset($errorLevels[$logLevelParameter])) { |
102 | throw new \Exception( |
103 | sprintf( |
104 | 'Such log level doesn`t exist. Please, use one of: %s', |
105 | implode(', ', array_flip($errorLevels)) |
106 | ) |
107 | ); |
108 | } |
109 | |
110 | return $errorLevels[$logLevelParameter]; |
111 | } |
112 | } |