PageRenderTime 48ms CodeModel.GetById 23ms RepoModel.GetById 1ms app.codeStats 0ms

/src/PhoolKit/Log.php

https://bitbucket.org/kayahr/phoolkit
PHP | 205 lines | 85 code | 18 blank | 102 comment | 25 complexity | 06bca34dbe292407706280c84cb92627 MD5 | raw file
  1. <?php
  2. /*
  3. * PhoolKit - A PHP toolkit.
  4. * Copyright (C) 2011 Klaus Reimer <k@ailis.de>
  5. *
  6. * This library is free software: you can redistribute it and/or modify it
  7. * under the terms of the GNU Lesser General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or (at
  9. * your option) any later version.
  10. *
  11. * This library is distributed in the hope that it will be useful, but
  12. * WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser
  14. * General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public License
  17. * along with this library. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. namespace PhoolKit;
  20. /**
  21. * A simple logger which logs to the PHP error log as configured in the
  22. * php.ini or to stdout/stderr if running on the command line.
  23. *
  24. * @author Klaus Reimer (k@ailis.de)
  25. */
  26. class Log
  27. {
  28. /** Constant for debug log level. */
  29. const DEBUG = 1;
  30. /** Constant for info log level. */
  31. const INFO = 2;
  32. /** Constant for warn log level. */
  33. const WARN = 3;
  34. /** Constant for error log level. */
  35. const ERROR = 4;
  36. /** The current log level. */
  37. private static $level = self::INFO;
  38. /** The application name. */
  39. private static $name = NULL;
  40. /**
  41. * Private constructor to prevent instantiation.
  42. */
  43. private function __construct()
  44. {
  45. // Empty
  46. }
  47. /**
  48. * Sets the log level.
  49. *
  50. * @param number $level
  51. * The log level to set.
  52. */
  53. public static function setLevel($level)
  54. {
  55. self::$level = $level;
  56. }
  57. /**
  58. * Returns the current log level.
  59. *
  60. * @return number
  61. * The current log level.
  62. */
  63. public static function getLevel()
  64. {
  65. return self::$level;
  66. }
  67. /**
  68. * Sets the application name. This is only used in the apache error log
  69. * to distinguish error messages from different applications and from
  70. * PHP errors. If not set, then no application name is printed.
  71. *
  72. * @param string $name
  73. * The application name.
  74. */
  75. public static function setName($name)
  76. {
  77. self::$name = $name;
  78. }
  79. /**
  80. * Returns the currently set application name,
  81. *
  82. * @return string
  83. * The application name. May be NULL if not set.
  84. */
  85. public static function getName()
  86. {
  87. return self::$name;
  88. }
  89. /**
  90. * Logs a message to STDOUT or STDERR when running in the CLI or to the
  91. * error log as configured in the php.ini.
  92. *
  93. * @param number $level
  94. * The log level.
  95. * @param string $format
  96. * The message format string.
  97. * @param array $args
  98. * Array with message arguments.
  99. */
  100. private static function log($level, $format, $args)
  101. {
  102. $message = vsprintf($format, $args);
  103. if (php_sapi_name() == "cli")
  104. {
  105. if ($level <= self::DEBUG) $levelStr = "DEBUG";
  106. else if ($level == self::INFO) $levelStr = "INFO";
  107. else if ($level == self::WARN) $levelStr = "WARN";
  108. else $levelStr = "ERROR";
  109. $timestamp = strftime("%Y-%m-%d %T");
  110. $out = sprintf("%s %-5s %s\n", $timestamp, $levelStr, $message);
  111. if ($level == self::ERROR)
  112. fprintf(STDERR, $out);
  113. else
  114. fprintf(STDOUT, $out);
  115. }
  116. else
  117. {
  118. if ($level <= self::DEBUG) $levelStr = "Debug";
  119. else if ($level == self::INFO) $levelStr = "Info";
  120. else if ($level == self::WARN) $levelStr = "Warning";
  121. else $levelStr = "Error";
  122. $out = sprintf("%s: %s", ucfirst(strtolower($levelStr)), $message);
  123. if (self::$name) $out = self::$name . " $out";
  124. error_log($out);
  125. }
  126. }
  127. /**
  128. * Logs a debug message to STDOUT or the apache error log.
  129. *
  130. * @param string $message
  131. * The message to log.
  132. * @param mixed $args___
  133. * Variable number of message arguments.
  134. */
  135. public static function debug($message, $args___ = NULL)
  136. {
  137. if (self::$level > self::DEBUG) return;
  138. $args = func_get_args();
  139. array_shift($args);
  140. self::log(self::DEBUG, $message, $args);
  141. }
  142. /**
  143. * Logs an info message to STDOUT or the apache error log.
  144. *
  145. * @param string $message
  146. * The message to log.
  147. * @param mixed $args___
  148. * Variable number of message arguments.
  149. */
  150. public static function info($message, $args___ = NULL)
  151. {
  152. if (self::$level > self::INFO) return;
  153. $args = func_get_args();
  154. array_shift($args);
  155. self::log(self::INFO, $message, $args);
  156. }
  157. /**
  158. * Logs an warning to STDOUT or the apache error log.
  159. *
  160. * @param string $message
  161. * The message to log.
  162. * @param mixed $args___
  163. * Variable number of message arguments.
  164. */
  165. public static function warn($message, $args___ = NULL)
  166. {
  167. if (self::$level > self::WARN) return;
  168. $args = func_get_args();
  169. array_shift($args);
  170. self::log(self::WARN, $message, $args);
  171. }
  172. /**
  173. * Logs an error message to STDERR or the apache error log.
  174. *
  175. * @param string $message
  176. * The message to log.
  177. * @param mixed $args___
  178. * Variable number of message arguments.
  179. */
  180. public static function error($message, $args___ = NULL)
  181. {
  182. if (self::$level > self::ERROR) return;
  183. $args = func_get_args();
  184. array_shift($args);
  185. self::log(self::ERROR, $message, $args);
  186. }
  187. }