daoandco/cakephp-logging

此包的最新版本(v1.1.1)没有提供许可证信息。

CakePHP 插件,用于在数据库中记录用户操作

安装: 739

依赖项: 0

建议者: 0

安全性: 0

星级: 4

关注者: 3

分支: 0

开放问题: 0

类型:cakephp-plugin

v1.1.1 2016-04-28 11:37 UTC

This package is not auto-updated.

Last update: 2024-09-14 18:26:19 UTC


README

记录您的数据库中的用户操作。此插件由一个组件和一个日志引擎组成。此插件与 CakePHP 的核心日志兼容。

------------------------------------

该项目已不再维护

------------------------------------

需求

  • PHP 版本 5.4.16 或更高版本
  • CakePhp 3.0 或更高版本

安装

您可以使用 composer 将此插件安装到您的 CakePHP 应用程序中。

安装 composer 包的推荐方式是

composer require daoandco/cakephp-logging

以这种方式加载插件

// In config/bootstrap.php
Plugin::load('Logging', ['bootstrap' => true, 'routes' => false]);

创建日志表:在 config/shema/logs.sql 中执行 shema(您可以根据需要更改表名)

CREATE TABLE `logs` (
    `id` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,
    `created` DATETIME NOT NULL,
    `level` VARCHAR(50) NOT NULL,
    `scope` VARCHAR(50) NULL DEFAULT NULL,
    `user_id` INT(10) UNSIGNED NULL DEFAULT NULL,
    `message` TEXT NULL,
    `context` TEXT NULL,
    PRIMARY KEY (`id`),
    INDEX `user_id` (`user_id`),
    INDEX `scope` (`scope`),
    INDEX `level` (`level`)
)
COLLATE='utf8_general_ci'
;

快速入门

如果您只想替换默认配置,您可以在 app 文件中更改 Log 的配置

// In config/app.php
'Log' => [
	'debug' => [
		'className' => 'Logging.Database',
        'levels' => ['notice', 'info', 'debug'],
    ],
    'error' => [
        'className' => 'Logging.Database',
        'levels' => ['warning', 'error', 'critical', 'alert', 'emergency'],
    ],
],

有关写入日志的信息,请参阅 https://book.cakephp.com.cn/3.0/en/core-libraries/logging.html#writing-to-logs

use Cake\Log\Log;

Log::write('debug', 'Something did not work');

或者您可以使用控制器中的 LogComponent。组件默认将 requestsession 存储在上下文字段中。

$this->loadComponent('Logging.Log');

$this->Log->write('debug', 'myScope', 'Message');

配置

选项

  • className : 'Logging.Database'
  • model : 模型名称(默认:'Logging.Logs'
  • table : 表名称(默认:'logs'
  • levels : 日志级别(默认:'[]' = 所有级别)更多信息
  • scopes: 日志作用域(默认:'[]' = 所有作用域)更多信息
  • requiredScope : 如果为真,作用域为空时不存储日志(默认 false
  • userId : 存储用户 ID 的会话路径(默认 Auth.User.id

用例

编辑 config/app.php

使用插件写入所有内容

// In config/app.php
'Log' => [
	'debug' => [
		'className' => 'Logging.Database',
        'levels' => ['notice', 'info', 'debug'],
    ],
    'error' => [
        'className' => 'Logging.Database',
        'levels' => ['warning', 'error', 'critical', 'alert', 'emergency'],
    ],
],

将 cake 日志写入文件,并用插件写入应用程序日志

在此配置下,写入日志时需要作用域

'Log' => [
    'debug' => [
        'className' => 'Cake\Log\Engine\FileLog',
        'path' => LOGS,
        'file' => 'debug',
        'levels' => ['notice', 'info', 'debug'],
        'scopes' => false,
        'url' => env('LOG_DEBUG_URL', null),
    ],
    'error' => [
        'className' => 'Cake\Log\Engine\FileLog',
        'path' => LOGS,
        'file' => 'error',
        'levels' => ['warning', 'error', 'critical', 'alert', 'emergency'],
        'scopes' => false,
        'url' => env('LOG_ERROR_URL', null),
    ],
    'app' => [
        'className'     => 'Logging.Database',
        'requiredScope' => true,
    ],
],

写入日志

// With Cake\Log\Log;
Log::write('debug', 'Something did not work', ['scope'=>['myScope']]);

// Or with component
$this->Log->write('debug', 'myScope', 'Something did not work');

组件

// Load component
$this->loadComponent('Logging.Log');

配置

  • request: 如果为真,将 $this->request 存储在上下文中(默认:'false'
  • session: 如果为真,将 $_SESSION 存储在上下文中(默认:'false'
  • ip: 如果为真,将 $this->request->clientIp() 存储在上下文中(默认:'false'
  • referer: 如果为真,将 $this->request->referer() 存储在上下文中(默认:'false'
  • vars: 存储更多数据(例如:['plugin' => $this->plugin]

方法

  • write($level, $scope, $message, $context, $config) 记录一条消息

  • emergency($scope, $message, $context, $config) 记录一条紧急消息

  • alert($scope, $message, $context, $config) 记录一条警报消息

  • critical($scope, $message, $context, $config) 记录一条关键消息

  • error($scope, $message, $context, $config) 记录一条错误消息

  • warning($scope, $message, $context, $config) 记录一条警告消息

  • notice($scope, $message, $context, $config) 记录一条通知消息

  • debug($scope, $message, $context, $config) 记录一条调试消息

  • info($scope, $message, $context, $config) 记录一条信息消息

参数

  • 级别: (字符串) 记录级别 ('紧急'|'警报'|'关键'|'错误'|'警告'|'通知'|'调试'|'信息') 更多信息
  • 范围: (字符串|数组) 记录范围 更多信息
  • 消息: (字符串) 日志消息
  • 上下文: (数组) 附加数据,用于记录消息
  • 配置: 更改基本配置(例如请求、会话...)

使用

// Basic usage
$this->Log->write('debug', 'myScope', 'Something did not work');

// With convenience methods
$this->Log->emergency('myScope', 'My message');
$this->Log->alert('myScope', 'My message');
$this->Log->critical('myScope', 'My message');
$this->Log->error('myScope', 'My message');
$this->Log->warning('myScope', 'My message');
$this->Log->notice('myScope', 'My message');
$this->Log->debug('myScope', 'My message');
$this->Log->info('myScope', 'My message');

// Add datas
$this->Log->info('myScope', 'My message', ['key1' => 'value1', 'key2' => 'value2']);

// Save request
$this->Log->info('myScope', 'My message', [], ['request' => true]);

// Save session
$this->Log->info('myScope', 'My message', [], ['session' => true]);

// Save ip
$this->Log->info('myScope', 'My message', [], ['ip' => true]);

// Save referer url
$this->Log->info('myScope', 'My message', [], ['referer' => true]);

// Don't save userId
$this->Log->info('myScope', 'My message', ['userId' => null];

// Force userId if different to $_SESSION
$this->Log->info('myScope', 'My message', ['userId' => 2];

// No scope
$this->Log->info(null, 'My message');

// Multi scope = multi lines in bdd
$this->Log->info(['scope1', 'scope2'], 'My message');