<?php

namespace JinDouYun\Controller\Common;
error_reporting(0);
/**
 * demo Logger::php_error_log(E_USER_ERROR,'查询企业列表失败',__CLASS__,__LINE__,$objDEnterprise->error());
 * 错误记录
 * Class Logger
 * @package JinDouYun\Controller\Common
 */
class Logger
{
    /**
     * @var string 外部php生成日志路基
     */
    private static $customFileLogDir = '/www/wwwroot/logs/';

    /**
     * 写入php错误日志
     *
     * @param string $errno 错误编号
     * @param string $errmsg 错误信息
     * @param string $filename 错误文件 __CLASS__
     * @param string $linenum 错误行数 __LINE__
     * @param mixed $vars 错误参数
     * @param boolean|string $custom 使用非容器内php时这个参数传true
     *
     */
    public static function logs($errno, $errmsg, $filename, $linenum, $vars, $custom = false)
    {
        if (!defined('E_STRICT')) define('E_STRICT', 2048);

        $errortype = array(
            E_ERROR           => 'Error',
            E_WARNING         => 'Warning',
            E_PARSE           => 'Parsing Error',
            E_NOTICE          => 'Notice',
            E_CORE_ERROR      => 'Core Error',
            E_CORE_WARNING    => 'Core Warning',
            E_COMPILE_ERROR   => 'Compile Error',
            E_COMPILE_WARNING => 'Compile Warning',
            E_USER_ERROR      => 'User Error',
            E_USER_WARNING    => 'User Warning',
            E_USER_NOTICE     => 'User Notice',
            E_STRICT          => 'Runtime Notice'
        );
        $user_errors = array(E_USER_ERROR, E_USER_WARNING, E_USER_NOTICE);
        $filename = str_replace("\\", '/', $filename);
        $dt = date('Y-m-d H:i:s');

        $err = "#####" . $dt . "\n";
        $err .= "```" . "\n";
        $err .= "datetime     ：" . $dt . "\n";
        $err .= "errornum     ：" . $errno . "\n";
        $err .= "errormsg     ：" . $errortype[$errno] . "\n";
        $err .= "errortype    ：" . $errmsg . "\n";
        $err .= "scriptname   ：" . $filename . "\n";
        if (in_array($errno, $user_errors)) {
            //$err .= "scriptname   ：" . wddx_serialize_value($vars, "Variables") . "\n";
            $err .= "scriptname   ：" . serialize($vars) . "\n";
        }
        $err .= "scriptlinenum：" . $linenum . "\n";
        $err .= "```\n";

        if ($custom === false){
            $logDir = LOGS_DIR . 'SysError' . DS;
        }else{
            $logDir = self::$customFileLogDir . 'SysError' . DS;
        }

        $logFile = $logDir . date('m-d') . '.md';
        if (!is_dir($logDir)) {
            @mkdir($logDir);
        }

        @error_log($err, 3, $logFile);
        @chmod($logFile, 0777);
    }
}