Log.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. <?php namespace Dever;
  2. use Dever;
  3. class Log
  4. {
  5. public static function add($log, $type = 1)
  6. {
  7. if (!$config = Config::get('setting')['log']) {
  8. return;
  9. }
  10. $config['level'] = $type;
  11. return self::push(self::filter($log), $config);
  12. }
  13. private static function push($log, $config)
  14. {
  15. $method = 'push_' . $config['type'];
  16. return self::$method($log, $config);
  17. }
  18. private static function push_http($log, $config)
  19. {
  20. return false;
  21. }
  22. private static function push_udp($log, $config)
  23. {
  24. $handle = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP);
  25. if ($handle) {
  26. socket_sendto($handle, $log, strlen($log), 0, $config['host'], $config['port']);
  27. socket_close($handle);
  28. }
  29. }
  30. private static function push_syslog($log, $config)
  31. {
  32. if ($config['level'] == 1) {
  33. $level = LOG_DEBUG;
  34. $name = LOG_LOCAL1;
  35. } elseif ($config['level'] == 2) {
  36. $level = LOG_NOTICE;
  37. $name = LOG_LOCAL2;
  38. } elseif ($config['level'] == 3) {
  39. $level = LOG_INFO;
  40. $name = LOG_LOCAL3;
  41. } else {
  42. $level = LOG_INFO;
  43. $name = LOG_LOCAL4;
  44. }
  45. openlog(DEVER_APP_NAME, LOG_PID, $name);
  46. syslog($level, $log);
  47. closelog();
  48. }
  49. private static function push_file($log, $config)
  50. {
  51. $day = date('Y/m/d');
  52. $time = date('H:i:s');
  53. $log = $day . ' ' . $time . ' ' . DEVER_PROJECT . ' ' . DEVER_APP_NAME . ' ' . $log . "\r\n";
  54. $file = self::file($config['level'], $day, substr($time, 0, 2));
  55. $size = 5242880;
  56. if (isset($config['size'])) {
  57. $size = $config['size'];
  58. }
  59. $exists = false;
  60. if (is_file($file)) {
  61. $exists = true;
  62. }
  63. if ($exists && $size <= filesize($file)) {
  64. rename($file, $file . '.' . str_replace(':', '_', $time) . '.bak');
  65. $exists = false;
  66. }
  67. $state = error_log($log, 3, $file);
  68. if ($state && !$exists) {
  69. @chmod($file, 0755);
  70. @system('chmod -R 777 ' . $file);
  71. }
  72. return $state;
  73. }
  74. public static function get($day, $type = 1)
  75. {
  76. if (is_array(Config::get('setting')['log'])) {
  77. $method = Config::get('setting')['log']['type'];
  78. } else {
  79. $method = 'syslog';
  80. }
  81. $method = 'get_' . $method;
  82. return self::$method($day, $type);
  83. }
  84. private static function get_http($day, $type)
  85. {
  86. return false;
  87. }
  88. private static function get_udp($day, $type)
  89. {
  90. return false;
  91. }
  92. private static function get_syslog($day, $type)
  93. {
  94. return false;
  95. }
  96. private static function get_file($day, $type)
  97. {
  98. $file = self::file($type, $day);
  99. $content = '';
  100. $path = dirname($file);
  101. if (is_dir($path)) {
  102. $dir = scandir($path);
  103. foreach ($dir as $k => $v) {
  104. if (strstr($v, $file[2])) {
  105. $content .= file_get_contents($path . $v);
  106. }
  107. }
  108. }
  109. if ($content) {
  110. return explode("\n", $content);
  111. }
  112. return array();
  113. }
  114. public static function filter($string)
  115. {
  116. if (is_array($string)) {
  117. $string = json_encode($string);
  118. }
  119. return $string;
  120. }
  121. private static function file($level, $day, $hour = '')
  122. {
  123. if ($level == 1) {
  124. $file = 'debug';
  125. } elseif ($level == 2) {
  126. $file = 'notice';
  127. } elseif ($level == 3) {
  128. $file = 'info';
  129. } else {
  130. $file = $level;
  131. }
  132. if ($hour) {
  133. $file .= '_' . $hour;
  134. }
  135. return File::get('logs/' . $day . DIRECTORY_SEPARATOR . $file);
  136. }
  137. }