定制 daoandco/cakephp-logging 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

daoandco/cakephp-logging

Composer 安装命令:

composer require daoandco/cakephp-logging

包简介

Cakephp plugin for Log user's action in database

README 文档

README

Log user's action in your Database. This plugin is composed of a Component and a Log engine. This plugin is compatible with core logs of CakePHP.

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

This project is no longer maintained

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

Requirements

  • PHP version 5.4.16 or higher
  • CakePhp 3.0 or higher

Installation

You can install this plugin into your CakePHP application using composer.

The recommended way to install composer packages is:

composer require daoandco/cakephp-logging

Loading the Plugin like that

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

Create table log : execute shema in config/shema/logs.sql (you can change the table name)

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'
;

Quick Start

If you want just replace default config you can change Log's config in your app file

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

For writing to logs see http://book.cakephp.org/3.0/en/core-libraries/logging.html#writing-to-logs

use Cake\Log\Log;

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

Or you can use LogComponent in your controllers. The component store by default request and session in context field.

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

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

Configuration

Options

  • className : 'Logging.Database'
  • model : Model name (default: 'Logging.Logs')
  • table : table name (default: 'logs')
  • levels : logging levels (default: '[]' = all levels) More infos
  • scopes: logging scopes (default: '[]' = all scopes) More infos
  • requiredScope : if true no store logs if scope is empty (default false)
  • userId : path where is stored user id in Session (default Auth.User.id)

Use cases

Edit config/app.php

Write everything with the plugin

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

Write cake log in file and write application log with the plugin

With this configuration the scope is required when you write in a log

'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,
    ],
],

Writing to Logs :

// 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');

Component

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

Configuration

  • request: if true store $this->request in Context (default: 'false')
  • session: if true store $_SESSION Context (default: 'false')
  • ip: if true store $this->request->clientIp() Context (default: 'false')
  • referer: if true store $this->request->referer() Context (default: 'false')
  • vars: store more datas (ex : ['plugin' => $this->plugin]

Methods

  • write($level, $scope, $message, $context, $config) Log a message

  • emergency($scope, $message, $context, $config) Log a emergency message

  • alert($scope, $message, $context, $config) Log a alert message

  • critical($scope, $message, $context, $config) Log a critical message

  • error($scope, $message, $context, $config) Log a error message

  • warning($scope, $message, $context, $config) Log a warning message

  • notice($scope, $message, $context, $config) Log a notice message

  • debug($scope, $message, $context, $config) Log a debug message

  • info($scope, $message, $context, $config) Log a info message

Parameters

  • levels : (string) logging levels ('emergency'|'alert'|'critical'|'error'|'warning'|'notice'|'debug'|'info') More infos
  • scope : (string|array) logging scopes More infos
  • message: (string) log message
  • context: (array) Additional data to be used for logging the message
  • config: change base config (ex request, session...)

Use

// 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');

daoandco/cakephp-logging 适用场景与选型建议

daoandco/cakephp-logging 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 773 次下载、GitHub Stars 达 4, 最近一次更新时间为 2016 年 04 月 27 日, 在 PHP 生态内属于活跃度较高的组件。

我们在过去多个企业项目中使用过 daoandco/cakephp-logging 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 daoandco/cakephp-logging 我们能提供哪些服务?
定制开发 / 二次开发

基于 daoandco/cakephp-logging 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 773
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 4
  • 点击次数: 15
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 4
  • Watchers: 2
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: Unknown
  • 更新时间: 2016-04-27