The following is a log
Define ("Log_output_level", 1); 1-For Output all debug information 1-debug 2-info 4-warn 8-error
Interface Iloghandler
{
Public function write ($msg);
}
Class Clogfilehandler implements Iloghandler
{
Private $handle = null;
Public function __construct ($file = ")
{
$this->handle = fopen ($file, ' a ');
}
Public function write ($msg)
{
Fwrite ($this->handle, $msg, 4096);
}
Public Function __destruct ()
{
Fclose ($this->handle);
}
}
Class Log
{
Private $handler = null;
Private $level = 15;
private static $instance = null;
Private Function __construct () {}
Private Function __clone () {}
public static function Init ($handler = null, $level = 15)
{
if (!self:: $instance instanceof Self)
{
Self:: $instance = new self ();
Self:: $instance->__sethandle ($handler);
Self:: $instance->__setlevel ($level);
}
Return self:: $instance;
}
Private Function __sethandle ($handler) {
$this->handler = $handler;
}
Private Function __setlevel ($level)
{
$this->level = $level;
}
public static function DEBUG ($msg)
{
Self:: $instance->write (1,self::getfilepath (Debug_backtrace ()). $msg);
}
public static function WARN ($msg)
{
Self:: $instance->write (4, "". Self::getfilepath (Debug_backtrace ()). $msg);
}
public static function ERROR ($msg)
{
Self:: $instance->write (8,self::getfilepath (Debug_backtrace ()). $msg);
}
public static function INFO ($msg)
{
Self:: $instance->write (2, "". Self::getfilepath (Debug_backtrace ()). $msg);
}
Private Function Getlevelstr ($level)
{
Switch ($level)
{
Case 1:
Return ' DEBUG ';
Break
Case 2:
Return ' INFO ';
Break
Case 4:
Return ' WARN ';
Break
Case 8:
Return ' ERROR ';
Break
Default
}
}
public static function GetFilePath ($debugInfo) {
$stack = "[";
foreach ($debugInfo as $key = = $val) {
if (array_key_exists ("file", $val)) {
$stack. = "File:". Strstr ($val ["File"], ' \cyb ');
}
if (array_key_exists ("line", $val)) {
$stack. = ", line:". $val ["Line"];
}
if (array_key_exists ("function", $val)) {
$stack. = ", function:". $val ["function"];
}
}
$stack. = "]";
return $stack;
}
protected function Write ($level, $msg)
{
if (($level & $this->level) = = $level)
{
$msg = ' ['. Date (' y-m-d h:i:s '). '] ['. $this->getlevelstr ($level). '] '. $msg. ' \ r \ n ";
if ($level >=log_output_level) {
$this->handler->write ($msg);
}
}
}
}
2.php
Include (' log.php ');
$FileName =$_server[' Document_root ']. " /log/". Date (' Ym ', Time ()); Log folder by Time
if (!file_exists ($FileName)) {
mkdir ($FileName, 0777);//If you do not have the folder, create a
}
Log::init (New Clogfilehandler ($FileName. " /". Date (' y-m-d ', Time ()). TXT ')); Instantiate a folder
?>