Example #1
0
File: logger.php Project: rair/yacs
 /**
  * remember an event
  *
  * This script adds a line to temporary/log.txt, or to temporary/debug.txt.
  *
  * It the selected store is 'log', the function also submits a message to [code]syslog()[/code], to enable
  * distributed logging.
  *
  * Each line of the log store is made of fields separated by tabulations:
  * - time stamp (year-month-day hour:minutes:seconds)
  * - surfer name, if any, or '-'
  * - the label
  * - the description, if any
  *
  * Each line of the debug store is made of fields separated by tabulations:
  * - time stamp (year-month-day hour:minutes:seconds)
  * - the label
  * - the description, if any
  *
  * The description is truncated after 4 kbytes.
  *
  * @param string a one-line label that can be used as a mail title (e.g. 'creation of a new article')
  * @param mixed a more comprehensive description, if any
  * @param string either 'log' or 'debug'
  * @return void
  */
 public static function remember($label, $description = '', $store = 'log')
 {
     global $context;
     // ensure we have a string
     $description = Logger::to_string($description, $store != 'debug');
     // cap the description, just in case...
     $description = substr($description, 0, 8192);
     // event saved for debugging purpose
     if ($store == 'debug') {
         $store = 'temporary/debug.txt';
         // stamp the line
         $line = gmdate('Y-m-d H:i:s') . "\t" . $label . ' ' . $description;
         // event saved for later review
     } else {
         $store = 'temporary/log.txt';
         // surfer name -- surfer.php may not be loaded yet
         if (isset($_SESSION['surfer_name'])) {
             $name = preg_replace('/(@.+)$/', '', $_SESSION['surfer_name']);
         } else {
             $name = '-';
         }
         // cap the description, just in case...
         $description = substr($description, 0, 2048);
         // strip control chars, encode new lines, single space
         $from = array('/\\r/', '/\\n/', '/\\s+/');
         $to = array('', '\\n', ' ');
         // make a line
         $line = gmdate('Y-m-d H:i:s') . "\t" . $name . "\t" . preg_replace($from, $to, strip_tags($label)) . "\t" . preg_replace($from, $to, $description);
         // save it through syslog
         Safe::syslog(LOG_INFO, $line);
     }
     // save it in a local file
     if ($handle = Safe::fopen($context['path_to_root'] . $store, 'a')) {
         fwrite($handle, $line . "\n");
         fclose($handle);
     }
 }