PHP code example of daoandco / cakephp-logging
1. Go to this page and download the library: Download daoandco/cakephp-logging library . Choose the download type require .
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
daoandco / cakephp-logging example snippets
// In config/bootstrap.php
Plugin::load('Logging', ['bootstrap' => true, 'routes' => false]);
// In config/app.php
'Log' => [
'debug' => [
'className' => 'Logging.Database',
'levels' => ['notice', 'info', 'debug'],
],
'error' => [
'className' => 'Logging.Database',
'levels' => ['warning', 'error', 'critical', 'alert', 'emergency'],
],
],
use Cake\Log\Log;
Log::write('debug', 'Something did not work');
$this->loadComponent('Logging.Log');
$this->Log->write('debug', 'myScope', 'Message');
// In config/app.php
'Log' => [
'debug' => [
'className' => 'Logging.Database',
'levels' => ['notice', 'info', 'debug'],
],
'error' => [
'className' => 'Logging.Database',
'levels' => ['warning', 'error', 'critical', 'alert', 'emergency'],
],
],
'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',
'
// 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');
// 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');