php记录log源码,附:(Log.php)日志记录

[TOC]

* * * * *

## 1 日志文件源代码(thinkphp/library/think/Log.php)

~~~

class Log

{

const LOG = 'log';

const ERROR = 'error';

const INFO = 'info';

const SQL = 'sql';

const NOTICE = 'notice';

const ALERT = 'alert';

protected static $log = [];

protected static $type = ['log', 'error', 'info', 'sql', 'notice', 'alert'];

protected static $driver = null;

protected static $alarm = null;

public static function init($config = [])

{

$type = isset($config['type']) ? $config['type'] : 'File';

$class = (!empty($config['namespace']) ? $config['namespace'] : '\\think\\log\\driver\\') . ucwords($type);

unset($config['type']);

self::$driver = new $class($config);

APP_DEBUG && Log::record('[ LOG ] INIT ' . $type . ': ' . var_export($config, true), 'info');

}

public static function alarm($config = [])

{

$type = isset($config['type']) ? $config['type'] : 'Email';

$class = (!empty($config['namespace']) ? $config['namespace'] : '\\think\\log\\alarm\\') . ucwords($type);

unset($config['type']);

self::$alarm = new $class($config['alarm']);

APP_DEBUG && Log::record('[ CACHE ] ALARM ' . $type . ': ' . var_export($config, true), 'info');

}

public static function getLog()

{

return self::$log;

}

public static function record($msg, $type = 'log')

{

if (!is_string($msg)) {

$msg = var_export($msg, true);

}

self::$log[] = ['type' => $type, 'msg' => $msg];

}

public static function clear()

{

self::$log = [];

}

public static function save()

{

if (is_null(self::$driver)) {

self::init(Config::get('log'));

}

return self::$driver->save(self::$log);

}

public static function write($msg, $type = 'log')

{

if (!is_string($msg)) {

$msg = var_export($msg, true);

}

$log[] = ['type' => $type, 'msg' => $msg];

APP_HOOK && Hook::listen('log_write', $log);

if (is_null(self::$driver)) {

self::init(Config::get('log'));

}

return self::$driver->save($log);

}

public static function send($msg)

{

self::$alarm && self::$alarm->send($msg);

}

public static function __callStatic($method, $args)

{

if (in_array($method, self::$type)) {

array_push($args, $method);

return call_user_func_array('\\think\\Log::record', $args);

}

}

}

~~~

## 2 分析

### 成员变量

日志类型常量

const LOG = 'log';

const ERROR = 'error';

const INFO = 'info';

const SQL = 'sql';

const NOTICE = 'notice';

const ALERT = 'alert';

日志内容,日志类型,日志缓存驱动,日志警告驱动

protected static $log = [];

protected static $type = ['log', 'error', 'info', 'sql', 'notice', 'alert'];

protected static $driver = null;

protected static $alarm = null;

### 成员方法

>[info] init() 日志驱动初始化

`public static function init($config = [])`

> $config: 日志配置参数

* * * * *

>[info] alarm() 警告驱动初始化

`public static function alarm($config = [])`

> $config:日志配置参数

* * * * *

>[info] getLog() 获取日志内容

`public static function getLog()`

* * * * *

>[info] recode() 记录日志信息到$log

`public static function record($msg, $type = 'log')`

> $msg:日志内容

> $type:日志类型

* * * * *

>[info] log() 清空日志内容

`public static function clear()`

* * * * *

>[info] save() 保存日志内容

`public static function save()`

* * * * *

>[info] write() 实时保存日志内容

`public static function write($msg, $type = 'log')`

> $msg:日志内容

> $type:日志类型

可回调log_write标签行为

* * * * *

>[info] send() 发送警告

`public static function send($msg)`

> $msg:调试信息

* * * * *

>[info] __callStatic() 静态调用

`public static function __callStatic($method, $args)`

> $method:静态方法名称

> ['log', 'error', 'info', 'sql', 'notice', 'alert']其中一个

> $args:日志参数

例如:Log::Log(xx)

## 3 总结

Log.php 记录调试信息到内存,

可以使用save() write()保存到文件

还可以使用send()发送预警信息

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值