forked from Codeception/Codeception
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathLogger.php
More file actions
156 lines (133 loc) · 4.38 KB
/
Copy pathLogger.php
File metadata and controls
156 lines (133 loc) · 4.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
<?php
declare(strict_types=1);
namespace Codeception\Extension;
use Codeception\Event\FailEvent;
use Codeception\Event\StepEvent;
use Codeception\Event\SuiteEvent;
use Codeception\Event\TestEvent;
use Codeception\Events;
use Codeception\Exception\ConfigurationException;
use Codeception\Exception\ExtensionException;
use Codeception\Extension;
use Codeception\Test\Descriptor;
use Monolog\Formatter\LineFormatter;
use Monolog\Handler\RotatingFileHandler;
use function class_exists;
use function function_exists;
use function str_replace;
use function ucfirst;
/**
* Log suites/tests/steps using Monolog library.
* Monolog should be installed additionally by Composer.
*
* ```
* composer require monolog/monolog
* ```
*
* Codeception's core/internal stuff is logged into `tests/_output/codeception.log`.
* Test suites' steps are logged into `tests/_output/<test_full_name>-<rotation_date>.log`.
*
* To enable this module add to your `codeception.yml`:
*
* ``` yaml
* extensions:
* enabled: [Codeception\Extension\Logger]
* ```
*
* #### Config
*
* * `max_files` (default: 3) - how many log files to keep
*
*/
class Logger extends Extension
{
/**
* @var array<string, string>
*/
public static array $events = [
Events::SUITE_BEFORE => 'beforeSuite',
Events::TEST_BEFORE => 'beforeTest',
Events::TEST_AFTER => 'afterTest',
Events::TEST_END => 'endTest',
Events::STEP_BEFORE => 'beforeStep',
Events::TEST_FAIL => 'testFail',
Events::TEST_ERROR => 'testError',
Events::TEST_INCOMPLETE => 'testIncomplete',
Events::TEST_SKIPPED => 'testSkipped',
];
protected ?RotatingFileHandler $logHandler = null;
protected static ?\Monolog\Logger $logger = null;
protected ?string $path = null;
/**
* @var array<string, int>
*/
protected array $config = ['max_files' => 3];
public function _initialize(): void
{
if (!class_exists('\Monolog\Logger')) {
throw new ConfigurationException('Logger extension requires Monolog library to be installed');
}
$this->path = $this->getLogDir();
// internal log
$logHandler = new RotatingFileHandler($this->path . 'codeception.log', $this->config['max_files']);
$formatter = $logHandler->getFormatter();
if ($formatter instanceof LineFormatter) {
$formatter->ignoreEmptyContextAndExtra(true);
}
self::$logger = new \Monolog\Logger('Codeception');
self::$logger->pushHandler($logHandler);
}
public static function getLogger(): \Monolog\Logger
{
return self::$logger;
}
public function beforeSuite(SuiteEvent $event): void
{
$suiteLogFile = str_replace('\\', '_', $event->getSuite()->getName()) . '.log';
$this->logHandler = new RotatingFileHandler($this->path . $suiteLogFile, $this->config['max_files']);
}
public function beforeTest(TestEvent $event): void
{
self::$logger = new \Monolog\Logger(Descriptor::getTestFullName($event->getTest()));
self::$logger->pushHandler($this->logHandler);
self::$logger->info('------------------------------------');
self::$logger->info('STARTED: ' . ucfirst(Descriptor::getTestAsString($event->getTest())));
}
public function afterTest(TestEvent $event): void
{
}
public function endTest(TestEvent $event): void
{
self::$logger->info('PASSED');
}
public function testFail(FailEvent $event): void
{
self::$logger->alert($event->getFail()->getMessage());
self::$logger->info('# FAILED #');
}
public function testError(FailEvent $event): void
{
self::$logger->alert($event->getFail()->getMessage());
self::$logger->info('# ERROR #');
}
public function testSkipped(FailEvent $event): void
{
self::$logger->info('# Skipped #');
}
public function testIncomplete(FailEvent $event): void
{
self::$logger->info('# Incomplete #');
}
public function beforeStep(StepEvent $event): void
{
self::$logger->info((string) $event->getStep());
}
}
if (!function_exists('codecept_log')) {
function codecept_log(): \Monolog\Logger
{
return Logger::getLogger();
}
} else {
throw new ExtensionException(Logger::class, "function 'codecept_log' already defined");
}