/** * creates a singleton ZLog object * * @param array|int $options create options if array, minimal log level if int * * @return ZLogger */ public static function singleton($options = array()) { if (empty(self::$instance)) { self::$instance = new self(true); // possible options (first is default) $option_control = array('min_level' => array(self::LOG_LEVEL_DEBUG, self::LOG_LEVEL_MESSAGE, self::LOG_LEVEL_CRAZY, self::LOG_LEVEL_NOTICE, self::LOG_LEVEL_WARNING, self::LOG_LEVEL_ERROR, self::LOG_LEVEL_FATAL, self::LOG_LEVEL_CRASH, self::LOG_LEVEL_ALARM), 'target' => array('stdout', 'file', 'return'), 'locale' => array('EN', 'RU'), 'format' => array('html', 'plain'), 'line_delimiter' => array('<br />', PHP_EOL), 'flush' => array('manual', 'immediate'), 'mem_unit' => array('auto', 'kilo', 'byte', 'mega'), 'rewrite_log' => array(false, true), 'default_level' => array(self::LOG_LEVEL_MESSAGE, self::LOG_LEVEL_DEBUG, self::LOG_LEVEL_CRAZY, self::LOG_LEVEL_NOTICE, self::LOG_LEVEL_WARNING, self::LOG_LEVEL_ERROR, self::LOG_LEVEL_FATAL, self::LOG_LEVEL_CRASH, self::LOG_LEVEL_ALARM)); // some options are unrestricted, so we only need to check if they are set $option_defaults = array('filename' => 'default.log', 'output_format' => '%time_delta_start% (%time_delta_prev%) %memory_color_start%%memory_delta_prev%%memory_color_end% [%level%] %message%'); // simple call detection if (!is_array($options)) { $options = array('min_level' => $options); } // set default values to unrestricted options if not set foreach ($option_defaults as $option_name => $value) { if (!isset($options[$option_name])) { $options[$option_name] = $value; } } // parse init values and set runtime options foreach ($option_control as $option_name => $values) { if (!isset($options[$option_name]) || !in_array($options[$option_name], $values)) { $options[$option_name] = $values[0]; } } // some more checks if ($options['target'] == 'file' && $options['filename'] == '') { self::$bad_config = true; trigger_error('ZLog: Unable to create logger: target is file, but filename was not specified. No log will be collected.', E_USER_WARNING); return self::$instance; } self::$options = $options; // apply language self::$instance->setLocale($options['locale']); // initial start values self::$time_start = microtime(true); self::$memory_start = memory_get_usage(); self::$flushed_to = -1; // recycle prev log if requested if (self::$options['rewrite_log'] && self::$options['target'] == 'file' && self::$options['filename'] > '') { if (file_exists(self::$options['filename'])) { if (!unlink(self::$options['filename'])) { trigger_error('ZLOG: An error occured while deleting previous log (' . self::$options['filename'] . '). Check if file exists and writable.', E_USER_WARNING); } } } self::log('***************************************************************', self::LOG_LEVEL_MESSAGE); self::log(self::$locale['logger_started'], self::LOG_LEVEL_MESSAGE); // dump options in debug mode foreach (self::$options as $option_name => $value) { self::log('option set: ' . $option_name . ' = ' . $value, self::LOG_LEVEL_DEBUG); } } return self::$instance; }