PageRenderTime 53ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/Krai/Log.php

https://github.com/gsmcwhirter/krai
PHP | 276 lines | 120 code | 18 blank | 138 comment | 9 complexity | b6265b3b0875afb06ca6ac2b4340ca51 MD5 | raw file
Possible License(s): MIT
  1. <?php
  2. /**
  3. * Logging interface for the Krai Framework.
  4. *
  5. * Apologies, but I am not going to document this well since it will be replaced
  6. * by version 1.1.
  7. *
  8. * @package Krai
  9. * @subpackage Log
  10. * @author Greg McWhirter <gsmcwhirter@gmail.com>
  11. * @copyright Copyright (c) 2008, Greg McWhirter
  12. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  13. */
  14. Krai::Uses(
  15. Krai::$FRAMEWORK."/Log/Logger.php",
  16. Krai::$FRAMEWORK."/Log/Logger/Console.php",
  17. Krai::$FRAMEWORK."/Log/Logger/Email.php",
  18. Krai::$FRAMEWORK."/Log/Logger/Local.php",
  19. Krai::$FRAMEWORK."/Log/Logger/Syslog.php",
  20. Krai::$FRAMEWORK."/Log/Session.php",
  21. Krai::$FRAMEWORK."/Struct/Loginfo.php"
  22. );
  23. /**
  24. * Log output path
  25. *
  26. */
  27. define ("KRAI_DEFAULT_LOCAL_LOGFILE_PATH", Krai::$APPDIR."/log/");
  28. /**
  29. * Admin email constant
  30. */
  31. define("KRAI_ADMIN_EMAIL", Krai::GetConfig("ADMIN_EMAIL"));
  32. /**
  33. * Email log subject template.
  34. * The calling application is appended to the end of this
  35. *
  36. */
  37. define ("KRAI_DEFAULT_EMAIL_SUBJECT", "LOG MESSAGE: ".date("r"));
  38. /**
  39. * Logger interface
  40. *
  41. * Apologies, but I am not going to document this well since it will be replaced
  42. * by version 1.1.
  43. *
  44. * @package Krai
  45. * @subpackage Log
  46. */
  47. abstract class Krai_Log
  48. {
  49. /**
  50. * System log level-ish thing
  51. *
  52. */
  53. const DEFAULT_LOGGER_LOG_FACILITY = LOG_LOCAL6;
  54. /**
  55. * Default log severity
  56. *
  57. */
  58. const DEFAULT_MSG_SEVERITY = Krai::LOG_INFO;
  59. /**
  60. * Default category
  61. *
  62. */
  63. const DEFAULT_MSG_CATEGORY = "OTHER";
  64. /**
  65. * Default threshold
  66. *
  67. */
  68. const DEFAULT_THRESH = Krai::LOG_WARNING;
  69. /**
  70. * Default queue size
  71. *
  72. */
  73. const DEFAULT_MAX_QUEUE_SIZE = 250;
  74. /**
  75. * Default trigger threshold
  76. *
  77. */
  78. const DEFAULT_ERROR_CONDITION_TRIGGER_THRESH = Krai::LOG_ERROR;
  79. /**
  80. * Default error result threshold
  81. *
  82. */
  83. const DEFAULT_ERROR_CONDITION_THRESH = Krai::LOG_DEBUG;
  84. /**
  85. * Queue messages?
  86. *
  87. */
  88. const DEFAULT_QUEUE_MODE = false;
  89. /**
  90. * Default log name
  91. *
  92. */
  93. const DEFAULT_LOCAL_LOGFILE_NAME = "crash.log";
  94. /**
  95. * Default logging method
  96. *
  97. */
  98. const DEFAULT_LOCAL_METHOD = "ARCHIVE";
  99. /**
  100. * Default mail recipient
  101. *
  102. */
  103. const DEFAULT_EMAIL_RECIPIENT = KRAI_ADMIN_EMAIL;
  104. /**
  105. * Default mail sender
  106. *
  107. */
  108. const DEFAULT_EMAIL_SENDER = KRAI_ADMIN_EMAIL;
  109. /**
  110. * Default main subject
  111. */
  112. const DEFAULT_EMAIL_SUBJECT = KRAI_DEFAULT_EMAIL_SUBJECT;
  113. /**
  114. * Default syslog level
  115. */
  116. const DEFAULT_SYSLOG_FACILITY = LOG_LOCAL6;
  117. /**
  118. * List of message categories
  119. *
  120. */
  121. const MESSAGE_CATEGORIES_LIST = "LDAP,DATA,SIS,LOGIC,FTP,LOGS,SQL,OTHER,INTERNAL,MAIL";
  122. /**
  123. * List of log levels
  124. *
  125. */
  126. const SYSLOG_LEVELS_LIST = "LOG_EMERG,LOG_ALERT,LOG_CRITICAL,LOG_ERROR,LOG_WARNING,LOG_NOTICE,LOG_INFO,LOG_DEBUG";
  127. /**
  128. * List of log modes
  129. *
  130. */
  131. const LOG_MODES_LIST = "local,email,syslog,console";
  132. /**
  133. * The logger actual instance
  134. *
  135. * @var Krai_Log_Session
  136. */
  137. protected static $_LOGSESSION;
  138. /**
  139. * The state of the logger
  140. *
  141. * @var boolean
  142. */
  143. private static $_LCONNECTED = false;
  144. /**
  145. * The logs themselves
  146. *
  147. * @var array
  148. */
  149. public static $_LOGS = array();
  150. /**
  151. * The default log
  152. *
  153. * @var mixed
  154. */
  155. protected static $_LOGDEFAULT = null;
  156. /**
  157. * Initialize the logger
  158. *
  159. * @param Krai_Struct_Loginfo $loginfo
  160. */
  161. final public static function Start(Krai_Struct_Loginfo $loginfo)
  162. {
  163. if(!self::$_LCONNECTED)
  164. {
  165. self::$_LOGSESSION = new Krai_Log_Session("Krai");
  166. foreach($loginfo->types as $name => $type)
  167. {
  168. self::$_LOGS[$name] = self::$_LOGSESSION->Enable($type);
  169. }
  170. foreach($loginfo->configs as $key => $params)
  171. {
  172. $inst = "";
  173. foreach($params as $dir => $par)
  174. {
  175. if(substr($key,0,2) == "n:")
  176. {
  177. $temp = substr($key, 2);
  178. $inst = self::$_LOGS[$temp];
  179. }
  180. elseif(substr($key,0,2) == "t:")
  181. {
  182. $inst = substr($key,2);
  183. }
  184. if(is_null($par))
  185. {
  186. self::$_LOGSESSION->Configure($inst, $dir);
  187. }
  188. else
  189. {
  190. self::$_LOGSESSION->Configure($inst, $dir, $par);
  191. }
  192. }
  193. }
  194. self::$_LOGDEFAULT = $loginfo->default;
  195. self::$_LCONNECTED = true;
  196. }
  197. }
  198. /**
  199. * Write a log message
  200. *
  201. * @param string $message
  202. * @param integer $level
  203. * @param array $logs
  204. * @param string $cat
  205. * @param array $forces
  206. */
  207. final public static function Write($message,
  208. $level = Krai::LOG_INFO,
  209. array $logs = array(),
  210. $cat = null,
  211. array $forces = array())
  212. {
  213. $pars = array();
  214. if(count($logs) == 0)
  215. {
  216. $pars[0] = self::$_LOGS[self::$_LOGDEFAULT];
  217. foreach($forces as $f)
  218. {
  219. $pars[] = $f;
  220. }
  221. if(!is_null($cat))
  222. {
  223. $pars[] = $cat;
  224. }
  225. $pars[] = $level;
  226. $pars[] = $message;
  227. call_user_func_array(array(self::$_LOGSESSION, "Write"), $pars);
  228. }
  229. else
  230. {
  231. foreach($logs as $log)
  232. {
  233. $pars[0] = self::$_LOGS[$log];
  234. foreach($forces as $f)
  235. {
  236. $pars[] = $f;
  237. }
  238. if(!is_null($cat))
  239. {
  240. $pars[] = $cat;
  241. }
  242. $pars[] = $level;
  243. $pars[] = $message;
  244. call_user_func_array(array(self::$_LOGSESSION, "Write"), $pars);
  245. }
  246. }
  247. }
  248. /**
  249. * Close all the logs and stuff
  250. *
  251. * @return boolean
  252. */
  253. final public static function Close()
  254. {
  255. self::$_LOGSESSION->Close();
  256. return true;
  257. }
  258. }