<?php<liu21st@gmail.com>
namespace think;
use think\exception\ClassNotFoundException;
class Log implements LoggerInterface
{
const EMERGENCY = 'emergency';
const ALERT = 'alert';
const CRITICAL = 'critical';
const ERROR = 'error';
const WARNING = 'warning';
const NOTICE = 'notice';
const INFO = 'info';
const DEBUG = 'debug';
const SQL = 'sql';
protected $log = [];
protected $config = [];
protected $driver;
protected $key;
protected $allowWrite = true;
protected $app;
public function __construct(App $app)
{
$this->app = $app;
}
public function init($config = [])
{
$type = isset($config['type']) ? $config['type'] : 'File';
$class = false !== strpos($type, '\\') ? $type : '\\think\\log\\driver\\' . ucwords($type);
$this->config = $config;
unset($config['type']);
if (!empty($config['close'])) {
$this->allowWrite = false;
}
if (class_exists($class)) {
$this->driver = new $class($config);
} else {
throw new ClassNotFoundException('class not exists:' . $class, $class);
}
$this->app->isDebug() && $this->record('[ LOG ] INIT ' . $type);
return $this;
}
public function getLog($type = '')
{
return $type ? $this->log[$type] : $this->log;
}
public function record($msg, $type = 'info', array $context = [])
{
if (!$this->allowWrite) {
return;
}
if (is_string($msg)) {
$replace = [];
foreach ($context as $key => $val) {
$replace['{' . $key . '}'] = $val;
}
$msg = strtr($msg, $replace);
}
$this->log[$type][] = $msg;
if (PHP_SAPI == 'cli') {
$this->save();
}
return $this;
}
public function clear()
{
$this->log = [];
return $this;
}
public function key($key)
{
$this->key = $key;
return $this;
}
public function check($config)
{
if ($this->key && !empty($config['allow_key']) && !in_array($this->key, $config['allow_key'])) {
return false;
}
return true;
}
public function close()
{
$this->allowWrite = false;
$this->log = [];
return $this;
}
public function save()
{
if (empty($this->log) || !$this->allowWrite) {
return true;
}
if (is_null($this->driver)) {
$this->init($this->app['config']->pull('log'));
}
if (!$this->check($this->config)) {
return false;
}
if (empty($this->config['level'])) {
$log = $this->log;
if (!$this->app->isDebug() && isset($log['debug'])) {
unset($log['debug']);
}
} else {
$log = [];
foreach ($this->config['level'] as $level) {
if (isset($this->log[$level])) {
$log[$level] = $this->log[$level];
}
}
}
$result = $this->driver->save($log);
if ($result) {
$this->log = [];
}
return $result;
}
public function write($msg, $type = 'info', $force = false)
{
$log = $this->log;
if (true === $force || empty($this->config['level'])) {
$log[$type][] = $msg;
} elseif (in_array($type, $this->config['level'])) {
$log[$type][] = $msg;
} else {
return false;
}
$this->app['hook']->listen('log_write', $log);
if (is_null($this->driver)) {
$this->init($this->app['config']->pull('log'));
}
$result = $this->driver->save($log);
if ($result) {
$this->log = [];
}
return $result;
}
public function log($level, $message, array $context = [])
{
$this->record($message, $level, $context);
}
public function emergency($message, array $context = [])
{
$this->log(__FUNCTION__, $message, $context);
}
public function alert($message, array $context = [])
{
$this->log(__FUNCTION__, $message, $context);
}
public function critical($message, array $context = [])
{
$this->log(__FUNCTION__, $message, $context);
}
public function error($message, array $context = [])
{
$this->log(__FUNCTION__, $message, $context);
}
public function warning($message, array $context = [])
{
$this->log(__FUNCTION__, $message, $context);
}
public function notice($message, array $context = [])
{
$this->log(__FUNCTION__, $message, $context);
}
public function info($message, array $context = [])
{
$this->log(__FUNCTION__, $message, $context);
}
public function debug($message, array $context = [])
{
$this->log(__FUNCTION__, $message, $context);
}
public function sql($message, array $context = [])
{
$this->log(__FUNCTION__, $message, $context);
}
}