UdpLog.php 4.53 KB
<?php
/**
 * Created by IntelliJ IDEA.
 * User: hbomb_000
 * Date: 2016/5/5
 * Time: 18:17
 */

namespace WebPlugin;


/**
 * Class UdpLog
 * @useage:
 * UdpLog::info('get payment list begin',array('order_code'=>123231));
 * @package WebPlugin
 */
class UdpLog
{
    //日志开关
    private static $flag = true;
    //influxdb url
    public static $url = 'influxdb.yohobuy.com';
    //influxdb port
    public static $port = '4444';
    //influxdb measurement
    public static $measurement = 'php_log';
    
    public static $filePath = '/Data/logs/pc_pay';
    
    const RECORD_MODE_FILE = 'FILE';
    const RECORD_MODE_UDP = 'UDP';
    
    const RECORD_MODE = 'FILE';//mode: FILE | UDP

    /**
     * 初始化
     */
    private function __construct()
    {
        if (self::$flag) {
            return;
        }
    }
    /**
     * proc line and send log to influxdb
     * @param $level
     * @param $message
     * @param $meta
     */
    private static function procLog($level, $message, $debugInfo, $meta = '') {
        date_default_timezone_set('PRC');
        $level = str_replace(__CLASS__.'::','',$level);
        $file = $debugInfo[0]["file"];
        $line = $debugInfo[0]["line"];
        $string = '';
        //make tags
        $tags = array(
             'time' => date('Y-m-d H:i:s',time()),
              'level' => $level,
              'host'=> gethostname(),
              'file'=> $file,
              'line'=> $line,
              'message' => $message,
              'meta' =>  serialize($meta)
        );
        //make a line
        $string = implode('|', $tags); //format: time|level|host|file|line|message|meta
        if(self::RECORD_MODE == self::RECORD_MODE_UDP) {
            self::send($string);
        }
        else if(self::RECORD_MODE == self::RECORD_MODE_FILE) {
            self::fileLog($level, $string);
        }
    }

    /**
     * 文件日志记录
     * 
     * @param string $level
     * @param string $message
     */
    private static function fileLog($level, $message)
    {
        $filename = $level .'-pc-' . date('Ymd', time()). '.log'; //日志文件
        $logFile = self::createPath(self::$filePath, $filename);
        if (!file_exists(self::$filePath))//判断文件路径是否存在
        {
            if (!empty(self::$filePath))//判断路径是否为空
            {
                if (!(self::createDir(self::$filePath))) {
                    return false;
                }
                if (!is_writable($logFile)) {
                    return false;
                }
            }
        }
        @error_log($message."\r\n", 3, $logFile);
    }
    
    /**
     * 作用:创建目录
     * 输入:要创建的目录
     * 输出:true | false
     */
    private static function createDir($dir)
    {
        return is_dir($dir) or (self::createDir(dirname($dir)) and mkdir($dir, 0777));
    }
    
    /**
     * 作用:构建路径
     * 输入:文件的路径,要写入的文件名
     * 输出:构建好的路径字串
     */
    private static function createPath($dir, $filename)
    {
        if (empty($dir)) {
            return $filename;
        } else {
            return $dir . "/" . $filename;
        }
    }
    
    /**
     * send by udp
     * @param $string
     */
    private static function send($string) {
        $sock = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP);
        $len = strlen($string);
        socket_sendto($sock, $string, $len, 0, self::$url, self::$port);
        socket_close($sock);
    }

    /**
     * info log
     * @param $message
     * @param mixed $meta
     */
    public static function info($message, $meta = '') {
        self::procLog(__METHOD__, $message, debug_backtrace(), $meta);
    }

    /**
     * warn log
     * @param $message
     * @param mixed $meta
     */
    public static function warn($message, $meta = '') {
        self::procLog(__METHOD__, $message, debug_backtrace(), $meta);
    }

    /**
     * error log
     * @param $message
     * @param mixed $meta
     */
    public static function error($message, $meta = '') {
        self::procLog(__METHOD__, $message, debug_backtrace(), $meta);
    }

    /**
     * debug log
     * @param $message
     * @param mixed $meta
     */
    public static function debug($message, $meta = '') {
        self::procLog(__METHOD__, $message, debug_backtrace(), $meta);
    }

    /**
     * yaf trigger error
     * @param $message
     * @param mixed $meta
     */
    public static function triggerError($message, $meta = '') {
        self::procLog(__METHOD__, $message, debug_backtrace(), $meta);
    }
}