THINKPHP project development logging instance Analysis _ php instance

Source: Internet
Author: User
This article mainly introduces the usage of log records in THINKPHP project development, and comprehensively analyzes the creation of log tables and related database operation methods in the form of instances, which has some practical value, if you need it, refer to the example in this article to describe the log record usage in THINKPHP project development. Share it with you for your reference. The specific method is as follows:

1. create a log table

The code is as follows:

Create table 'logs '(
'Id' int (11) not null auto_increment,
'Guid 'varchar (100) character set utf8 not null,
'Addtime' timestamp not null default CURRENT_TIMESTAMP,
'Account' varchar (100) character set utf8 not null,
'Nickname' varchar (100) character set utf8 not null,
'App' varchar (100) character set utf8 not null,
'Action' varchar (100) character set utf8 not null,
'Method' varchar (100) character set utf8 not null,
'Query _ string' text character set utf8 not null,
'Is _ desc' varchar (100) character set utf8 not null,
'Desc' varchar (100) character set utf8 not null,
'Ipaddr 'varchar (100) character set utf8 not null,
'Unique _ flag' varchar (100) character set utf8 not null,
Primary key ('id ')
) ENGINE = InnoDB default charset = utf8;


2. define methods in Common/common. php:

The code is as follows:

Function getGlobalSkypeLogDbConfig (){
$ Global_skype_db_config = array (
'Dbms '=> C ('Db _ type '),
'Username' => C ('Db _ user '),
'Password' => C ('Db _ pwd '),
'Hostname' => C ('Db _ host '),
'Database' => C ('Db _ name '),
);
Return $ global_skype_db_config;
}

Function addOperationLog ($ desc = NULL, $ unique_flag = 'system', $ app = APP_NAME, $ action = MODULE_NAME, $ method = ACTION_NAME ){
$ Global_skype_db_config = getGlobalSkypeLogDbConfig ();
Static $ global_skype_log_db_conn;
If (! $ Global_skype_log_db_conn ){
$ Global_skype_log_db_conn = mysql_connect ($ global_skype_db_config ['hostname'], $ global_skype_db_config ['username'], $ global_skype_db_config ['password']);
}
If (! $ Global_skype_log_db_conn ){
Die ('MySQL _ Error: '. _ FILE _.' | '. _ LINE _.' | '. mysql_error ());
}
Mysql_query ('use'. $ global_skype_db_config ['database'], $ global_skype_log_db_conn );
Mysql_query ('set names utf8', $ global_skype_log_db_conn );

$ Account = getAdminAccount ();
$ Nickname = getAdminNickname ();
$ User_id = getAdminUserId ();
$ Ipaddr = get_client_ip ();
$ Query_string = print_r (array_merge ($ _ GET, $ _ POST), true );

$ Desc = $ desc;
$ Is_desc = 0;
$ Unique_flag = $ unique_flag;
If ($ desc ){
$ Is_desc = 1;
}

$ Insert_time = date ('Y-m-d H: I: s ');

$ Query = "insert '". C ('log _ DB_TABLE '). "'('Guid', 'account', 'nickname', 'addtime', 'app', 'Action ',
'Method', 'query _ string', 'is _ desc', 'desc', 'ipadd', 'unique _ flag') VALUES ('$ user_id ', '$ account',' $ nickname', '$ insert_time', '$ app ',
'$ Action',' $ method', '$ query_string', '$ is_desc', '$ desc',' $ ipaddr ',' $ unique_flag ');";

Return mysql_query ($ query, $ global_skype_log_db_conn );
}

Function getAdminAccount (){
Return $ _ SESSION ['loginaccount'];
}

Function getAdminNickname (){
Return $ _ SESSION ['loginusername'];
}

Function getAdminUserId (){
Return $ _ SESSION [C ('User _ AUTH_KEY ')];
}


Call addOperationLog (); in other operation classes.

I hope this article will help you with ThinkPHP framework programming.

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.