AloFramework documentation
  • Namespace
  • Class
  • Tree
  • Deprecated
  • Todo
  • Download

Namespaces

  • Alo
    • Cache
    • CLI
    • Controller
    • Db
    • Exception
    • FileSystem
    • Session
    • Statics
    • Test
    • Validators
    • Windows
  • Controller
  • None
  • PHP

Classes

  • Alo
  • Log

Functions

  • debug
  • escape_html5
  • get
  • getallheaders
  • includeifexists
  • includeonceifexists
  • lite_debug
  • server_is_windows
  • timestamp_precise
  1 <?php
  2 
  3    if (!defined('GEN_START')) {
  4       http_response_code(404);
  5       die();
  6    } elseif (!defined('LOG_LEVEL') || !defined('LOG_LEVEL_ERROR') || !defined('LOG_LEVEL_DEBUG') || !defined('LOG_LEVEL_NONE') || !in_array(LOG_LEVEL, [
  7          LOG_LEVEL_ERROR,
  8          LOG_LEVEL_DEBUG,
  9          LOG_LEVEL_NONE
 10       ], true)
 11    ) {
 12       die('Invalid LOG_LEVEL setting. Valid values are the framework constants LOG_LEVEL_ERROR, LOG_LEVEL_DEBUG and LOG_LEVEL_NONE!');
 13    }
 14 
 15    /**
 16     * Logs messages
 17     *
 18     * @author Art <a.molcanovas@gmail.com>
 19     */
 20    abstract class Log {
 21 
 22       /**
 23        * Identifier for error messages
 24        *
 25        * @var string
 26        */
 27       const MSG_ERROR = 'ERROR';
 28 
 29       /**
 30        * Identifier for debug messages
 31        *
 32        * @var string
 33        */
 34       const MSG_DEBUG = 'DEBUG';
 35 
 36       /**
 37        * The logging level
 38        *
 39        * @var string
 40        */
 41       protected static $log_level = LOG_LEVEL;
 42 
 43       /**
 44        * Today's date
 45        *
 46        * @var string
 47        */
 48       protected static $today;
 49 
 50       /**
 51        * Gets or sets the log level. If no parameter is passed, returns
 52        * self::$log_level, if it's set and has a valid value, sets it and
 53        * returns TRUE;
 54        *
 55        * @author Art <a.molcanovas@gmail.com>
 56        * @param string|null $level The logging level
 57        * @return boolean|string
 58        * @see    self::$log_level
 59        */
 60       static function log_level($level = null) {
 61          if (in_array($level, [
 62             LOG_LEVEL_DEBUG,
 63             LOG_LEVEL_ERROR,
 64             LOG_LEVEL_NONE
 65          ], true)) {
 66             self::$log_level = $level;
 67 
 68             return true;
 69          } else {
 70             return self::$log_level;
 71          }
 72       }
 73 
 74       /**
 75        * Logs a debug level message
 76        *
 77        * @author Art <a.molcanovas@gmail.com>
 78        * @param string $msg   The message
 79        * @param array  $trace optionally, supply the debug trace
 80        * @return string The message you passed on
 81        */
 82       static function debug($msg, $trace = null) {
 83          if (self::$log_level === LOG_LEVEL_DEBUG && is_scalar($msg)) {
 84             self::do_write($msg, self::MSG_DEBUG, $trace);
 85          }
 86 
 87          return $msg;
 88       }
 89 
 90       /**
 91        * Logs a error level message
 92        *
 93        * @author Art <a.molcanovas@gmail.com>
 94        * @param string $msg   The message
 95        * @param array  $trace optionally, supply the debug trace
 96        * @return string The message you passed on
 97        */
 98       static function error($msg, $trace = null) {
 99          if (self::$log_level != LOG_LEVEL_NONE && is_scalar($msg)) {
100             self::do_write($msg, self::MSG_ERROR, $trace);
101          }
102 
103          return $msg;
104       }
105 
106       /**
107        * Performs the write operation
108        *
109        * @author Art <a.molcanovas@gmail.com>
110        * @param string $msg   The message to log
111        * @param string $level The level of the message
112        * @param array  $trace optionally, supply the debug trace
113        * @return boolean
114        */
115       protected static function do_write($msg, $level, $trace = null) {
116          $filepath = DIR_APP . 'logs' . DIRECTORY_SEPARATOR . date('Y-m-d') . '.log';
117          $fp = @fopen($filepath, 'ab');
118 
119          if (!$fp) {
120             return false;
121          } else {
122             if (!$trace || !is_array($trace)) {
123                $trace = debug_backtrace();
124                array_shift($trace);
125             }
126 
127             if (defined('LOG_INTENSE') && LOG_INTENSE) {
128                $trace_append = serialize($trace);
129             } else {
130                $xpl = explode(DIR_INDEX, isset($trace[0]['file']) ? $trace[0]['file'] : null);
131 
132                $trace_append = (isset($trace[0]['line']) ? $trace[0]['line'] : '[unknown line]') . ' @ "'
133                   . str_replace('"', '\"', isset($xpl[1]) ? $xpl[1] : $xpl[0]) . '"';
134             }
135 
136             $message = str_pad('[' . timestamp_precise() . ']', 25, ' ')
137                . ' ' . str_pad($level, 5, ' ') . ' | "'
138                . str_replace('"', '\"', $msg) . '" | ' . $trace_append . PHP_EOL;
139 
140             flock($fp, LOCK_EX);
141             fwrite($fp, $message);
142             flock($fp, LOCK_UN);
143             fclose($fp);
144 
145             @chmod($filepath, '0666');
146 
147             return true;
148          }
149       }
150 
151    }
AloFramework documentation API documentation generated by ApiGen 2.8.0