Skip to content

Instantly share code, notes, and snippets.

@yuuan
Created January 5, 2014 14:43
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 yuuan/8269004 to your computer and use it in GitHub Desktop.
Save yuuan/8269004 to your computer and use it in GitHub Desktop.
PHPでログに出力するクラス
<?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