Changeset 31102 for trunk/i.php


Ignore:
Timestamp:
Apr 24, 2015, 4:00:50 PM (9 years ago)
Author:
mistic100
Message:

feature 3221 Add Logger class

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/i.php

    r28587 r31102  
    3131@include(PHPWG_ROOT_PATH.PWG_LOCAL_DIR .'config/database.inc.php');
    3232
     33include(PHPWG_ROOT_PATH . 'include/Logger.class.php');
     34
     35$logger = new Logger(array(
     36  'directory' => PHPWG_ROOT_PATH . $conf['data_location'] . $conf['log_dir'],
     37  'severity' => $conf['log_level'],
     38  // we use an hashed filename to prevent direct file access, and we salt with
     39  // the db_password instead of secret_key because the log must be usable in i.php
     40  // (secret_key is in the database)
     41  'filename' => 'log_' . date('Y-m-d') . '_' . sha1(date('Y-m-d') . $conf['db_password']) . '.txt',
     42  ));
     43
    3344
    3445function trigger_notify() {}
     
    6778// end fast bootstrap
    6879
    69 function ilog()
    70 {
    71   global $conf;
    72   if (!$conf['enable_i_log']) return;
    73 
    74   $line = date("c");
    75   foreach( func_get_args() as $arg)
    76   {
    77     $line .= ' ';
    78     if (is_array($arg))
    79     {
    80       $line .= implode(' ', $arg);
    81     }
    82     else
    83     {
    84       $line .= $arg;
    85     }
    86   }
    87         $file=PHPWG_ROOT_PATH.$conf['data_location'].'tmp/i.log';
    88   if (false == file_put_contents($file, $line."\n", FILE_APPEND))
    89         {
    90                 mkgetdir(dirname($file));
    91         }
    92 }
    93 
    9480function ierror($msg, $code)
    9581{
     82  global $logger;
    9683  if ($code==301 || $code==302)
    9784  {
     
    10289    // default url is on html format
    10390    $url = html_entity_decode($msg);
     91    $logger->warning($code . ' ' . $url, 'i.php', array(
     92      'url' => $_SERVER['REQUEST_URI'],
     93      ));
    10494    header('Request-URI: '.$url);
    10595    header('Content-Location: '.$url);
    10696    header('Location: '.$url);
    107     ilog('WARN', $code, $url, $_SERVER['REQUEST_URI']);
    10897    exit;
    10998  }
     
    118107  //todo improve
    119108  echo $msg;
    120   ilog('ERROR', $code, $msg, $_SERVER['REQUEST_URI']);
     109  $logger->error($code . ' ' . $msg, 'i.php', array(
     110      'url' => $_SERVER['REQUEST_URI'],
     111      ));
    121112  exit;
    122113}
     
    405396catch (Exception $e)
    406397{
    407   ilog("db error", $e->getMessage());
     398  $logger->error($e->getMessage(), 'i.php');
    408399}
    409400pwg_db_check_charset();
     
    502493  catch (Exception $e)
    503494  {
    504     ilog("db error", $e->getMessage());
     495    $logger->error($e->getMessage(), 'i.php');
    505496  }
    506497}
     
    622613$timing['send'] = time_step($step);
    623614
    624 ilog('perf',
    625   basename($page['src_path']), $o_size, $o_size[0]*$o_size[1],
    626   basename($page['derivative_path']), $d_size, $d_size[0]*$d_size[1],
    627   function_exists('memory_get_peak_usage') ? round( memory_get_peak_usage()/(1024*1024), 1) : '',
    628   time_step($begin),
    629   '|', $timing);
    630 ?>
     615$timing['total'] = time_step($begin);
     616
     617if ($logger->severity() >= Logger::INFO)
     618{
     619  $logger->info('perf', 'i.php', array(
     620    'src_path' => basename($page['src_path']),
     621    'derivative_path' => basename($page['derivative_path']),
     622    'o_size' => $o_size[0] . ' ' . $o_size[1] . ' ' . ($o_size[0]*$o_size[1]),
     623    'd_size' => $d_size[0] . ' ' . $d_size[1] . ' ' . ($d_size[0]*$d_size[1]),
     624    'mem_usage' => function_exists('memory_get_peak_usage') ? round( memory_get_peak_usage()/(1024*1024), 1) : '',
     625    'timing' => $timing,
     626    ));
     627}
Note: See TracChangeset for help on using the changeset viewer.