基本操作
日志文件存储地址
log记录目录 = basePath / logger / {fileName}.log
log文件名,以 `年月日` 分文件,如今天是2021年01月18日期,那么 `{fileName}` = `20210118`;
常用操作函数
SeasLog::setBasePath("/var/www/data/log");
SeasLog::setLogger("testLogger");
SeasLog::info("This is a Info");
SeasLog::debug("This is a debug");
SeasLog::notice("This is a notice");
SeasLog::warning('This waring is {$warning}', ['{$warning}' => 'code fail']);
SeasLog::error('This is a error');
还有其他写日志的没有写,总共有8钟日志级别
SEASLOG_DEBUG "DEBUG" - debug信息、细粒度信息事件
SEASLOG_INFO "INFO" - 重要事件、强调应用程序的运行过程
SEASLOG_NOTICE "NOTICE" - 一般重要性事件、执行过程中较INFO级别更为重要的信息
SEASLOG_WARNING "WARNING" - 出现了非错误性的异常信息、潜在异常信息、需要关注并且需要修复
SEASLOG_ERROR "ERROR" - 运行时出现的错误、不必要立即进行修复、不影响整个逻辑的运行、需要记录并做检测
SEASLOG_CRITICAL "CRITICAL" - 紧急情况、需要立刻进行修复、程序组件不可用
SEASLOG_ALERT "ALERT" - 必须立即采取行动的紧急事件、需要立即通知相关人员紧急修复
SEASLOG_EMERGENCY "EMERGENCY" - 系统不可用
可以使用log()方法快速编写日志
SeasLog::log($level,$message,$params,$logger);
运行结果
其中的参数为 时间 | 日志等级 | php进程id | 生成的uniqid | 时间戳 | 日志信息|
集成到codeIgniter框架中
这里我将记录日志信息封装成了一个类进行调用,和之前封装的功能类一样放在libraries目录下,但是因为记录日志运用广泛,所以直接在入口文件进行实例化,这样框架全局就都可以使用Logs类记录日志了。
<?php
class Logs
{
public static function init($basePath = "", $logger = "")
{
if (class_exists("SeasLog")) {
if ($basePath) {
SeasLog::setBasePath($basePath);
}
if ($logger) {
SeasLog::setLogger($logger);
}
}
}
public static function log($level, $message = "", $data = [], $module = "")
{
if (class_exists("SeasLog")){
$msg['url'] = isset($_SERVER['REQUEST_URI'])?$_SERVER['REQUEST_URI']:'';
$msg['msg'] = $message;
SeasLog::log($level, json_encode($message, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES), $data, $module);
if (PHP_SAPI == "cli" OR defined('STDIN')){
SeasLog::closeLoggerStream(SEASLOG_CLOSE_LOGGER_STREAM_MOD_ALL);
}
}
}
public static function debug($message = "", $data = [], $module = "")
{
if (class_exists("SeasLog")) {
self::log(SEASLOG_DEBUG, $message, $data, $module);
}
}
public static function info($message = "", $data = [], $module = "")
{
if (class_exists("SeasLog")) {
self::log(SEASLOG_INFO, $message, $data, $module);
}
}
public static function notice($message = "", $data = [], $module = "")
{
if (class_exists("SeasLog")) {
self::log(SEASLOG_NOTICE, $message, $data, $module);
}
}
public static function warning($message = "", $data = [], $module = "")
{
if (class_exists("SeasLog")) {
self::log(SEASLOG_WARNING, $message, $data, $module);
}
}
public static function error($message = "", $data = [], $module = "")
{
if (class_exists("SeasLog")) {
self::log(SEASLOG_ERROE, $message, $data, $module);
}
}
public static function critical($message = "", $data = [], $module = "")
{
if (class_exists("SeasLog")) {
self::log(SEASLOG_CRITICAL, $message, $data, $module);
}
}
public static function alert($message = "", $data = [], $module = "")
{
if (class_exists("SeasLog")) {
self::log(SEASLOG_ALERT, $message, $data, $module);
}
}
public static function emergency($message = "", $data = [], $module = "")
{
if (class_exists("SeasLog")) {
self::log(SEASLOG_EMERGENCY, $message, $data, $module);
}
}
public static function countLog($level, $module = "default", $date = "")
{
if (class_exists("SeasLog")) {
if (empty($module)) {
$module = "default";
}
SeasLog::setLogger($module);
return SeasLog::analyzerCount($level, $date);
}
}
public static function detailLog($level, $module = "default", $date = "")
{
if (class_exists("SeasLog")) {
if (empty($module)) {
$module = "default";
}
SeasLog::setLogger($module);
return SeasLog::analyzerDetail($level, $date);
}
}
}
index.php入口文件加入如下代码,加载框架时就引入Logs功能类
require_once('application' . DIRECTORY_SEPARATOR . 'libraries' . DIRECTORY_SEPARATOR . 'Logs.php');
Logs::init('/log','default');
在登录操作中加入Logs记录
public function login()
{
$post = $this->input->post();
$data = $this->users_model->findByUsername($post['username']);
if ($data) {
if (encrypt($post['password']) == $data['password']) {
$sqlData = [
'ip' => $_SERVER['SERVER_ADDR'],
'init_time' => time(),
'user_name' => $post['username'],
'uid' => $data['id']
];
$data['init_time'] = time();
$this->logger_model->userLoginLog($sqlData);
Logs::info("用户{$post['username']}登陆成功",[],'userLogin');
$res = $this->Activity_service->addDice($data['id'],'Login');
$data['token'] = $this->Jwt_service->getToken($data['id']);
if ($res){
$data['magicBox'] = $res['magicBox'];
}
success($data, '登陆成功!');
} else {
fail(501, '密码错误!');
}
} else {
fail(500, '没有该用户!');
}
}
运行结果
后续封装的统计日志函数
public function sendmail()
{
$data = $this->input->post();
$this->form_validation->set_data($data);
$this->form_validation->set_rules('email', 'email', 'required|valid_email');
if ($this->form_validation->run() == FALSE) {
fail('400', '邮箱地址错误!');
}
$email = $data['email'];
$code = rand_code(6);
$conn = getRedis();
$conn->selectDb(0);
$key = 'emailCode' . $email;
$status = $conn->setNx($key, $code);
$conn->expire($key, 60);
if (!$status) {
fail(500, '操作太快啦,验证码还没过期!');
}
$Ema = getEmail();
$title = "用户注册验证码";
$content = "<h1>邮箱验证码为{$code}</h1>";
$res = $Ema->sendmail($email, $title, $content);
$data = [
'title' => $title,
'content' => $content,
'email' => $email,
];
if ($res) {
Logs::debug($data);
success($code, 'success');
}
Logs::error($data);
fail($code, "fail");
}
运行结果
日志上面几次不一致是因为前面编写的代码有点问题,只有最后三条是正确的信息。
这里就实现了基本的SeasLog记录日志的功能,SeasLog还有许多其他强大的功能,后续有用到在继续封装使用。
|