PageRenderTime 25ms CodeModel.GetById 2ms app.highlight 16ms RepoModel.GetById 2ms app.codeStats 0ms

/includes/init/logger.php

https://bitbucket.org/baruffaldi/website-insaneminds
PHP | 102 lines | 54 code | 14 blank | 34 comment | 14 complexity | f3a6ce22d82b8aaa66bd5ea38004618b MD5 | raw file
  1<?php
  2/**
  3 * Loggers
  4 *
  5 * @version 0.3
  6 * @package StreamingAPI
  7 * @author Filippo Baruffaldi
  8 * @email filippo.baruffaldi@oneitalia.it
  9 * @last-revisioner Filippo Baruffaldi
 10 */
 11
 12require_once ZENDLIBPATH . 'Log.php';
 13require_once ZENDLIBPATH . 'Exception.php';
 14require_once ZENDLIBPATH . 'Log' . DIRECTORY_SEPARATOR . 'Exception.php';
 15require_once ZENDLIBPATH . 'Log' . DIRECTORY_SEPARATOR . 'Writer' . DIRECTORY_SEPARATOR . 'Null.php';
 16require_once ZENDLIBPATH . 'Log' . DIRECTORY_SEPARATOR . 'Writer' . DIRECTORY_SEPARATOR . 'Stream.php';
 17require_once ZENDLIBPATH . 'Log' . DIRECTORY_SEPARATOR . 'Writer' . DIRECTORY_SEPARATOR . 'Db.php';
 18
 19//***************************************************//
 20//****************** Log Handlers *******************//
 21//***************************************************//
 22
 23  //*** do I have to log ??
 24  //************************
 25    define( '__LOG__', ( $Request->type == 'php' && __DB__ ) );
 26
 27  //*** Log Files
 28  //**************
 29    $accessLogFile = LOGSPATH . 'access_' . date( 'Ym' ) . '.log';
 30    $errorsLogFile = LOGSPATH . 'errors_' . date( 'Ym' ) . '.log';
 31    $soapLogFile   = LOGSPATH . 'soap_'   . date( 'Ym' ) . '.log';
 32
 33  //*** Database Writer
 34  //********************
 35    $loggerDbColumnsMap = array( 'ip_local'     => 'ip_local', 
 36                                 'ip_remote'    => 'ip_remote', 
 37                                 'msisdn'       => 'msisdn', 
 38                                 'partyid'      => 'partyid', 
 39                                 'useragent'    => 'useragent', 
 40                                 'category'     => 'category', 
 41                                 'method'       => 'method', 
 42                                 'priority'     => 'priority', 
 43                                 'priorityName' => 'priorityname', 
 44                                 'message'      => 'message' );
 45                                 
 46    if ( __LOG__ ) $loggerDbWriter     = new Zend_Log_Writer_Db( $db, "{$dbParams['logsdbname']}.{$dbParams['tableprefix']}{$dbParams['table_ad_ev_log']}_$serverId", $columnMapping);
 47    else $loggerDbWriter     = new Zend_Log_Writer_Null( $db, "{$dbParams['logsdbname']}.{$dbParams['tableprefix']}{$dbParams['table_ad_ev_log']}_$serverId", $columnMapping);
 48
 49  //*** Stream Writers
 50  //*******************
 51    if ( __LOG__ ) $accessWriter = new Zend_Log_Writer_Stream( $accessLogFile );
 52    else $accessWriter = new Zend_Log_Writer_Null( $accessLogFile );
 53    if ( __LOG__ ) $errorsWriter = new Zend_Log_Writer_Stream( $errorsLogFile );
 54    else $errorsWriter = new Zend_Log_Writer_Null( $errorsLogFile );
 55    if ( __LOG__ ) $soapWriter   = new Zend_Log_Writer_Stream( $soapLogFile   );
 56    else $soapWriter = new Zend_Log_Writer_Null( $soapLogFile );
 57  
 58  //*** Define Formatters
 59  //**********************
 60    $accessFormat = new Zend_Log_Formatter_Simple( "[%timestamp%][%ip_local%][%ip_remote%][%msisdn%][%partyid%][%useragent%][%category%][%method%][%priority%][%priorityName%][%whois%]: %message%" . PHP_EOL );
 61    $errorsFormat = new Zend_Log_Formatter_Simple( "[%timestamp%][%ip_local%][%ip_remote%][%msisdn%][%partyid%][%useragent%][%category%][%method%][%priority%][%priorityName%][%whois%]: %message%" . PHP_EOL );
 62    $soapFormat   = new Zend_Log_Formatter_Simple( "[%timestamp%][%ip_local%][%ip_remote%][%msisdn%][%partyid%][%useragent%][%category%][%method%][%priority%][%priorityName%][%whois%]: %message%" . PHP_EOL );
 63
 64  //*** Set Writers Formats
 65  //************************
 66	  $accessWriter->setFormatter( $accessFormat );
 67	  $errorsWriter->setFormatter( $errorsFormat );
 68	  $soapWriter->setFormatter(   $soapFormat   );
 69
 70  //*** Loggers
 71  //************
 72    $accessLogger = new Zend_Log( $accessWriter );  
 73    $errorsLogger = new Zend_Log( $errorsWriter );
 74    $soapLogger   = new Zend_Log( $soapWriter   );
 75    
 76    // We want all loggers inside an array to easily set all event items
 77    $loggers      = array( $accessLogger, $errorsLogger, $soapLogger );
 78	  
 79  //*** Set Loggers Items
 80  //**********************
 81    foreach ( $loggers as $logz => $log )
 82    {
 83      $log->setEventItem( 'timestamp', date( 'Y.m.d-H:i:s' ) );
 84	    $log->setEventItem( 'ip_local',  $_SERVER['SERVER_ADDR'] );
 85	    $log->setEventItem( 'ip_remote', $_SERVER['REMOTE_ADDR'] );
 86	    $log->setEventItem( 'msisdn',    var_export( $_SERVER['HTTP_X_H3G_MSISDN'], true ) );
 87	    $log->setEventItem( 'partyid',   var_export( $_SERVER['HTTP_X_H3G_PARTY_ID'], true ) );
 88	    $log->setEventItem( 'useragent', DSS::getUAPattern( $_SERVER['HTTP_USER_AGENT'] ) );
 89	    $log->setEventItem( 'category',  $Request->category );
 90	    $log->setEventItem( 'method',    $Request->action );
 91	    $log->setEventItem( 'whois',     ( array_key_exists( $_SERVER['HTTP_X_H3G_MSISDN'] , $MsISDNtoUsers ) ) ? $MsISDNtoUsers[$_SERVER['HTTP_X_H3G_MSISDN']] : ( gethostbyaddr( $_SERVER['REMOTE_ADDR'] ) != $_SERVER['REMOTE_ADDR'] ) ? gethostbyaddr( $_SERVER['REMOTE_ADDR'] ) : NULL );
 92    }
 93
 94  //*** Set Database Writer
 95  //************************
 96    $accessLogger->addWriter( $loggerDbWriter );
 97    $errorsLogger->addWriter( $loggerDbWriter );
 98
 99//***************************************************//
100//****************** ------------ *******************//
101//***************************************************//
102  if ( !empty( $_SERVER['HTTP_REFERER'] ) && $_SERVER['SERVER_ADDR'] != '127.0.0.1' ) $accessLogger->log( 'Incoming connection from: ' . $_SERVER['HTTP_REFERER'], 6 );