Пример #1
0
 public static function log($txt = '', $lvl = -1)
 {
     if (self::$logFile && !self::$logHandle) {
         if (!file_exists(self::$logFile)) {
             self::$logHandle = fopen(self::$logFile, 'w');
         } else {
             $logFileParts = pathinfo(self::$logFile);
             $i = 1;
             while (file_exists($logFileParts['dirname'] . '/' . $logFileParts['filename'] . $i . (isset($logFileParts['extension']) ? '.' . $logFileParts['extension'] : ''))) {
                 $i++;
             }
             self::$logFile = $logFileParts['dirname'] . '/' . $logFileParts['filename'] . $i . (isset($logFileParts['extension']) ? '.' . $logFileParts['extension'] : '');
             self::$logHandle = fopen(self::$logFile, 'w');
         }
     }
     $msg = "\n";
     if ($txt) {
         $msg = str_pad(date('H:i:s'), 10);
         switch ($lvl) {
             case self::LOG_ERROR:
                 // red      critical error
                 $msg .= '[' . self::red('ERR') . ']   ';
                 break;
             case self::LOG_WARN:
                 // yellow   notice
                 $msg .= '[' . self::yellow('WARN') . ']  ';
                 break;
             case self::LOG_OK:
                 // green    success
                 $msg .= '[' . self::green('OK') . ']    ';
                 break;
             case self::LOG_INFO:
                 // blue     info
                 $msg .= '[' . self::blue('INFO') . ']  ';
                 break;
             default:
                 $msg .= '        ';
         }
         $msg .= $txt . "\n";
     }
     echo $msg;
     if (self::$logHandle) {
         // remove highlights for logging
         fwrite(self::$logHandle, preg_replace(["/\\[\\d+m/", "/\\[0m/"], '', $msg));
     }
     flush();
 }