Logger.php 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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. * This logger will be automatically created and applied to underlying [[\Swift_Mailer]] instance, if [[Mailer::$enableSwiftMailerLogging]]
  14. * is enabled. For example:
  15. *
  16. * ```php
  17. * [
  18. * 'components' => [
  19. * 'mailer' => [
  20. * 'class' => 'yii\swiftmailer\Mailer',
  21. * 'enableSwiftMailerLogging' => true,
  22. * ],
  23. * ],
  24. * // ...
  25. * ],
  26. * ```
  27. *
  28. *
  29. * In order to catch logs written by this class, you need to setup a log route for 'yii\swiftmailer\Logger::add' category.
  30. * For example:
  31. *
  32. * ```php
  33. * [
  34. * 'components' => [
  35. * 'log' => [
  36. * 'targets' => [
  37. * [
  38. * 'class' => 'yii\log\FileTarget',
  39. * 'categories' => ['yii\swiftmailer\Logger::add'],
  40. * ],
  41. * ],
  42. * ],
  43. * // ...
  44. * ],
  45. * // ...
  46. * ],
  47. * ```
  48. *
  49. * @author Paul Klimov <klimov.paul@gmail.com>
  50. * @since 2.0.4
  51. */
  52. class Logger implements \Swift_Plugins_Logger
  53. {
  54. /**
  55. * @inheritdoc
  56. */
  57. public function add($entry)
  58. {
  59. $categoryPrefix = substr($entry, 0, 2);
  60. switch ($categoryPrefix) {
  61. case '++':
  62. $level = \yii\log\Logger::LEVEL_TRACE;
  63. break;
  64. case '>>':
  65. case '<<':
  66. $level = \yii\log\Logger::LEVEL_INFO;
  67. break;
  68. case '!!':
  69. $level = \yii\log\Logger::LEVEL_WARNING;
  70. break;
  71. default:
  72. $level = \yii\log\Logger::LEVEL_INFO;
  73. }
  74. Yii::getLogger()->log($entry, $level, __METHOD__);
  75. }
  76. /**
  77. * @inheritdoc
  78. */
  79. public function clear()
  80. {
  81. // do nothing
  82. }
  83. /**
  84. * @inheritdoc
  85. */
  86. public function dump()
  87. {
  88. return '';
  89. }
  90. }