init
This commit is contained in:
42
core/log/Builder.php
Normal file
42
core/log/Builder.php
Normal file
@@ -0,0 +1,42 @@
|
||||
<?php
|
||||
/**
|
||||
* @copyright (C)2016-2099 Hnaoyun Inc.
|
||||
* @author XingMeng
|
||||
* @email hnxsh@foxmail.com
|
||||
* @date 2017年10月24日
|
||||
* 日志操作接口类
|
||||
*/
|
||||
namespace core\log;
|
||||
|
||||
interface Builder
|
||||
{
|
||||
|
||||
// 用于获取单一实例
|
||||
public static function getInstance();
|
||||
|
||||
/**
|
||||
* 日志写入
|
||||
*
|
||||
* @param string $content
|
||||
* 日志内容
|
||||
* @param string $level
|
||||
* 内容级别
|
||||
*/
|
||||
public function write($content, $level = "info", $username = null);
|
||||
|
||||
/**
|
||||
* 错误日志快速写入,error级别
|
||||
*
|
||||
* @param string $content
|
||||
* 日志内容
|
||||
*/
|
||||
public function error($content);
|
||||
|
||||
/**
|
||||
* 基础日志快速写入, info级别
|
||||
*
|
||||
* @param string $content
|
||||
* 日志内容
|
||||
*/
|
||||
public function info($content);
|
||||
}
|
||||
60
core/log/LogDb.php
Normal file
60
core/log/LogDb.php
Normal file
@@ -0,0 +1,60 @@
|
||||
<?php
|
||||
/**
|
||||
* @copyright (C)2016-2099 Hnaoyun Inc.
|
||||
* @author XingMeng
|
||||
* @email hnxsh@foxmail.com
|
||||
* @date 2017年11月16日
|
||||
* 日志数据库驱动
|
||||
*/
|
||||
namespace core\log;
|
||||
|
||||
use core\basic\Model;
|
||||
|
||||
class LogDb implements Builder
|
||||
{
|
||||
|
||||
protected static $logDb;
|
||||
|
||||
protected static $model;
|
||||
|
||||
private function __construct()
|
||||
{}
|
||||
|
||||
// 用于获取单一实例
|
||||
public static function getInstance()
|
||||
{
|
||||
if (! self::$logDb) {
|
||||
self::$logDb = new self();
|
||||
self::$model = new Model();
|
||||
}
|
||||
return self::$logDb;
|
||||
}
|
||||
|
||||
// 写入日志
|
||||
public function write($content, $level = "info", $username = null)
|
||||
{
|
||||
$username = $username ?: session('username');
|
||||
$data = array(
|
||||
'level' => $level,
|
||||
'event' => escape_string($content),
|
||||
'user_ip' => ip2long(get_user_ip()),
|
||||
'user_os' => get_user_os(),
|
||||
'user_bs' => get_user_bs(),
|
||||
'create_user' => $username,
|
||||
'create_time' => get_datetime()
|
||||
);
|
||||
return self::$model->table('ay_syslog')->insert($data);
|
||||
}
|
||||
|
||||
// 写入错误日志
|
||||
public function error($content)
|
||||
{
|
||||
return $this->write($content, 'error');
|
||||
}
|
||||
|
||||
// 写入信息日志
|
||||
public function info($content)
|
||||
{
|
||||
return $this->write($content, 'info');
|
||||
}
|
||||
}
|
||||
50
core/log/LogText.php
Normal file
50
core/log/LogText.php
Normal file
@@ -0,0 +1,50 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @copyright (C)2016-2099 Hnaoyun Inc.
|
||||
* @author XingMeng
|
||||
* @email hnxsh@foxmail.com
|
||||
* @date 2017年10月24日
|
||||
* 日志记录文本驱动
|
||||
*/
|
||||
namespace core\log;
|
||||
|
||||
class LogText implements Builder
|
||||
{
|
||||
|
||||
protected static $logText;
|
||||
|
||||
private function __construct()
|
||||
{}
|
||||
|
||||
// 用于获取单一实例
|
||||
public static function getInstance()
|
||||
{
|
||||
if (! self::$logText) {
|
||||
self::$logText = new self();
|
||||
}
|
||||
return self::$logText;
|
||||
}
|
||||
|
||||
// 写入文本日志
|
||||
public function write($content, $level = "info")
|
||||
{
|
||||
$logfile = ROOT_PATH . '/log/' . date('Ymd') . '.log';
|
||||
check_file($logfile, true);
|
||||
$username = session('username') ?: 'system';
|
||||
$string = $level . ' ' . $content . ' ' . get_user_ip() . ' ' . get_user_os() . ' ' . get_user_bs() . ' ' . $username . ' ' . get_datetime() . PHP_EOL;
|
||||
return file_put_contents($logfile, $string, FILE_APPEND);
|
||||
}
|
||||
|
||||
// 写入文本错误日志
|
||||
public function error($content)
|
||||
{
|
||||
return $this->write($content, 'error');
|
||||
}
|
||||
|
||||
// 写入文本信息日志
|
||||
public function info($content)
|
||||
{
|
||||
return $this->write($content, 'info');
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user