Logger.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. <?php
  2. namespace Codeception\Extension;
  3. use Codeception\Event\FailEvent;
  4. use Codeception\Event\StepEvent;
  5. use Codeception\Event\SuiteEvent;
  6. use Codeception\Event\TestEvent;
  7. use Codeception\Events;
  8. use Codeception\Exception\ConfigurationException;
  9. use Codeception\Extension;
  10. use Codeception\Test\Descriptor;
  11. use Monolog\Handler\RotatingFileHandler;
  12. /**
  13. * Log suites/tests/steps using Monolog library.
  14. * Monolog should be installed additionally by Composer.
  15. *
  16. * ```
  17. * composer require monolog/monolog
  18. * ```
  19. *
  20. * Steps are logged into `tests/_output/codeception.log`
  21. *
  22. * To enable this module add to your `codeception.yml`:
  23. *
  24. * ``` yaml
  25. * extensions:
  26. * enabled: [Codeception\Extension\Logger]
  27. * ```
  28. *
  29. * #### Config
  30. *
  31. * * `max_files` (default: 3) - how many log files to keep
  32. *
  33. */
  34. class Logger extends Extension
  35. {
  36. public static $events = [
  37. Events::SUITE_BEFORE => 'beforeSuite',
  38. Events::TEST_BEFORE => 'beforeTest',
  39. Events::TEST_AFTER => 'afterTest',
  40. Events::TEST_END => 'endTest',
  41. Events::STEP_BEFORE => 'beforeStep',
  42. Events::TEST_FAIL => 'testFail',
  43. Events::TEST_ERROR => 'testError',
  44. Events::TEST_INCOMPLETE => 'testIncomplete',
  45. Events::TEST_SKIPPED => 'testSkipped',
  46. ];
  47. protected $logHandler;
  48. /**
  49. * @var \Monolog\Logger
  50. */
  51. protected $logger;
  52. protected $path;
  53. protected $config = ['max_files' => 3];
  54. public function _initialize()
  55. {
  56. if (!class_exists('\Monolog\Logger')) {
  57. throw new ConfigurationException("Logger extension requires Monolog library to be installed");
  58. }
  59. $this->path = $this->getLogDir();
  60. // internal log
  61. $logHandler = new RotatingFileHandler($this->path . 'codeception.log', $this->config['max_files']);
  62. $this->logger = new \Monolog\Logger('Codeception');
  63. $this->logger->pushHandler($logHandler);
  64. }
  65. public function beforeSuite(SuiteEvent $e)
  66. {
  67. $suite = str_replace('\\', '_', $e->getSuite()->getName());
  68. $this->logHandler = new RotatingFileHandler($this->path . $suite, $this->config['max_files']);
  69. }
  70. public function beforeTest(TestEvent $e)
  71. {
  72. $this->logger = new \Monolog\Logger(Descriptor::getTestFileName($e->getTest()));
  73. $this->logger->pushHandler($this->logHandler);
  74. $this->logger->info('------------------------------------');
  75. $this->logger->info("STARTED: " . ucfirst(Descriptor::getTestAsString($e->getTest())));
  76. }
  77. public function afterTest(TestEvent $e)
  78. {
  79. }
  80. public function endTest(TestEvent $e)
  81. {
  82. $this->logger->info("PASSED");
  83. }
  84. public function testFail(FailEvent $e)
  85. {
  86. $this->logger->alert($e->getFail()->getMessage());
  87. $this->logger->info("# FAILED #");
  88. }
  89. public function testError(FailEvent $e)
  90. {
  91. $this->logger->alert($e->getFail()->getMessage());
  92. $this->logger->info("# ERROR #");
  93. }
  94. public function testSkipped(FailEvent $e)
  95. {
  96. $this->logger->info("# Skipped #");
  97. }
  98. public function testIncomplete(FailEvent $e)
  99. {
  100. $this->logger->info("# Incomplete #");
  101. }
  102. public function beforeStep(StepEvent $e)
  103. {
  104. $this->logger->info((string) $e->getStep());
  105. }
  106. }