Created
January 5, 2014 14:43
-
-
Save yuuan/8269004 to your computer and use it in GitHub Desktop.
PHPでログに出力するクラス
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 | |
class Log { | |
const LOG_FILE = 'log/console.log'; | |
public static function warn($message = '', $toError = true) { | |
if ($toError) { | |
return user_error("$message", E_USER_WARNING); | |
} | |
else { | |
// 呼び出し元のファイル名と行番号とか出したい | |
return self::writeln(mb_substr(0, 1024, $message), false); | |
} | |
} | |
public static function dump($object = null, $toError = true) { | |
return self::write(print_r($object, true), $toError); | |
} | |
public static function write($string = '', $toError = true) { | |
if ($toError === true) { | |
return error_log("$string"); | |
} | |
else { | |
return self::puts((($toError) ?: self::LOG_FILE), "$string"); | |
} | |
} | |
public static function writeln($string = '', $toError = true) { | |
return self::write($string . PHP_EOL, $toError); | |
} | |
public static function hr($pattern = '=', $toError = true) { | |
return self::writeln(str_repeat($pattern, 20), $toError); | |
} | |
public static function err($string) { | |
error_log($string); | |
} | |
public static function puts() { | |
if ($fp = fopen(func_get_arg(0), 'a')) { | |
for ($i = 1; $i < func_num_args(); $i++) { | |
fwrite($fp, func_get_arg($i)); | |
} | |
fclose($fp); | |
return true; | |
} | |
return false; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment