Skip to content

Instantly share code, notes, and snippets.

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 anfangd/870909945872140358ea506f26eabe80 to your computer and use it in GitHub Desktop.
Save anfangd/870909945872140358ea506f26eabe80 to your computer and use it in GitHub Desktop.
CakePHP 2.x 系におけるログ拡張クラスの例
<?php
/**
* File Storage stream for Logging
*
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
*
* Licensed under The MIT License
* For full copyright and license information, please see the LICENSE.txt
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
* @link http://www.cakefoundation.org/projects/info/cakephp CakePHP(tm) Project
* @package Cake.Log.Engine
* @since CakePHP(tm) v 1.3
* @license http://www.opensource.org/licenses/mit-license.php MIT License
*/
App::uses('FileLog', 'Log/Engine');
/**
* File Storage stream for Logging. Writes logs to different files
* based on the type of log it is.
*
* @package Cake.Log.Engine
*/
class CustomFileLog extends FileLog {
/**
* Default configuration values
*
* @var array
* @see FileLog::__construct()
*/
protected $_defaults = array(
'path' => LOGS,
'file' => null,
'types' => null,
'scopes' => array(),
'rotate' => 10,
'size' => 10485760, // 10MB
'mask' => null,
);
/**
* Path to save log files on.
*
* @var string
*/
protected $_path = null;
/**
* Log file name
*
* @var string
*/
protected $_file = null;
/**
* Max file size, used for log file rotation.
*
* @var int
*/
protected $_size = null;
/**
* Constructs a new File Logger.
*
* Config
*
* - `types` 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 reabable 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.
*
* @param array $config Options for the FileLog, see above.
*/
public function __construct($config = array()) {
$config = Hash::merge($this->_defaults, $config);
parent::__construct($config);
}
/**
* Implements writing to log files.
*
* @param string $type The type of log you are making.
* @param string $message The message you want to log.
* @return bool success of write.
*/
public function write($type, $message) {
$ip = "-";
$sessionid = "-";
if (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
$ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
$ip_array = explode(“,”, $ip);
$ip = $ip_array[0];
} else {
$ip = $_SERVER['REMOTE_ADDR'];
}
if (session_id() != "") {
$sessionid = session_id();
}
$dbg = debug_backtrace();
$output = "host:".$ip."\tpid:".getmypid()."\tsessionid:".$sessionid."\ttime:[".date("d/M/Y:H:i:s O")."]\ttype:".ucfirst($type)."\tclass:".$dbg[2]['class']."\tfunc:".$dbg[2]['function']."\tline:".$dbg[1]['line']."\tmessage:" . $message . "\n";
$output = "$ip [".getmypid()."] [".$sessionid."] [".date("d/M/Y:H:i:s O")."] ".ucfirst($type)." ".$dbg[2]['class']."#".$dbg[2]['function']." [".$dbg[1]['line']."] " . $message . "\n";
$filename = $this->_getFilename($type);
if (!empty($this->_size)) {
$this->_rotateFile($filename);
}
$pathname = $this->_path . $filename;
if (empty($this->_config['mask'])) {
return file_put_contents($pathname, $output, FILE_APPEND);
}
$exists = file_exists($pathname);
$result = file_put_contents($pathname, $output, FILE_APPEND);
static $selfError = false;
if (!$selfError && !$exists && !chmod($pathname, (int)$this->_config['mask'])) {
$selfError = true;
trigger_error(__d(
'cake_dev', 'Could not apply permission mask "%s" on log file "%s"',
array($this->_config['mask'], $pathname)), E_USER_WARNING);
$selfError = false;
}
return $result;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment