Created
October 3, 2015 11:20
-
-
Save megatk/b7b7d9add34592d104a6 to your computer and use it in GitHub Desktop.
PHP monologをカスタマイズ
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// ★ composerでmonologをインストールしている前提 | |
require_once('vendor/autoload.php'); | |
use Monolog\Logger; | |
use Monolog\Handler\StreamHandler; | |
use Monolog\Processor\WebProcessor; | |
use Monolog\Formatter\LineFormatter; | |
class myLogger | |
{ | |
// 外からアクセスしやすいように配慮 | |
const DEBUG = Logger::DEBUG; | |
const INFO = Logger::INFO; | |
const NOTICE = Logger::NOTICE; | |
const WARNING = Logger::WARNING; | |
const ERROR = Logger::ERROR; | |
const CRITICAL = Logger::CRITICAL; | |
const EMERGENCY = Logger::EMERGENCY; | |
private $log; // ファイル出力用のLoggerインスタンス | |
private $startmsec; // 実行開始のUnixエポック | |
private $endmsec; // 実行終了(デバック関数を呼んだタイミング)のUnixエポック | |
private $logformat = "[%datetime%] %msec%msec %level_name% %file%:%line% %message% %extra%\n"; | |
public function __construct($dir, $filename,$level=self::DEBUG, $webinfo=false) | |
{ | |
$this->startmsec = microtime(true); | |
$this->log = new Logger('myLogger'); // chanelは固定とする | |
// ファイルハンドラを設定(フォーマットを指定) | |
// ★第3引数にtrueを指定して有効にする | |
// monologは、デフォルトで改行を許さない仕様になっている | |
// http://hack.aipo.com/archives/12506/ | |
// ★webプロセッサーを使わない場合は付加情報は不要なため、第4引数をtrueにする | |
// http://hack.aipo.com/archives/12509/ | |
$formatter = new LineFormatter($this->logformat,null,true,!$webinfo); | |
$stream = new StreamHandler($this->getLogfilePath($dir, $filename), $level); | |
$stream->setFormatter($formatter); | |
$this->log->pushHandler($stream); | |
// プロセッサーの設定 | |
$this->log->pushProcessor(array($this, 'processorCallback')); | |
if($webinfo){ | |
$this->log->pushProcessor(new WebProcessor()); | |
} | |
} | |
private function getLogfilePath($dir, $filename) | |
{ | |
// ログファイル名の特定 | |
if('/' != substr($dir,-1)){ | |
$dir .= '/'; | |
} | |
return $dir.'/'.$filename.date('Ymd').'.log'; | |
} | |
public function processorCallback($record) | |
{ | |
$record['file'] = $record['context']['file']; | |
$record['line'] = $record['context']['line']; | |
// ★実行時間はミリ秒単位で取得する | |
// microtime(true) = [Unixエポックからの経過秒数].[経過マイクロ秒] | |
// → 秒単位の浮動小数点型で値が返る | |
// 秒 * 1000 = ミリ秒 | |
$record['msec'] = round(($this->endmsec - $this->startmsec)*1000); | |
return $record; | |
} | |
public function debug($message, $level=self::DEBUG, $depth=0) | |
{ | |
$this->endmsec = microtime(true); | |
// 呼び出し元ファイルと行数の取得 | |
$backtrace = debug_backtrace(); | |
// 指定の深さが存在しない場合は呼び出し元 | |
$key = isset($backtrace[$depth]) ? $depth : 0; | |
$file = $backtrace[$key]['file']; | |
$line = $backtrace[$key]['line']; | |
// Web経由の場合は、インストールディレクトリまでのパスは省略する | |
if(isset($_SERVER['DOCUMENT_ROOT'])){ | |
$base_path = dirname($_SERVER['DOCUMENT_ROOT']) . '/'; | |
$file = str_replace($base_path, '', $file); | |
} | |
$context = array( | |
'file' => $file, | |
'line' => $line | |
); | |
// データ型を考慮して出力 | |
$lb = (is_array($message) || is_object($message)) ? "\n" : ''; | |
ob_start(); | |
var_dump($message); | |
$expression = $lb . trim(ob_get_contents()); | |
ob_end_clean(); | |
// デフォルトはDEBUG | |
switch ($level) { | |
case self::DEBUG: | |
$this->log->addDebug($expression, $context); | |
break; | |
case self::INFO: | |
$this->log->addInfo($expression, $context); | |
break; | |
case self::NOTICE: | |
$this->log->addNotice($expression, $context); | |
break; | |
case self::WARNING: | |
$this->log->addWarning($expression, $context); | |
break; | |
case self::ERROR: | |
$this->log->addError($expression, $context); | |
break; | |
case self::CRITICAL: | |
$this->log->addCritical($expression, $context); | |
break; | |
case self::EMERGENCY: | |
$this->log->addEmergency($expression, $context); | |
break; | |
default: | |
$this->log->addDebug($expression, $context); | |
break; | |
} | |
} | |
} | |
$path = 'log'; // ログを出力するパス | |
$filename = 'test'; // ログファイル名(test[Ymd].log の形式になる) | |
$level = myLogger::DEBUG; // 出力するログレベル。指定したレベル以上のものをログ出力する | |
$webinfo = true; // リクエストURIなどのWebリクエストの付加情報を表示するか | |
$log = new myLogger($path, $filename, $level, $webinfo); | |
$log->debug('error!!', myLogger::DEBUG); // ログ出力 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment