123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137 |
- <?php namespace Dever;
- use Dever;
- class Log
- {
- public static function add($log, $type = 1)
- {
- if (!$config = Config::get('setting')['log']) {
- return;
- }
- $config['level'] = $type;
- return self::push(self::filter($log), $config);
- }
- private static function push($log, $config)
- {
- $method = 'push_' . $config['type'];
- return self::$method($log, $config);
- }
- private static function push_http($log, $config)
- {
- return false;
- }
- private static function push_udp($log, $config)
- {
- $handle = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP);
- if ($handle) {
- socket_sendto($handle, $log, strlen($log), 0, $config['host'], $config['port']);
- socket_close($handle);
- }
- }
- private static function push_syslog($log, $config)
- {
- if ($config['level'] == 1) {
- $level = LOG_DEBUG;
- $name = LOG_LOCAL1;
- } elseif ($config['level'] == 2) {
- $level = LOG_NOTICE;
- $name = LOG_LOCAL2;
- } elseif ($config['level'] == 3) {
- $level = LOG_INFO;
- $name = LOG_LOCAL3;
- } else {
- $level = LOG_INFO;
- $name = LOG_LOCAL4;
- }
- openlog(DEVER_APP_NAME, LOG_PID, $name);
- syslog($level, $log);
- closelog();
- }
- private static function push_file($log, $config)
- {
- $day = date('Y/m/d');
- $time = date('H:i:s');
- $log = $day . ' ' . $time . ' ' . DEVER_PROJECT . ' ' . DEVER_APP_NAME . ' ' . $log . "\r\n";
- $file = self::file($config['level'], $day, substr($time, 0, 2));
- $size = 5242880;
- if (isset($config['size'])) {
- $size = $config['size'];
- }
- $exists = false;
- if (is_file($file)) {
- $exists = true;
- }
- if ($exists && $size <= filesize($file)) {
- rename($file, $file . '.' . str_replace(':', '_', $time) . '.bak');
- $exists = false;
- }
- $state = error_log($log, 3, $file);
- if ($state && !$exists) {
- @chmod($file, 0755);
- @system('chmod -R 777 ' . $file);
- }
- return $state;
- }
- public static function get($day, $type = 1)
- {
- if (is_array(Config::get('setting')['log'])) {
- $method = Config::get('setting')['log']['type'];
- } else {
- $method = 'syslog';
- }
- $method = 'get_' . $method;
- return self::$method($day, $type);
- }
- private static function get_http($day, $type)
- {
- return false;
- }
- private static function get_udp($day, $type)
- {
- return false;
- }
- private static function get_syslog($day, $type)
- {
- return false;
- }
- private static function get_file($day, $type)
- {
- $file = self::file($type, $day);
- $content = '';
- $path = dirname($file);
- if (is_dir($path)) {
- $dir = scandir($path);
- foreach ($dir as $k => $v) {
- if (strstr($v, $file[2])) {
- $content .= file_get_contents($path . $v);
- }
- }
- }
- if ($content) {
- return explode("\n", $content);
- }
- return array();
- }
- public static function filter($string)
- {
- if (is_array($string)) {
- $string = json_encode($string);
- }
- return $string;
- }
- private static function file($level, $day, $hour = '')
- {
- if ($level == 1) {
- $file = 'debug';
- } elseif ($level == 2) {
- $file = 'notice';
- } elseif ($level == 3) {
- $file = 'info';
- } else {
- $file = $level;
- }
- if ($hour) {
- $file .= '_' . $hour;
- }
- return File::get('logs/' . $day . DIRECTORY_SEPARATOR . $file);
- }
- }
|