PageRenderTime 44ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

/phprojekt/library/Phprojekt/Log.php

https://github.com/aponto/PHProjekt
PHP | 132 lines | 40 code | 5 blank | 87 comment | 6 complexity | fe69944e24c91843082912f79cc762a9 MD5 | raw file
Possible License(s): LGPL-3.0, BSD-3-Clause, LGPL-2.0, MIT
  1. <?php
  2. /**
  3. * The file contains the log functions.
  4. *
  5. * This software is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU Lesser General Public
  7. * License version 3 as published by the Free Software Foundation
  8. *
  9. * This library is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * Lesser General Public License for more details.
  13. *
  14. * @category PHProjekt
  15. * @package Phprojekt
  16. * @subpackage Core
  17. * @copyright Copyright (c) 2010 Mayflower GmbH (http://www.mayflower.de)
  18. * @license LGPL v3 (See LICENSE file)
  19. * @link http://www.phprojekt.com
  20. * @since File available since Release 6.0
  21. * @version Release: @package_version@
  22. * @author Gustavo Solt <solt@mayflower.de>
  23. */
  24. /**
  25. * Manage an array with Zend_Log objects for loging each type of log in one distinct file.
  26. *
  27. * Since the Zend_Log use only one file for log everything in one big file,
  28. * we create an array with various Zend_Log objects,
  29. * each one, defined with a own log file and a own filter.
  30. *
  31. * The path to the log file is defined in the configuration.php file in the way:
  32. * log.debug.filename is for log DEBUG stuffs.
  33. * log.crit.filename is for log CRIT stuffs.
  34. * etc.
  35. *
  36. * The type defined for use are:
  37. * EMERG = Emergency: system is unusable.
  38. * ALERT = Alert: action must be taken immediately.
  39. * CRIT = Critical: critical conditions.
  40. * ERR = Error: error conditions.
  41. * WARN = Warning: warning conditions.
  42. * NOTICE = Notice: normal but significant condition.
  43. * INFO = Informational: informational messages.
  44. * DEBUG = Debug: debug messages.
  45. *
  46. * You can add in the configuration.php all of these types.
  47. * If the path to a log file is not defined, the class just drop the log.
  48. *
  49. * @category PHProjekt
  50. * @package Phprojekt
  51. * @subpackage Core
  52. * @copyright Copyright (c) 2010 Mayflower GmbH (http://www.mayflower.de)
  53. * @license LGPL v3 (See LICENSE file)
  54. * @link http://www.phprojekt.com
  55. * @since File available since Release 6.0
  56. * @version Release: @package_version@
  57. * @author Gustavo Solt <solt@mayflower.de>
  58. */
  59. class Phprojekt_Log extends Zend_Log
  60. {
  61. /**
  62. * An array of Zend_Log with priority filtering.
  63. *
  64. * @var array
  65. */
  66. protected $_loggers = array();
  67. /**
  68. * Constructor function.
  69. *
  70. * For all the defined filenames for log constant,
  71. * will create a Zend_Log object
  72. * with the path to the filename and a filter for these log.
  73. *
  74. * @param Zend_Config $config Object contain the user configuration.
  75. *
  76. * @return void
  77. */
  78. public function __construct(Zend_Config $config)
  79. {
  80. parent::__construct();
  81. $this->_loggers = array();
  82. if (isset($config->log)) {
  83. foreach ($config->log as $key => $val) {
  84. $constant = "self::" . strtoupper($key);
  85. if (defined($constant)) {
  86. $priority = constant($constant);
  87. $logger = new Zend_Log(new Zend_Log_Writer_Stream($val->filename));
  88. $logger->addFilter(new Zend_Log_Filter_Priority($priority));
  89. $this->_loggers[] = $logger;
  90. }
  91. }
  92. }
  93. }
  94. /**
  95. * Write the text into the file.
  96. *
  97. * For DEBUG log, is defined a special format.
  98. *
  99. * The message is passed to all Zend_Log instances saved in _loggers,
  100. * but they have priority filtering and therefore decide themself
  101. * if they pass the message to the file.
  102. *
  103. * @param string $message Text to write.
  104. * @param string $priority Type of log.
  105. *
  106. * @return void
  107. */
  108. public function log($message, $priority, $extras = null)
  109. {
  110. if ($priority >= Zend_Log::DEBUG) {
  111. $btrace = debug_backtrace();
  112. if (isset($btrace[3])) {
  113. if (!isset($btrace[3]['line'])) {
  114. $btrace[3]['line'] = '';
  115. }
  116. if (!isset($btrace[3]['class'])) {
  117. $btrace[3]['class'] = '';
  118. }
  119. $message = sprintf("%d %s::%s:\n %s\n", $btrace[3]['line'],
  120. $btrace[3]['class'], $btrace[3]['function'], $message);
  121. }
  122. }
  123. foreach ($this->_loggers as $logger) {
  124. $logger->log($message, $priority, $extras);
  125. }
  126. }
  127. }