Skip to content

Instantly share code, notes, and snippets.

@calpo
Last active October 1, 2015 22:08
Show Gist options
  • Save calpo/2069328 to your computer and use it in GitHub Desktop.
Save calpo/2069328 to your computer and use it in GitHub Desktop.
logger interface案
<?php
/*
それぞれのロガーに対してフォーマッターがあった方が良さそう
フォーマッター = ロガーの特性に合わせて渡された構造体(配列)を成型する
= ロガーの特性に合わせてタグの処理を行なう(ファイル名にするとか)
ロガー+フォーマッター →注入→ ログマネージャ
---------------- ↓ --------------------
ファクトリー
ログマネージャインスタンス
*/
interface LoggerInterface {
public static function getInstance();
public function write($info, $tag);
public function report($info, $tag);
}
class StdLogger implements LoggerInterface
{
public static function getInstance() {
return new StdLogger();
}
public function write($info, $tag) {
echo print_r($info,true) ." $tag\n";
}
public function report($info, $tag) {
echo print_r($info,true) ." $tag alerting!!!!\n";
}
}
class LogManager
{
const ENV = 'devl';
private $tag, $logger;
public function __construct($tag, $logger) {
$this->tag = $tag;
$this->logger = $logger;
}
public function notice($info, $tag=null) {
if (self::ENV == 'devl') $this->logger->write($info, $tag?:$this->tag);
}
public function warn($info, $tag=null) {
$this->logger->write($info, $tag?:$this->tag);
}
public function alert($info, $tag=null) {
$this->logger->write($info, $tag?:$this->tag);
$this->logger->report($info, $tag?:$this->tag);
}
}
function LMFactory($tag) {
$logger_class = 'StdLogger';
return new LogManager($tag, $logger_class::getInstance());
}
$log = LMFactory('deftag');
$log->alert('hogehoge');
$log->warn('fuga', 'newtag');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment