Skip to content

Instantly share code, notes, and snippets.

@nojimage
Created October 3, 2017 05:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nojimage/f66a8cf6d7c7e1676ee3ff3ea5eef62b to your computer and use it in GitHub Desktop.
Save nojimage/f66a8cf6d7c7e1676ee3ff3ea5eef62b to your computer and use it in GitHub Desktop.
CakePHP3 のログで$contextを宜しくロギング
<?php
/*
*
* Copyright 2017 ELASTIC Consultants Inc.
*
*/
namespace Elastic\ContextLogEngine\Log\Engine;
use Cake\Http\ServerRequest;
use Cake\Log\Engine\FileLog as BaseLogEngine;
use Exception;
use JsonSerializable;
/**
* FileLog with context
*/
class FileLog extends BaseLogEngine
{
/**
* Default config for this class
*
* - `levels` string or array, levels the engine is interested in
* - `scopes` string or array, scopes the engine is interested in
* - `file` Log file name
* - `path` The path to save logs on.
* - `size` Used to implement basic log file rotation. If log file size
* reaches specified size the existing file is renamed by appending timestamp
* to filename and new log file is created. Can be integer bytes value or
* human readable string values like '10MB', '100KB' etc.
* - `rotate` Log files are rotated specified times before being removed.
* If value is 0, old versions are removed rather then rotated.
* - `mask` A mask is applied when log files are created. Left empty no chmod
* is made.
* - `jsonOptions` json_encode options.
* - `deepStackTrace` When `exception` is included in the $context, a detailed stack trace is output.
*
* @var array
*/
protected $_defaultConfig = [
'path' => null,
'file' => null,
'types' => null,
'levels' => [],
'scopes' => [],
'rotate' => 10,
'size' => 10485760, // 10MB
'mask' => null,
'jsonOptions' => null,
'deepStackTrace' => false,
];
/**
* json_encode options.
*
* @var int|null
*/
protected $_jsonOptions;
/**
* When `exception` is included in the $context,
* a detailed stack trace is output.
*
* @var bool
*/
protected $_deepStackTrace = false;
/**
* Sets protected properties based on config provided
*
* @param array $config Configuration array
*/
public function __construct(array $config = [])
{
parent::__construct($config);
if (isset($this->_config['jsonOptions'])) {
$this->_jsonOptions = $this->_config['jsonOptions'];
} else {
$this->_jsonOptions = JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE |
JSON_PRETTY_PRINT | JSON_PARTIAL_OUTPUT_ON_ERROR;
}
if (isset($this->_config['deepStackTrace'])) {
$this->_deepStackTrace = $this->_config['deepStackTrace'];
}
}
/**
* {@inheritdoc}
*
* @param mixed $data The data to be converted to string and logged.
* @param array $context Additional logging information for the message.
* @return string
*/
protected function _format($data, array $context = [])
{
$message = parent::_format($data, $context);
$dataWithContext = $this->_injectContext($message, $context);
return parent::_format(json_encode($dataWithContext, $this->_jsonOptions));
}
/**
* merge context data
*
* @param string $message
* @param array $context
* @return array
*/
protected function _injectContext($message, array $context)
{
$data = compact('message');
if (isset($context['request']) && $context['request'] instanceof ServerRequest) {
$data['request'] = [
'url' => $context['request']->getRequestTarget(),
'clientIp' => $context['request']->clientIp(),
'query' => $context['request']->getQuery(),
'data' => $context['request']->getData(),
];
unset($context['request']);
}
if (isset($context['exception']) && $context['exception'] instanceof Exception) {
$data['exception'] = [
'message' => $context['exception']->getMessage(),
'code' => $context['exception']->getCode(),
'trace' => $this->_deepStackTrace ? $context['exception']->getTrace() : $context['exception']->getTraceAsString(),
];
unset($context['exception']);
}
foreach ($context as $key => $value) {
if ($value instanceof JsonSerializable || is_array($value)) {
$data[$key] = $value;
} else {
$data[$key] = parent::_format($value);
}
}
return $data;
}
}
@nojimage
Copy link
Author

nojimage commented Oct 3, 2017

プラグインで公開する予定は未定。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment