/fuel/core/classes/log.php
PHP | 192 lines | 95 code | 30 blank | 67 comment | 12 complexity | 0a6e7b9a376010e7859d7c537bdc960f MD5 | raw file
Possible License(s): MIT
- <?php
- /**
- * Part of the Fuel framework.
- *
- * @package Fuel
- * @version 1.0
- * @author Fuel Development Team
- * @license MIT License
- * @copyright 2010 - 2012 Fuel Development Team
- * @link http://fuelphp.com
- */
- namespace Fuel\Core;
- /**
- * Log Class
- *
- * @package Fuel
- * @category Logging
- * @author Phil Sturgeon
- * @link http://docs.fuelphp.com/classes/log.html
- */
- class Log
- {
- public static function _init()
- {
- \Config::load('file', true);
- // make sure the configured chmod values are octal
- $chmod = \Config::get('file.chmod.folders', 0777);
- is_string($chmod) and \Config::set('file.chmod.folders', octdec($chmod));
- $chmod = \Config::get('file.chmod.files', 0666);
- is_string($chmod) and \Config::set('file.chmod.files', octdec($chmod));
- }
- /**
- * Logs a message with the Info Log Level
- *
- * @param string $msg The log message
- * @param string $method The method that logged
- * @return bool If it was successfully logged
- */
- public static function info($msg, $method = null)
- {
- return static::write(\Fuel::L_INFO, $msg, $method);
- }
- /**
- * Logs a message with the Debug Log Level
- *
- * @param string $msg The log message
- * @param string $method The method that logged
- * @return bool If it was successfully logged
- */
- public static function debug($msg, $method = null)
- {
- return static::write(\Fuel::L_DEBUG, $msg, $method);
- }
- /**
- * Logs a message with the Warning Log Level
- *
- * @param string $msg The log message
- * @param string $method The method that logged
- * @return bool If it was successfully logged
- */
- public static function warning($msg, $method = null)
- {
- return static::write(\Fuel::L_WARNING, $msg, $method);
- }
- /**
- * Logs a message with the Error Log Level
- *
- * @param string $msg The log message
- * @param string $method The method that logged
- * @return bool If it was successfully logged
- */
- public static function error($msg, $method = null)
- {
- return static::write(\Fuel::L_ERROR, $msg, $method);
- }
- /**
- * Write Log File
- *
- * Generally this function will be called using the global log_message() function
- *
- * @access public
- * @param int|string the error level
- * @param string the error message
- * @param string information about the method
- * @return bool
- */
- public static function write($level, $msg, $method = null)
- {
- // defined default error labels
- static $labels = array(
- 1 => 'Error',
- 2 => 'Warning',
- 3 => 'Debug',
- 4 => 'Info',
- );
- // get the levels defined to be logged
- $loglabels = \Config::get('log_threshold');
- // bail out if we don't need logging at all
- if ($loglabels == \Fuel::L_NONE)
- {
- return false;
- }
- // if it's not an array, assume it's an "up to" level
- if ( ! is_array($loglabels))
- {
- $loglabels = array_keys(array_slice($labels, 0, $loglabels, true));
- }
- // if $level is string, it is custom level.
- if (is_int($level))
- {
- // do we need to log the message with this level?
- if ( ! in_array($level, $loglabels))
- {
- return false;
- }
-
- // store the label for this level for future use
- $level = $labels[$level];
- }
- // if profiling is active log the message to the profile
- if (Config::get('profiling'))
- {
- \Console::log($method.' - '.$msg);
- }
- // and write it to the logfile
- $filepath = \Config::get('log_path').date('Y/m').'/';
- if ( ! is_dir($filepath))
- {
- $old = umask(0);
- mkdir($filepath, \Config::get('file.chmod.folders', 0777), true);
- umask($old);
- }
- $filename = $filepath.date('d').'.php';
- $message = '';
- if ( ! $exists = file_exists($filename))
- {
- $message .= "<"."?php defined('COREPATH') or exit('No direct script access allowed'); ?".">".PHP_EOL.PHP_EOL;
- }
- if ( ! $fp = @fopen($filename, 'a'))
- {
- return false;
- }
- $call = '';
- if ( ! empty($method))
- {
- $call .= $method;
- }
- $message .= $level.' '.(($level == 'info') ? ' -' : '-').' ';
- $message .= date(\Config::get('log_date_format'));
- $message .= ' --> '.(empty($call) ? '' : $call.' - ').$msg.PHP_EOL;
- flock($fp, LOCK_EX);
- fwrite($fp, $message);
- flock($fp, LOCK_UN);
- fclose($fp);
- if ( ! $exists)
- {
- $old = umask(0);
- @chmod($filename, \Config::get('file.chmod.files', 0666));
- umask($old);
- }
- return true;
- }
- }