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

Namespaces

  • Alo
    • Cache
    • CLI
    • Controller
    • Db
      • Query
    • Exception
    • FileSystem
    • Session
    • Traversables
    • Validators
    • Windows
  • Controller
  • None
  • PHP

Classes

  • Alo
  • Log

Functions

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