Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- namespace logger {
- define('DS', DIRECTORY_SEPARATOR);
- defined('DEBUG_LOGGER') or define('DEBUG_LOGGER', true);
- defined('DIR_LOG') or define('DIR_LOG', $_SERVER['DOCUMENT_ROOT'] . DS . 'upload' . DS . 'log' . DS);
- final class CLogger
- {
- private static $fileExtension = '.log';
- private static function initDir($dirName)
- {
- if (empty($dirName) === true || \is_string($dirName) === false) {
- if (self::isDebug() === true) {
- throw new \Exception('Invalid DirName: ' . $dirName);
- } else {
- return false;
- }
- }
- $dirLogger = $dirName . self::getActualDirName() . DS;
- if (\file_exists($dirLogger) === false) {
- if (\mkdir($dirLogger, 0755, true) === false) {
- if (self::isDebug() === true) {
- throw new \Exception('Not Create Folder: ' . $dirLogger);
- } else {
- return false;
- }
- }
- }
- return $dirLogger;
- }
- public static function addLog(/*args*/)
- {
- $args = \func_get_args();
- $key = \array_shift($args);
- $dirLogger = self::initDir(DIR_LOG . $key . DS);
- if ($dirLogger === false)
- return false;
- \array_unshift($args, date('H:i:s'));
- $argsToSave = \implode("\r\n", $args) . "\r\n" . '--------------------------------------------------------------------' . "\r\n\n";
- $fileFullPath = $dirLogger . \date('H') . self::getFileExtension();
- if (\file_put_contents($fileFullPath, \print_r($argsToSave, true), FILE_APPEND) === false) {
- if (self::isDebug() === true) {
- throw new \Exception('Not Write To File: ' . $fileFullPath);
- } else {
- return false;
- }
- }
- return true;
- }
- private static function getActualDirName($format = 'Y-m-d')
- {
- return \date($format);
- }
- private static function getFileExtension()
- {
- return self::$fileExtension;
- }
- private static function isDebug()
- {
- return (\defined('DEBUG_LOGGER') && (DEBUG_LOGGER === true));
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement