00001 <?php
00021 if(!defined('DIR_FS_LOG')) define('DIR_FS_LOG', DIR_FS_APP.DS.'log');
00022
00023 class Logger
00024 {
00025 var $routes;
00026 var $files;
00027
00031 function Logger($routes=null)
00032 {
00033
00034
00035
00036
00037 if($routes) {
00038 $this->routes = $routes;
00039 return;
00040 }
00041
00042
00043 if(DEBUG !== true) {
00044
00045
00046
00047
00048 $this->routes = array(
00049 'pronto(:.*)*' => array('!(debug|info)' => 'pronto.log'),
00050 'app(:.*)*' => array('!(debug|info)' => 'app.log')
00051 );
00052 } else {
00053
00054 $this->routes = array(
00055 'pronto(:.*)*' => array('.*' => 'pronto.log'),
00056 'app(:.*)*' => array('.*' => 'app.log')
00057 );
00058 }
00059 }
00060
00064 function add_routes($routes)
00065 {
00066 foreach($routes as $k=>$v) $this->routes[$k] = $v;
00067 }
00068
00072 function clear_routes()
00073 {
00074 $this->routes = array();
00075 }
00076
00077
00082 function msg($facility, $priority, $message)
00083 {
00084 foreach($this->routes as $fac=>$v) {
00085 $m = array();
00086 if(!$this->_preg($fac, $facility, $m)) continue;
00087 for($i = 1; $i < count($m); $i++) $v = str_replace("\\$i", $m[$i], $v);
00088 if(is_array($v)) {
00089 foreach($v as $pri=>$fn) {
00090 $m = array();
00091 if(!$this->_preg($pri, $priority, $m)) continue;
00092 for($i = 1; $i < count($m); $i++) $fn = str_replace("\\$i", $m[$i], $fn);
00093 $this->_log_msg($fn, $facility, $priority, $message);
00094 }
00095 } else {
00096 $this->_log_msg($v, $facility, $priority, $message);
00097 }
00098 }
00099 }
00100
00107 function _preg($re, $str, &$matches)
00108 {
00109 if(substr($re, 0, 1) == '!') {
00110 $re = substr($re, 1);
00111 return !preg_match("/^$re$/", $str);
00112 }
00113 return preg_match("/^$re$/", $str, $matches);
00114 }
00115
00116 function _log_msg($filename, $facility, $priority, $message)
00117 {
00118
00119 if(!is_dir(DIR_FS_LOG)) return;
00120
00121 if(!is_resource($this->files[$filename])) {
00122
00123 $path = substr($filename, 0, 1) == DS ? $filename : DIR_FS_LOG.DS.$filename;
00124 $this->files[$filename] = @fopen($path, 'a');
00125 if(!is_resource($this->files[$filename])) {
00126
00127
00128 return;
00129 }
00130 }
00131 $dt = date('Y-m-d H:i:s');
00132 $msg = "[$dt] [$facility.$priority] $message\n";
00133 fputs($this->files[$filename], $msg);
00134 }
00135 }
00136
00145 function l() {
00146 $logger =& Registry::get('pronto:logger');
00147 if(!$logger) return;
00148
00149 $args = func_get_args();
00150 if(count($args) == 1) {
00151 $facility = 'app';
00152 $priority = 'info';
00153 $message = $args[0];
00154 } else if(count($args) == 2) {
00155 $facility = 'app';
00156 $priority = $args[0];
00157 $message = $args[1];
00158 } else {
00159 $facility = $args[0];
00160 $priority = $args[1];
00161 $message = vsprintf($args[2], array_slice($args, 3));
00162 }
00163
00164 $logger->msg($facility, $priority, $message);
00165 }
00166
00167 ?>