wrote this file. * As long as you retain this notice you can do whatever you want with * this stuff. If we meet some day, and you think this stuff is worth it, * you can buy me a beer in return. * * @author Sven Strittmatter * @copyright Copyright (c) 2010, Sven Strittmatter. * @version 0.2.2 * @license http://www.weltraumschaf.de/the-beer-ware-license.txt */ /** * Simple logger class. * * Collects all log messages in an array. Has no persistence. */ class Api_Log { /** * Flags if logging is enabled. * * @var bool */ private static $isEnabled = true; /** * Stores the log messages. * * @var array */ private static $logLines = array(); private static $logFile; public static function setLogFile($fileName) { $fileName = (string) $fileName; if (!is_writable($fileName)) { throw new InvalidArgumentException("Given log file '{$fileName}' is not writeable!"); } self::$logFile = $fileName; } /** * Stores a log message. * * @param string $message */ public static function log($message) { if (self::$isEnabled) { self::$logLines[] = (string) $message; if (null !== self::$logFile) { file_put_contents(self::$logFile, (string) $message . PHP_EOL, FILE_APPEND); } } } /** * Get all log messages. * * @return array */ public static function get() { return self::$logLines; } /** * Enables logging. * * Logging is enabled by default, unless you call Api_Log::disable(). */ public static function enable() { self::$isEnabled = true; } /** * Disables logging. * * If you call this method all calls to Api_Log::log() will be ignored * until you call Api_Log::enable(). */ public static function disable() { self::$isEnabled = false; } /** * Deletes all logmessages. */ public static function clear() { self::$logLines = array(); } }