PHP log usage experience (2)

Source: Internet
Author: User
<? PHP  /*  ** Create by: Xiaojiang * the PHP log storage function is only for communication and learning.  */  Class  Config {  Public   Static   Function  Getconfig (){  Return   Array  ( '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 (){}  Public   Static   Function Getinstance ( $ Name  ){  Return  Log : Backendmanager ( $ Name  );}  Public   Static   Function Register ( $ App , $ Params  ){  $ Urldate = Parse_url ( $ Params  );  If (!Isset ( $ Urldate ['Scheme' ]) {  Throw   Exception ('Invalid log connection' );}@  Include ('Logger/backend '. $ Urldate ['Scheme '].'. Class. php' );  $ Classname = $ Urldate ['Scheme ']. 'backend' ;  If (!Class_exists ( $ Classname  )){  Throw   New   Exception ('Invalidclassname '. $ Classname  );}  $ Backobj = New   $ Classname ( $ Params  );  Log : Backendmanager ($ App , $ Backobj  );}  Private   Static   Function Backendmanager ( $ Name , $ OBJ = Null  ){  Static   $ Backends_arr  ;  If (! Isset ($ Backends  )){  $ Backends = Null  ;}  If ( Isset ( $ OBJ  )){  $ Backends_arr [ $ Name ] = $ OBJ  ;}  Else  { If (! Isset ( $ Backends_arr [ $ Name  ]) {  Throw   New   Exception ("The specified { $ Name } Was not registered" );}  Return   $ Backends_arr [ $ Name  ] ;}} 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  ;}}  Try  {  Log : Register ('error', 'HTTP: // localhost? User_name = root & dbname = Test & table = Test & Password =' );}  Catch ( Exception   $ E ){  Echo   $ E -> Getmessage ();}  Try  {  $ Logins = Log : Getinstance ('error' );}  Catch ( Exception   $ E  ){  Echo   $ E ->Getmessage ();}  $ Logins -> Logmessage ('haha ', Log : Info, 'test' );  /*  ** File logger/httpbackend. Class. php  */  Class  Httpbackend {  Public   Function _ Construct ( $ Params  ){  $ Date =Parse_url ( $ Params  );  Parse_str ( $ Date ['Query'], $ Url_params  );  $ This -> DB = @ Mysql_connect ( $ Date ['Host'], $ Url_params ['User _ name'], $ Url_params ['Password' ]); If (! Is_resource ( $ This -> DB )){  Throw   New   Exception ('Invalid dB resource' );}  Mysql_select_db ( $ Url_params ['Table'], $ This -> DB); mysql_set_charset ( 'Utf8 ', $ This ->DB );  $ This -> Loglevel = config: getconfig ('Log _ level' );}  Public   Function Logmessage ( $ Msg , $ Log_level = Log : Info, $ Module = Null  ){  If ( $ Log_level >$ This -> Loglevel) Return  ;  $ SQL =" Insert into test ('msg ', module, add_time) values ('{  $ Msg }','{ $ Module } ', Unix_timestamp ())" ;  Mysql_query ( $ SQL , $ This -> DB );}} ?>

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.