Advertisement
Guest User

Untitled

a guest
Jan 17th, 2018
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.44 KB | None | 0 0
  1. <?php
  2. namespace logger {
  3.     define('DS', DIRECTORY_SEPARATOR);
  4.     defined('DEBUG_LOGGER') or define('DEBUG_LOGGER', true);
  5.     defined('DIR_LOG') or define('DIR_LOG', $_SERVER['DOCUMENT_ROOT'] . DS . 'upload' . DS . 'log' . DS);
  6.  
  7.     final class CLogger
  8.     {
  9.         private static $fileExtension = '.log';
  10.  
  11.  
  12.         private static function initDir($dirName)
  13.         {
  14.             if (empty($dirName) === true || \is_string($dirName) === false) {
  15.                 if (self::isDebug() === true) {
  16.                     throw new \Exception('Invalid DirName: ' . $dirName);
  17.                 } else {
  18.                     return false;
  19.                 }
  20.             }
  21.             $dirLogger = $dirName . self::getActualDirName() . DS;
  22.             if (\file_exists($dirLogger) === false) {
  23.                 if (\mkdir($dirLogger, 0755, true) === false) {
  24.                     if (self::isDebug() === true) {
  25.                         throw new \Exception('Not Create Folder: ' . $dirLogger);
  26.                     } else {
  27.                         return false;
  28.                     }
  29.                 }
  30.             }
  31.             return $dirLogger;
  32.         }
  33.  
  34.         public static function addLog(/*args*/)
  35.         {
  36.             $args = \func_get_args();
  37.             $key = \array_shift($args);
  38.             $dirLogger = self::initDir(DIR_LOG . $key . DS);
  39.             if ($dirLogger === false)
  40.                 return false;
  41.  
  42.  
  43.             \array_unshift($args, date('H:i:s'));
  44.             $argsToSave = \implode("\r\n", $args) . "\r\n" . '--------------------------------------------------------------------' . "\r\n\n";
  45.             $fileFullPath = $dirLogger . \date('H') . self::getFileExtension();
  46.             if (\file_put_contents($fileFullPath, \print_r($argsToSave, true), FILE_APPEND) === false) {
  47.                 if (self::isDebug() === true) {
  48.                     throw  new \Exception('Not Write To File: ' . $fileFullPath);
  49.                 } else {
  50.                     return false;
  51.                 }
  52.             }
  53.             return true;
  54.         }
  55.  
  56.         private static function getActualDirName($format = 'Y-m-d')
  57.         {
  58.             return \date($format);
  59.         }
  60.  
  61.         private static function getFileExtension()
  62.         {
  63.             return self::$fileExtension;
  64.         }
  65.  
  66.         private static function isDebug()
  67.         {
  68.             return (\defined('DEBUG_LOGGER') && (DEBUG_LOGGER === true));
  69.         }
  70.     }
  71. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement