[php]代码库
/**
* 功能描述:记录各种日志
*/
class Log
{
var $MLogPath = LOG_PATH;
var $MLogTitle;
// 静态函数日志记录
static function LOG($type, $msg, $path = '')
{
$user = trim(@shell_exec("whoami"));
$path = date('Ym') . "/";
if ($user == 'root') {
$path = date('Ym') . "-root/";
}
if (php_sapi_name() == "cli") {
$path .= 'cli';
} else {
$uri = $_SERVER['REQUEST_URI'];
if (strpos($uri, "/admin/") !== false) {
$path .= "admin";
} else {
$path .= "front";
}
}
$log = new Log();
$log->log_by_type($type, $msg, $path);
}
/**
* 功能描述:构造函数
*/
function __construct($selfpath = '')
{
if (!empty($selfpath)) {
Dir::checkDirectory($selfpath, true);
}
$this->MLogPath = $selfpath ? $selfpath : $this->MLogPath;
$this->time = date('Y-m-d H:i:s', time());
$this->ip = $_SERVER["REMOTE_ADDR"];
$this->pid = getmypid();
}
/**
* 功能描述:按时间记录日志
*/
function log_by_date($profix, $msg, $path = '')
{
$this->MLogTitle = $profix . date('Y-m-d', time()) . ".txt";
if ($path) {
$this->MLogPath .= $path;
if (!is_dir($this->MLogPath)) {
mkdir($this->MLogPath);
}
}
//$this->MLogPath .= '/';
$logFile = $this->MLogPath . $this->MLogTitle;
$fp = fopen($logFile, "a+");
$msg = $this->time . "\t" . $this->ip . "\t" . $msg . "\n\n";
fwrite($fp, $msg);
fflush($fp);
fclose($fp);
}
/**
* 功能描述:按类型记录日志
*/
function log_by_type($type, $msg, $path = '')
{
$this->MLogPath = ROOTDIR ."log/";
$this->MLogTitle = $type."_".date('Y-m-d').".txt";
if($path)
{
$arr = explode("/",$path);
foreach($arr as $name){
$this->MLogPath .= $name."/";
if(!file_exists($this->MLogPath)){
mkdir($this->MLogPath);
}
}
}
$pre_utf_header = "";
if(!file_exists($this->MLogPath.$this->MLogTitle))
$pre_utf_header = "\xEF\xBB\xBF";
$fp = fopen($this->MLogPath.$this->MLogTitle,"a+");
$msg=$this->pid."\t".$this->time."\t".$this->ip."\t".$msg."\r\n";
fwrite($fp,$pre_utf_header . $msg);
fflush($fp);
fclose($fp);
}
public function log_by_dateEx($msg)
{
$now = date("Y-m-d H:i:s");
$cur_year = "year" . date("Y", time());
$cur_month = "month" . date("m", time());
$cur_day = "day" . date("d", time());
$msg = $this->time . "\t" . $this->ip . "\t" . $msg . "\n";
if (!file_exists($this->MLogPath . $foldername . "/" . $cur_year)) {
mkdir($this->MLogPath . $foldername . "/" . $cur_year);
}
if (!file_exists($this->MLogPath . $foldername . "/" . $cur_year . "/" . $cur_month)) {
mkdir($this->MLogPath . $foldername . "/" . $cur_year . "/" . $cur_month);
}
$fp = fopen($this->MLogPath . $foldername . "/" . $cur_year . "/" . $cur_month . "/" . $cur_day . ".txt", "a+");
fwrite($fp, $msg);
fclose($fp);
}
}
使用:Log::LOG('erp_api', "send_trade_order");