PHP log usage experience

Source: Internet
Author: User
<? PHP
/* ** PHP log class */ Class Config { Public Static Function Getconfig (){ Return Array ( 'Log _ file' => 'test.txt ', 'Log _ level' => 75 // Info );}} Class Log { Private $ Logfile ; Private $ Loglevel ; Const DEBUG = 100 ; Const Info = 75 ; Const Notice = 50 ; Const Warning = 25 ; Const Error = 10 ; Const Critical = 5 ; Private Function _ Construct (){ $ Cfg = Config :: Getconfig (); $ This -> Loglevel = Isset ( $ Cfg ['Log _ level'])? $ Cfg ['Log _ level']: Log :: Info; If (! Isset ( $ Cfg ['Log _ file']) & Strlen ( $ Cfg ['Log _ file' ]) { Throw New Exception ('Can \'t set file to empty' );} $ This -> Logfile = @ Fopen ( $ Cfg ['Log _ file'], 'a +' ); If (! Is_resource ( $ This -> Logfile )){ Throw New Exception ('Invalid file stream' );}} Public Static Function Getinstance (){ Static $ OBJ ; If (! Isset ( $ OBJ )){ $ OBJ = New Log ();} Return $ OBJ ;} Public Function Logmessage ( $ Msg , $ Loglevel = Log : Info, $ Module = Null ){ If ( $ Loglevel > $ This -> Loglevel ){ Return ;} Date_default_timezone_set ( 'Asian/Shanghai' ); $ Time = Strftime ('% X % x ', Time ()); $ Msg = Str_replace ("\ T ",'', $ Msg ); $ Msg = Str_replace ("\ N ",'', $ Msg ); $ Strloglevel = $ This -> Leveltostring ( $ Loglevel ); If ( Isset ( $ Module )){ $ Module = Str_replace ( Array ("\ N", "\ t "), Array ("",""),$ Module );} $ Logline =" $ Time \ T $ Msg \ T $ Strloglevel \ T $ Module \ R \ n" ; Fwrite ( $ This -> Logfile, $ Logline );} Public Function Leveltostring ($ Loglevel ){ $ RET = '[Unknow]' ; Switch ( $ Loglevel ){ Case Log : Debug: $ RET = 'Log: debug' ; Break ; Case Log : Info: $ RET = 'Log: info' ; Break ; Case Log : Notice: $ RET = 'Log: notice' ; Break ; Case Log : Warning:$ RET = 'Log: warning' ; Break ; Case Log : Error: $ RET = 'Log: error' ; Break ; Case Log : Critical: $ RET = 'Log: Critical'; Break ;} Return $ RET ;}} $ Logins = Log :: Getinstance (); // Print_r ($ logins ); $ Logins -> Logmessage ("test ", Log : Info, 'mytest' ); ?>

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.