一个通用的日志记录器
1 <?php 2 class Log4p{ 3 4 const LOG_PATH = "/data/logs/mobact2rd/drawcard"; 5 6 static function writeLog($str, $filename=‘debug‘, $fileCutByDate = true, $dirCutByDate = true) { 7 if (empty($str)) return; 8 $dir = ‘‘; 9 10 if ( $dirCutByDate) { 11 $dir = self::LOG_PATH . ‘/‘ . date("Ymd"); 12 } else { 13 $dir = self::LOG_PATH; 14 } 15 16 if (!file_exists($dir)) { 17 @mkdir($dir, 0775, true); 18 } 19 20 $file = ‘‘; 21 22 if ($fileCutByDate) { 23 $file = $dir . ‘/‘ . $filename . ‘_‘ . date("Y-m-d") . ‘.log‘; 24 } else { 25 $file = $dir . ‘/‘ . $filename . ‘.log‘; 26 } 27 28 29 $f = fopen($file, ‘a‘); 30 if ($f) { 31 fwrite($f, $str . "\n"); 32 fclose($f); 33 } 34 } 35 36 37 38 public static function getLogRootPath() { 39 return self::LOG_PATH; 40 } 41 42 // remote 日志 43 public static function r($uid, $type, $message) { 44 $log = self::buildLog($uid, $type, $message); 45 self::writeLog($log, ‘remote‘); 46 } 47 48 public static function f($uid, $type, $message) { 49 $log = self::buildLog($uid, $type, $message); 50 self::writeLog($log, ‘fetal‘); 51 } 52 53 public static function e($uid, $type, $message) { 54 $log = self::buildLog($uid, $type, $message); 55 self::writeLog($log, ‘error‘); 56 } 57 58 public static function i($uid, $type, $message) { 59 $log = self::buildLog($uid, $type, $message); 60 self::writeLog($log, ‘info‘); 61 } 62 63 public static function w($uid, $type, $message) { 64 $log = self::buildLog($uid, $type, $message); 65 self::writeLog($log, ‘warn‘); 66 } 67 68 public static function d($log = ‘‘) { 69 self::writeLog($log, ‘debug‘); 70 } 71 72 public static function m($uid, $type, $message) { 73 $log = self::buildLog($uid, $type, $message); 74 self::writeLog($log, ‘monitor‘); 75 } 76 77 public static function winner($uid, $type, $message) { 78 $log = self::buildLog($uid, $type, $message); 79 self::writeLog($log, ‘winner‘, false, false); 80 } 81 82 private static function buildLog($uid, $type, $message) { 83 $_log = array( 84 date("Y-m-d H:i:s"), 85 Utils::getRealIp(), 86 $uid, 87 $type, 88 $message, 89 ); 90 return implode("\t", $_log); 91 } 92 93 } 94 ?>