PageRenderTime 48ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/framework/components/Log.php

http://github.com/Shadez/Framework
PHP | 171 lines | 75 code | 24 blank | 72 comment | 12 complexity | cbbfd8ae2fac220b5783e6fa4fc38eb6 MD5 | raw file
Possible License(s): GPL-3.0
  1. <?php
  2. /**
  3. * Copyright (C) 2011-2012 Shadez <https://github.com/Shadez/Framework>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  18. **/
  19. class Log extends Component
  20. {
  21. /**
  22. * Log filename
  23. * @access private
  24. * @var string
  25. **/
  26. private $m_logFile = '';
  27. /**
  28. * Log level
  29. * @access private
  30. * @var int
  31. **/
  32. private $m_logLevel = 1;
  33. /**
  34. * Is logging enabled?
  35. * @access private
  36. * @var bool
  37. **/
  38. private $m_isEnabled = true;
  39. public function initialize()
  40. {
  41. $this->m_isEnabled = $this->c('Config')->getValue('logging.enabled');
  42. if (!$this->m_isEnabled)
  43. return $this;
  44. $this->m_logFile = STATIC_DIR . '_debug' . DS . 'tmp.dbg';
  45. $this->m_logLevel = $this->c('Config')->getValue('logging.level');
  46. return $this;
  47. }
  48. /**
  49. * Adds some lines to debug message
  50. * @param array $args
  51. * @param string $type
  52. * @return void
  53. **/
  54. private function addLines($args, $type)
  55. {
  56. if (!$this->m_isEnabled)
  57. return;
  58. $log = $this->applyStyle($type);
  59. $text = call_user_func_array('sprintf', $args);
  60. $log .= $text . '<br />' . NL;
  61. $this->writeData($log);
  62. }
  63. /**
  64. * Writes error message to log file (if allowed)
  65. * @param string $message
  66. * @param ...
  67. * @return void
  68. **/
  69. public function writeError($message)
  70. {
  71. if (!$this->m_isEnabled)
  72. return;
  73. $args = func_get_args();
  74. $this->addLines($args, 'error');
  75. }
  76. /**
  77. * Writes component log message to log file (if allowed)
  78. * @param string $message
  79. * @param ...
  80. * @return void
  81. **/
  82. public function writeComponent($message)
  83. {
  84. if (!$this->m_isEnabled || $this->m_logLevel < 4)
  85. return;
  86. $args = func_get_args();
  87. $this->addLines($args, 'component');
  88. }
  89. /**
  90. * Writes debug message to log file (if allowed)
  91. * @param string $message
  92. * @param ...
  93. * @return void
  94. **/
  95. public function writeDebug($message)
  96. {
  97. if (!$this->m_isEnabled || $this->m_logLevel < 2)
  98. return;
  99. $args = func_get_args();
  100. $this->addLines($args, 'debug');
  101. }
  102. /**
  103. * Writes sql log message to log file (if allowed)
  104. * @param string $message
  105. * @param ...
  106. * @return void
  107. **/
  108. public function writeSql($message)
  109. {
  110. if (!$this->m_isEnabled || $this->m_logLevel < 3)
  111. return;
  112. $args = func_get_args();
  113. $this->addLines($args, 'sql');
  114. }
  115. /**
  116. * Applies HTML style to log message
  117. * @param string $type
  118. * @return string
  119. **/
  120. private function applyStyle($type)
  121. {
  122. if (!$this->m_isEnabled)
  123. return;
  124. $date = date('d-m-Y H:i:s');
  125. switch ($type)
  126. {
  127. case 'error':
  128. case 'debug':
  129. case 'sql':
  130. case 'component':
  131. return '<strong>' . strtoupper($type) . '</strong> [' . $date . ']: ';
  132. default:
  133. return '';
  134. }
  135. }
  136. /**
  137. * Writes new log message to log file
  138. * @param string $data
  139. * @return void
  140. **/
  141. private function writeData($data)
  142. {
  143. if (!$this->m_isEnabled)
  144. return;
  145. file_put_contents($this->m_logFile, $data, FILE_APPEND);
  146. }
  147. };