質問
class::method
ロギング クラスを作成し、ロガー クラスの静的メソッドを呼び出した各行をスタンプしたいと考えています。
PHPで現在のメソッドを呼び出したものを知ることは可能ですか?
解決
助けてくれてありがとう。私のロギング方法の PHP コードは次のとおりです。
/**
* Writes a line into the log.
* @param string $level The logging level.
* @param string $message The message.
*/
protected static function write($level,$message)
{
self::scope_init();
if(self::$scope[$level])
{
$e = new Exception;
$stack = $e->getTrace();
$caller = '';
if(isset($stack[2]))
{
$class = isset($stack[2]['class']) ? $stack[2]['class'] : false;
$method = isset($stack[2]['function']) ? $stack[2]['function'] : '';
if($class !== false)
{
$caller = $class.'::'.$method;
}
else
{
$caller = $method;
}
}
$caller = substr(str_pad($caller,20),0,20);
$msg = date('m.d.y h:i:sa').' ['.$level.'] ['.$caller.'] '.$message;
self::$lines[] = $msg;
echo "$msg\n";
}
}