/** * Accumulate log messages, but also append them to a running log file for easy viewing. */ public static function log($msg, $before = null) { static $flushed = false; if ($before) { self::$_log = "{$msg}\n" . self::$_log; } else { self::$_log .= "{$msg}\n"; } $registry = Zend_Registry::getInstance(); // performance is not an issue for this demo, so just sync to disk everytime if (isset($registry['config'])) { if ($flushed) { file_put_contents($registry['config']->log, "{$msg}\n", FILE_APPEND); } else { file_put_contents($registry['config']->log, self::$_log); $flushed = true; } } }
/** * Accumulate log messages, but also append them to a running log file for easy viewing. */ public static function log($msg, $before = null) { static $flushed = false; if ($before) { self::$_log = "{$msg}\n" . self::$_log; } else { self::$_log .= "{$msg}\n"; } $registry = Zend_Registry::getInstance(); // performance is not an issue for this demo, so just sync to disk everytime if (isset($registry['config'])) { if ($flushed) { file_put_contents($registry['config']->log, "{$msg}\n", FILE_APPEND); } else { file_put_contents($registry['config']->log, self::$_log); $flushed = true; ///////////////////////////// // ==> SECTION: log <== require 'Zend/Log.php'; require 'Zend/Log/Formatter/Simple.php'; require 'Zend/Log/Writer/Stream.php'; $filename = $registry['config']->log . '.error'; $errorLogStream = @fopen($filename, 'wb', false); if ($errorLogStream === false) { throw new ZFDemo_Exception("CRITICAL: Can not open log file '{$filename}' for writing", 500); } $filename = $registry['config']->log . '.verbose'; $verboseLogStream = @fopen($filename, 'wb', false); if ($verboseLogStream === false) { throw new ZFDemo_Exception("CRITICAL: Can not open log file '{$filename}' for writing", 500); } // create a custom formatter for our log writers, so that we can include the custom user %id% field $formatter = new Zend_Log_Formatter_Simple('%timestamp% %priorityName% (%priority%) %id%: %message%' . PHP_EOL); // create a log writer for errors, using the previously opened stream $errorWriter = new Zend_Log_Writer_Stream($errorLogStream); $errorWriter->setFormatter($formatter); $filter = new Zend_Log_Filter_Priority(Zend_Log::ERR); $errorWriter->addFilter($filter); // log messages lower than priority ERR are ignored by this writer // create a log writer for all messages, using the previously opened stream $verboseWriter = new Zend_Log_Writer_Stream($verboseLogStream); $verboseWriter->setFormatter($formatter); // all log messages are logged by this writer // finally, create a logger and attach the two log writers to the logger self::$_logger = new Zend_Log($errorWriter); self::$_logger->addWriter($verboseWriter); self::$_logger->pause = false; } } self::$_messages[] = $msg; if (self::$_logger && !self::$_logger->pause) { while (count(self::$_messages)) { $msg = array_shift(self::$_messages); // Each log event has: timestamp, message, priority, and priorityName // Now we add an additional datum, the authentication id (if any) of the current user. if (isset($registry['authenticationId']) && isset($registry['authenticationId']['username'])) { $id = $registry['authenticationId']['username'] . '.' . $registry['authenticationId']['realm']; } else { $id = '-'; } self::$_logger->setEventItem('id', $id); $priority = substr($msg, 0, $pos = strpos($msg, ' ')); if (isset($priorities[$priority])) { $priority = $priorities[$priority]; $msg = substr($msg, $pos + 1); } else { $priority = 7; // default to DEBUG priority } self::$_logger->log($msg, $priority); } } }