<?php

namespace app\service;

use think\Service;
use think\facade\Log;
use think\exception\ErrorException;

class BaseService extends Service
{
    private $className = '';
    public function __construct($className)
    {
        $this->className = $className;
    }
    protected function log_notice($any, $msg = '', $tag = 'log_notice')
    {
        if ($any instanceof ErrorException || $any instanceof Exception) {
            $str = c_formate_exception($any);
        } else {
            $str = json_encode($any, JSON_UNESCAPED_UNICODE);
        }
        Log::write('[' . $this->className . ']' . '[' . $tag . ']' . '[' . $msg . ']' . '[' . $str . ']', 'NOTICE');
    }
    protected function log_warn($any, $msg = '', $tag = 'log_warn')
    {
        if ($any instanceof ErrorException || $any instanceof Exception) {
            $str = c_formate_exception($any);
        } else {
            $str = json_encode($any, JSON_UNESCAPED_UNICODE);
        }
        Log::write('[' . $this->className . ']' . '[' . $tag . ']' . '[' . $msg . ']' . '[' . json_encode($any, JSON_UNESCAPED_UNICODE) . ']', 'WARN');
    }
    protected function log_error($any, $msg = '', $tag = 'log_error')
    {
        if ($any instanceof ErrorException || $any instanceof Exception) {
            $str = c_formate_exception($any);
        } else {
            $str = json_encode($any, JSON_UNESCAPED_UNICODE);
        }
        Log::write('[' . $this->className . ']' . '[' . $tag . ']' . '[' . $msg . ']' . '[' . json_encode($any, JSON_UNESCAPED_UNICODE) . ']', 'ERROR');
    }
    protected function alarm($type = '', $content = '')
    {
        D('alarm')->insert([
            'type' => $type,
            'content' => $content,
            'create_time' => c_now(),
        ]);
    }
}
