php记录日志方法

来源:互联网 发布:php 二进制打印出来 编辑:程序博客网 时间:2024/06/07 06:29
/**
 * 日志记录
 *
 * @param string $type
 * @param mixed $msg
 */
function logger($type, $msg)
{
    if (!is_string($msg)) {
        $msg = json_encode($msg);
    }
    $data = array_merge($_GET, $_POST);
    $url = explode('?', $_SERVER['REQUEST_URI'])[0] . '?' . urldecode(http_build_query($data));
    $ip = ip_address();
    $pid = function_exists('posix_getpid') ? posix_getpid() : '0';
    $content = sprintf("[%s]%s,%s,%s|%s|%s\r\n", date('Y-m-d H:i:s'), $uid, $ip, $pid, $msg, $url);
    $type = str_replace(':', '_', strtolower($type));
    $file = '/data/logs/' . $_SERVER["HTTP_HOST"] . '/' . $type . date('Ymd') . '.log';
    is_dir(dirname($file)) or mkdir(dirname($file), 0755, true);
    file_put_contents($file, $content, FILE_APPEND);
}
0 0
原创粉丝点击