Вход Регистрация
* Taltos *

Логер, с созданием архивов за прошедший день

  1. class loger {
  2.  
  3.  
  4.  
  5. private static $user;
  6.  
  7. private static $dir;
  8.  
  9. private static $logdir = 'logs';
  10.  
  11. private static $archdir = 'archive';
  12.  
  13.  
  14.  
  15. public function __construct($user) {
  16.  
  17. if (!is_dir(self::$logdir . DIRECTORY_SEPARATOR . date('Y-m-d'))) {
  18.  
  19. if (!mkdir(self::$logdir . DIRECTORY_SEPARATOR . date('Y-m-d'), 0777, true)) {
  20.  
  21. #throw new Exception('WTF, log dir not created');
  22.  
  23. }
  24.  
  25. }
  26.  
  27. if (!is_dir(self::$archdir)) {
  28.  
  29. if (!mkdir(self::$archdir, 0777, true)) {
  30.  
  31. #throw new Exception('WTF, archive dir not created');
  32.  
  33. }
  34.  
  35. }
  36.  
  37. self::$dir = self::$logdir . DIRECTORY_SEPARATOR . date('Y-m-d');
  38.  
  39. self::$user = $user;
  40.  
  41. }
  42.  
  43.  
  44.  
  45. public static function writelog($text) {
  46.  
  47. if (self::$dir === null) {
  48.  
  49. #throw new Exception('WTF, dir not initialiting');
  50.  
  51. } else {
  52.  
  53. $file = self::$dir . DIRECTORY_SEPARATOR . self::$user . '.txt';
  54.  
  55. file_put_contents($file, $text . PHP_EOL, FILE_APPEND | LOCK_EX);
  56.  
  57. }
  58.  
  59. self::logrotate();
  60.  
  61. }
  62.  
  63.  
  64.  
  65. public static function logrotate() {
  66.  
  67. $sda = array_diff(scandir(self::$logdir), array('.', '..'));
  68.  
  69. if (sizeof($sda)>0) {
  70.  
  71. foreach ($sda as $dir) {
  72.  
  73. if ($dir != date('Y-m-d')) {
  74.  
  75. $zip = new ZipArchive;
  76.  
  77. if ($zip->open(self::$archdir . DIRECTORY_SEPARATOR . $dir . '.zip', ZipArchive::CREATE) === TRUE) {
  78.  
  79. $zip->addEmptyDir($dir);
  80.  
  81. $sdf = array_diff(scandir(self::$logdir . DIRECTORY_SEPARATOR . $dir), array('.', '..'));
  82.  
  83. foreach ($sdf as $file) {
  84.  
  85. $zip->addFile(self::$logdir . DIRECTORY_SEPARATOR . $dir . DIRECTORY_SEPARATOR . $file, $dir . DIRECTORY_SEPARATOR . $file);
  86.  
  87. }
  88.  
  89. $zip->close();
  90.  
  91. } else {
  92.  
  93. #throw new Exception('WTF, archive not created');
  94.  
  95. }
  96.  
  97. if (is_file(self::$archdir . DIRECTORY_SEPARATOR . $dir . '.zip')) {
  98.  
  99. self::deldir(self::$logdir . DIRECTORY_SEPARATOR . $dir);
  100.  
  101. }
  102.  
  103. }
  104.  
  105. }
  106.  
  107. }
  108.  
  109. }
  110.  
  111.  
  112.  
  113. public static function deldir($dir) {
  114.  
  115. $dirarr = array_diff(scandir($dir), array('.', '..'));
  116.  
  117. if (sizeof($dirarr)>0) {
  118.  
  119. foreach ($dirarr as $file) {
  120.  
  121. unlink($dir . DIRECTORY_SEPARATOR . $file);
  122.  
  123. }
  124.  
  125. }
  126.  
  127. rmdir($dir);
  128.  
  129. }
  130.  
  131.  
  132.  
  133. }
» Описание: пользоваться так

include('koelog.php');
new loger($user_name);
loger::writelog('test1');
loger::writelog('test2');
loger::writelog('test3');
loger::writelog('test4');
» Время добавления: 12 Марта 2015 в 11:25
» Посмотров: 984
» textarea
» Рейтинг: [+0 | -0]
Комментарии [0]
Онлайн: 1
Реклама