Logger.php 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. <?php
  2. /**
  3. * @link http://www.yiiframework.com/
  4. * @copyright Copyright (c) 2008 Yii Software LLC
  5. * @license http://www.yiiframework.com/license/
  6. */
  7. namespace yii\swiftmailer;
  8. use Yii;
  9. /**
  10. * Logger is a SwiftMailer plugin, which allows passing of the SwiftMailer internal logs to the
  11. * Yii logging mechanism. Each native SwiftMailer log message will be converted into Yii 'info' log entry.
  12. *
  13. * In order to catch logs written by this class, you need to setup a log route for 'yii\swiftmailer\Logger::add' category.
  14. * For example:
  15. *
  16. * ~~~
  17. * 'log' => [
  18. * 'targets' => [
  19. * [
  20. * 'class' => 'yii\log\FileTarget',
  21. * 'categories' => ['yii\swiftmailer\Logger::add'],
  22. * ],
  23. * ],
  24. * ],
  25. * ~~~
  26. *
  27. * @author Paul Klimov <klimov.paul@gmail.com>
  28. * @since 2.0
  29. */
  30. class Logger implements \Swift_Plugins_Logger
  31. {
  32. /**
  33. * @inheritdoc
  34. */
  35. public function add($entry)
  36. {
  37. $categoryPrefix = substr($entry, 0, 2);
  38. switch ($categoryPrefix) {
  39. case '++':
  40. $level = \yii\log\Logger::LEVEL_TRACE;
  41. break;
  42. case '>>':
  43. case '<<':
  44. $level = \yii\log\Logger::LEVEL_INFO;
  45. break;
  46. case '!!':
  47. $level = \yii\log\Logger::LEVEL_WARNING;
  48. break;
  49. }
  50. if (!isset($level)) {
  51. $level = \yii\log\Logger::LEVEL_INFO;
  52. }
  53. Yii::getLogger()->log($entry, $level, __METHOD__);
  54. }
  55. /**
  56. * @inheritdoc
  57. */
  58. public function clear()
  59. {
  60. // do nothing
  61. }
  62. /**
  63. * @inheritdoc
  64. */
  65. public function dump()
  66. {
  67. return '';
  68. }
  69. }