Changeset 31102


Ignore:
Timestamp:
04/24/15 16:00:50 (4 years ago)
Author:
mistic100
Message:

feature 3221 Add Logger class

Location:
trunk
Files:
1 added
6 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} 
  • trunk/include/common.inc.php

    r29904 r31102  
    105105include(PHPWG_ROOT_PATH . 'include/constants.php'); 
    106106include(PHPWG_ROOT_PATH . 'include/functions.inc.php'); 
     107include(PHPWG_ROOT_PATH . 'include/template.class.php'); 
     108include(PHPWG_ROOT_PATH . 'include/cache.class.php'); 
     109include(PHPWG_ROOT_PATH . 'include/Logger.class.php'); 
    107110 
    108111$persistent_cache = new PersistentFileCache(); 
     
    122125 
    123126load_conf_from_db(); 
     127 
     128$logger = new Logger(array( 
     129  'directory' => PHPWG_ROOT_PATH . $conf['data_location'] . $conf['log_dir'], 
     130  'severity' => $conf['log_level'], 
     131  // we use an hashed filename to prevent direct file access, and we salt with 
     132  // the db_password instead of secret_key because the log must be usable in i.php 
     133  // (secret_key is in the database) 
     134  'filename' => 'log_' . date('Y-m-d') . '_' . sha1(date('Y-m-d') . $conf['db_password']) . '.txt', 
     135  'globPattern' => 'log_*.txt', 
     136  'archiveDays' => $conf['log_archive_days'], 
     137  )); 
    124138 
    125139if (!$conf['check_upgrade_feed']) 
  • trunk/include/config_default.inc.php

    r31097 r31102  
    463463$conf['show_php_errors'] = E_ALL; 
    464464 
    465 // enable log for i derivative script 
    466 $conf['enable_i_log'] = false; 
    467465 
    468466// +-----------------------------------------------------------------------+ 
     
    653651// Web services are allowed (true) or completely forbidden (false) 
    654652$conf['allow_web_services'] = true; 
    655  
    656 // enable log for web services 
    657 $conf['ws_enable_log'] = false; 
    658  
    659 // web services log file path 
    660 $conf['ws_log_filepath'] = '/tmp/piwigo_ws.log'; 
    661653 
    662654// Maximum number of images to be returned foreach call to the web service 
     
    813805// the directory where "ffmpeg" executable is. 
    814806$conf['ffmpeg_dir'] = ''; 
    815 ?> 
     807 
     808// +-----------------------------------------------------------------------+ 
     809// |                                 log                                   | 
     810// +-----------------------------------------------------------------------+ 
     811// Logs directory, relative to $conf['data_location'] 
     812$conf['log_dir'] = '/logs'; 
     813 
     814// Log level (OFF, CRITICAL, ERROR, WARNING, NOTICE, INFO, DEBUG) 
     815// development = DEBUG, production = ERROR 
     816$conf['log_level'] = 'DEBUG'; 
     817 
     818// Keep logs file during X days 
     819$conf['log_archive_days'] = 30; 
  • trunk/include/functions.inc.php

    r30864 r31102  
    3737include_once( PHPWG_ROOT_PATH .'include/derivative_std_params.inc.php'); 
    3838include_once( PHPWG_ROOT_PATH .'include/derivative.inc.php'); 
    39 include_once( PHPWG_ROOT_PATH .'include/template.class.php'); 
    40 include_once( PHPWG_ROOT_PATH .'include/cache.class.php'); 
    4139 
    4240 
  • trunk/include/ws_functions.inc.php

    r26461 r31102  
    206206 
    207207/** 
    208  * Writes info to the log file 
    209  */ 
    210 function ws_logfile($string) 
    211 { 
    212   global $conf; 
    213  
    214   if (!$conf['ws_enable_log']) 
    215   { 
    216     return true; 
    217   } 
    218  
    219   file_put_contents( 
    220     $conf['ws_log_filepath'], 
    221     '['.date('c').'] '.$string."\n", 
    222     FILE_APPEND 
    223     ); 
    224 } 
    225  
    226 /** 
    227208 * create a tree from a flat list of categories, no recursivity for high speed 
    228209 */ 
  • trunk/include/ws_functions/pwg.images.php

    r31031 r31102  
    183183function merge_chunks($output_filepath, $original_sum, $type) 
    184184{ 
    185   global $conf; 
    186  
    187   ws_logfile('[merge_chunks] input parameter $output_filepath : '.$output_filepath); 
     185  global $conf, $logger; 
     186 
     187  $logger->debug('[merge_chunks] input parameter $output_filepath : '.$output_filepath, 'WS'); 
    188188 
    189189  if (is_file($output_filepath)) 
     
    207207      if (preg_match($pattern, $file)) 
    208208      { 
    209         ws_logfile($file); 
     209        $logger->debug($file, 'WS'); 
    210210        $chunks[] = $upload_dir.'/'.$file; 
    211211      } 
     
    217217 
    218218  if (function_exists('memory_get_usage')) { 
    219     ws_logfile('[merge_chunks] memory_get_usage before loading chunks: '.memory_get_usage()); 
     219    $logger->debug('[merge_chunks] memory_get_usage before loading chunks: '.memory_get_usage(), 'WS'); 
    220220  } 
    221221 
     
    227227 
    228228    if (function_exists('memory_get_usage')) { 
    229       ws_logfile('[merge_chunks] memory_get_usage on chunk '.++$i.': '.memory_get_usage()); 
     229      $logger->debug('[merge_chunks] memory_get_usage on chunk '.++$i.': '.memory_get_usage(), 'WS'); 
    230230    } 
    231231 
     
    239239 
    240240  if (function_exists('memory_get_usage')) { 
    241     ws_logfile('[merge_chunks] memory_get_usage after loading chunks: '.memory_get_usage()); 
     241    $logger->debug('[merge_chunks] memory_get_usage after loading chunks: '.memory_get_usage(), 'WS'); 
    242242  } 
    243243} 
     
    825825function ws_images_add_chunk($params, $service) 
    826826{ 
    827   global $conf; 
     827  global $conf, $logger; 
    828828 
    829829  foreach ($params as $param_key => $param_value) 
     
    833833      continue; 
    834834    } 
    835     ws_logfile( 
    836       sprintf( 
    837         '[ws_images_add_chunk] input param "%s" : "%s"', 
    838         $param_key, 
    839         is_null($param_value) ? 'NULL' : $param_value 
    840         ) 
    841       ); 
     835 
     836    $logger->debug(sprintf( 
     837      '[ws_images_add_chunk] input param "%s" : "%s"', 
     838      $param_key, 
     839      is_null($param_value) ? 'NULL' : $param_value 
     840      ), 'WS'); 
    842841  } 
    843842 
     
    857856    ); 
    858857 
    859   ws_logfile('[ws_images_add_chunk] data length : '.strlen($params['data'])); 
     858  $logger->debug('[ws_images_add_chunk] data length : '.strlen($params['data']), 'WS'); 
    860859 
    861860  $bytes_written = file_put_contents( 
     
    882881function ws_images_addFile($params, $service) 
    883882{ 
    884   ws_logfile(__FUNCTION__.', input :  '.var_export($params, true)); 
    885  
    886   global $conf; 
     883  global $conf, $logger; 
     884 
     885  $logger->debug(__FUNCTION__, 'WS', $params); 
    887886 
    888887  // what is the path and other infos about the photo? 
     
    975974function ws_images_add($params, $service) 
    976975{ 
    977   global $conf, $user; 
     976  global $conf, $user, $logger; 
    978977 
    979978  foreach ($params as $param_key => $param_value) 
    980979  { 
    981     ws_logfile( 
    982       sprintf( 
    983         '[pwg.images.add] input param "%s" : "%s"', 
    984         $param_key, 
    985         is_null($param_value) ? 'NULL' : $param_value 
    986         ) 
    987       ); 
     980    $logger->debug(sprintf( 
     981      '[pwg.images.add] input param "%s" : "%s"', 
     982      $param_key, 
     983      is_null($param_value) ? 'NULL' : $param_value 
     984      ), 'WS'); 
    988985  } 
    989986 
     
    13991396function ws_images_exist($params, $service) 
    14001397{ 
    1401   ws_logfile(__FUNCTION__.' '.var_export($params, true)); 
    1402  
    1403   global $conf; 
     1398  global $conf, $logger; 
     1399 
     1400  $logger->debug(__FUNCTION__, 'WS', $params); 
    14041401 
    14051402  $split_pattern = '/[\s,;\|]/'; 
     
    14721469function ws_images_checkFiles($params, $service) 
    14731470{ 
    1474   ws_logfile(__FUNCTION__.', input :  '.var_export($params, true)); 
     1471  global $logger; 
     1472 
     1473  $logger->debug(__FUNCTION__, 'WS', $params); 
    14751474 
    14761475  $query = ' 
     
    15101509  if (isset($compare_type)) 
    15111510  { 
    1512     ws_logfile(__FUNCTION__.', md5_file($path) = '.md5_file($path)); 
     1511    $logger->debug(__FUNCTION__.', md5_file($path) = '.md5_file($path), 'WS'); 
    15131512    if (md5_file($path) != $params[$compare_type.'_sum']) 
    15141513    { 
     
    15211520  } 
    15221521 
    1523   ws_logfile(__FUNCTION__.', output :  '.var_export($ret, true)); 
     1522  $logger->debug(__FUNCTION__, 'WS', $ret); 
    15241523 
    15251524  return $ret; 
Note: See TracChangeset for help on using the changeset viewer.