<?php

namespace App\Lib;

use Base\Log\Log;

class AliLog
{
    protected $appName = '';
    protected $logDir = '';
    protected $requestPath;
    /**
     * UserLog constructor.
     * @param array $config
     * @param $path
     */
    public function __construct(array $config, $path)
    {
        $this->appName = $config['appName'];
        $this->logDir = $config['logDir'];
        Log::init($this->appName, $this->logDir);
        $this->requestPath =  $path;
    }

    /**
     * @param $level
     * @param int $code
     * @param string $msg
     * @param string $ext
     * @param $userInfo
     * @return bool
     * @throws \Exception
     */
    public function recordLog($level, $code = 0, $msg = '', $userInfo = '', $ext = '')
    {
        switch ($level) {
            case 'debug':
                Log::debug($this->requestPath, $userInfo, $code, $msg, $ext);
                break;
            case 'info':
                Log::info($this->requestPath, $userInfo, $code, $msg, $ext);
                break;
            case 'warn':
                Log::warn($this->requestPath, $userInfo, $code, $msg, $ext);
                break;
            case 'error':
                Log::error($this->requestPath, $userInfo, $code, $msg, $ext);
                break;
            case 'fatal':
                Log::fatal($this->requestPath, $userInfo, $code, $msg, $ext);
                break;
            default:
                Log::info($this->requestPath, $userInfo, $code, $msg, $ext);
                break;
        }
        Log::flush();
        return true;
    }
}