Logger.php 3.6 KB

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