Source for file class.Logger.inc

Documentation is available at class.Logger.inc

  1. <?php //-*-php-*-
  2. /* ******************************************************************** **
  3. ** Copyright notice **
  4. ** **
  5. ** (c) 1995-2003 PHPOpenChat Development Team **
  6. ** http://phpopenchat.sourceforge.net/ **
  7. ** **
  8. ** All rights reserved **
  9. ** **
  10. ** This script is part of the PHPOpenChat project. The PHPOpenChat **
  11. ** project is free software; you can redistribute it and/or modify **
  12. ** it under the terms of the GNU General Public License as published by **
  13. ** the Free Software Foundation; either version 2 of the License, or **
  14. ** (at your option) any later version. **
  15. ** **
  16. ** The GNU General Public License can be found at **
  17. ** http://www.gnu.org/copyleft/gpl.html. **
  18. ** A copy is found in the textfile GPL and important notices to the **
  19. ** license from the team is found in the textfile LICENSE distributed **
  20. ** with these scripts. **
  21. ** **
  22. ** This script is distributed in the hope that it will be useful, **
  23. ** but WITHOUT ANY WARRANTY; without even the implied warranty of **
  24. ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the **
  25. ** GNU General Public License for more details. **
  26. ** **
  27. ** This copyright notice MUST APPEAR in all copies of the script! **
  28. ** ******************************************************************** */
  29.  
  30. //Get default values
  31. require_once(POC_BASE.'/config.inc.php');
  32.  
  33. /**
  34. * Implements a logging mechanism
  35. *
  36. * @package phpopenchat
  37. * @author Michael Oertel <michael@ortelius.de>
  38. * @access public
  39. * @version $Id: class.Logger.inc,v 1.16 2003/10/18 08:39:03 letreo Exp $
  40. */
  41. class Logger
  42. {
  43.  
  44. /**
  45. * Constructor
  46. *
  47. * @access public
  48. */
  49. function Logger()
  50. {
  51. }
  52. /**
  53. * Writes lines into the log file
  54. *
  55. * @access private
  56. * @return void
  57. */
  58. function _write( $case, $message, $file = '', $line = '' )
  59. {
  60. if( NO_LOGGING ) return null;
  61. define_syslog_variables();
  62. $file = ($file!='')? $file.':'.$line.' ' : '';
  63.  
  64. //mo, check for user-ip exists in Chatter::go_online()
  65. if( isset($_SESSION['chatter']) ) {
  66. $ip = $_SESSION['chatter']->get_last_IP();
  67. } else {
  68. $ip = (isset($_SERVER['REMOTE_ADDR']))? $_SERVER['REMOTE_ADDR'] : '-' ;
  69. }
  70. $log = rtrim("$ip $file $message").NL;
  71.  
  72. if ( LOG_TO_FILE ) {
  73. $fh=fopen(LOG_TO_FILE, "a");
  74. fwrite($fh, date("Y-m-d H:i:s"). " $case $log");
  75. fclose($fh);
  76. }
  77.  
  78. if ( LOG_TO_SYSLOG ) {
  79. openlog('[POC-'.$case.']', LOG_PERROR, LOG_SYSLOG_FACIL);
  80. switch($case)
  81. {
  82. case 'INFO': syslog(LOG_INFO, $log); break;
  83. case 'LINE': syslog(LOG_INFO, $log); break;
  84. case 'POST': syslog(LOG_INFO, $log); break;
  85. case 'DEBUG': syslog(LOG_DEBUG, $log); break;
  86. case 'WARNING':syslog(LOG_WARNING, $log); break;
  87. case 'ERROR': syslog(LOG_ERR, $log); break;
  88. case 'EMERG': syslog(LOG_EMERG, $log); break;
  89. }
  90. closelog();
  91. }
  92. }
  93.  
  94. function history_raw ( $input ) {
  95. if ( NO_LOGGING || !LOG_TO_FILE || ! LOGFILE_HISTORY_RAW ) return null;
  96.  
  97. if (! $fh=fopen(LOGFILE_HISTORY_RAW, "a") ) {
  98. $_SESSION['logger']->error("can not open".LOGFILE_HISTORY_RAW );
  99. }
  100.  
  101. $log.= date ("Y-m-d H:i:s", $input->get_date() );
  102. $log.= " ".$input->get_ip();
  103. $log.= " [" . $input->get_channel() . "] " ;
  104. $chatter=$input->get_chatter();
  105. $log.=$chatter->get_nick();
  106. unset($chatter);
  107.  
  108. $log .= $input->get_whispered() == 'on' ? " w" : " s" ;
  109. $log.= " to ".$input->get_recipient() . ":" ;
  110. $log .= " ". $input->get_line();
  111.  
  112. fwrite($fh, $log."\n");
  113. fclose($fh);
  114. unset($log);
  115. }
  116.  
  117. function history_html ( $input ) {
  118. if ( ! LOGFILE_HISTORY_HTML ) return null;
  119. $_SESSION['logger']->debug("hist-html:".$input);
  120. }
  121.  
  122. /**
  123. * Loggs info messages
  124. *
  125. * @access public
  126. */
  127. function info( $message )
  128. {
  129. if( !LOG_POC_INFOS ) return null;
  130. $this->_write('INFO',$message);
  131. }
  132. /**
  133. * Loggs lines
  134. *
  135. * @access public
  136. */
  137. function line($message)
  138. {
  139. if( !LOG_POC_LINES ) return null;
  140. $this->_write('LINE',$message);
  141. }
  142. /**
  143. * Loggs raw posts
  144. *
  145. * @access public
  146. */
  147. function raw_post($message)
  148. {
  149. if( !LOG_POC_RAW_POST ) return null;
  150. $this->_write('POST',$message);
  151. }
  152.  
  153. /**
  154. * Loggs debug messages
  155. *
  156. * @access public
  157. */
  158. function debug($message, $file = '' , $line = '')
  159. {
  160. if( !LOG_POC_DEBUG ) return null;
  161. $this->_write('DEBUG',$message, $file, $line);
  162. }
  163.  
  164. /**
  165. * Loggs warnings
  166. *
  167. * warning conditions
  168. *
  169. * @access public
  170. */
  171. function warning($message)
  172. {
  173. if( !LOG_POC_WARNING ) return null;
  174. $this->_write('WARNING',$message);
  175. }
  176.  
  177. /**
  178. * Loggs errors
  179. *
  180. * error conditions
  181. *
  182. * @access public
  183. */
  184. function error($message, $file = '' , $line = '')
  185. {
  186. if( !LOG_POC_ERROR ) return null;
  187. $this->_write('ERROR',$message, $file, $line);
  188. }
  189.  
  190. /**
  191. * Loggs emergency errors
  192. *
  193. * system is unusable
  194. *
  195. * @access public
  196. */
  197. function emerg($message, $file = '' , $line = '')
  198. {
  199. if( !LOG_POC_EMERG ) return null;
  200. $this->_write('EMERG',$message, $file, $line);
  201. }
  202.  
  203. /**
  204. * Loggs traffic
  205. *
  206. * @access public
  207. * @param integer
  208. * @return mixed
  209. */
  210. function traffic( $content_length = 0 )
  211. {
  212. if( !LOG_POC_TRAFFIC || NO_LOGGING ) return null;
  213. //if( !$_SESSION['chat']->check_php_version( '4.3.0' ) ) return null;
  214. if( php_sapi_name() == 'cgi' ) return null;
  215. if( !function_exists('apache_response_headers') ) return null;
  216. if( !is_int($content_length) ) die('Integer expected!');
  217.  
  218. $bytes_in = 0;
  219. $bytes_out = 0;
  220.  
  221. $headers_out = apache_response_headers();
  222. if( is_array($headers_out) ) {
  223. foreach( $headers_out as $key => $value ) {
  224. $bytes_out += strlen($key) + strlen($value);
  225. }
  226. }
  227.  
  228. $headers_in = apache_request_headers();
  229. if( is_array($headers_in) ) {
  230. foreach( $headers_in as $key => $value ) {
  231. $bytes_in += strlen($key) + strlen($value);
  232. }
  233. }
  234. $bytes_out += $content_length;
  235. $_SESSION['chat']->connect();
  236. $_SESSION['chat']->log_traffic( $bytes_in, $bytes_out );
  237. $_SESSION['chat']->disconnect();
  238. }
  239. }
  240. ?>

Documentation generated on Tue, 29 Jun 2004 14:42:09 +0200 by phpDocumentor 1.3.0RC3