"PHP" WeChat official code log debug Output class, Object-oriented design mode! Come and see, you will have the benefit!

Source: Internet
Author: User

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

?>

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.