THINKPHP项目开发中的日志记录实例分析
网络编程 2021-07-05 09:50www.168986.cn编程入门
这篇文章主要介绍了THINKPHP项目开发中的日志记录用法,以实例形式较为全面的分析了日志表的建立与相关数据库操作方法,具有一定的实用价值,需要的朋友可以参考下
本文实例讲述了THINKPHP项目开发中的日志记录用法。分享给大家供大家参考。具体方法如下
1、建立日志表
代码如下:
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,
`aount` 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 ;
`id` int(11) NOT NULL auto_increment,
`guid` varchar(100) character set utf8 NOT NULL,
`addtime` timestamp NOT NULL default CURRENT_TIMESTAMP,
`aount` 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、Common/mon.php里面进行方法的定义:
代码如下:
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);
$aount = getAdminAount();
$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 INTO `" . C('LOG_DB_TABLE') . "` (`guid`,`aount`,`nickname`,`addtime`,`app`,`action`,
`method`,`query_string`,`is_desc`,`desc`,`ipaddr`,`unique_flag`) VALUES ('$user_id','$aount','$nickname','$insert_time','$app',
'$action','$method','$query_string','$is_desc','$desc','$ipaddr','$unique_flag');";
return mysql_query($query, $global_skype_log_db_conn);
}
function getAdminAount() {
return $_SESSION['loginAount'];
}
function getAdminNickname() {
return $_SESSION['loginUserName'];
}
function getAdminUserId() {
return $_SESSION[C('USER_AUTH_KEY')];
}
$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);
$aount = getAdminAount();
$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 INTO `" . C('LOG_DB_TABLE') . "` (`guid`,`aount`,`nickname`,`addtime`,`app`,`action`,
`method`,`query_string`,`is_desc`,`desc`,`ipaddr`,`unique_flag`) VALUES ('$user_id','$aount','$nickname','$insert_time','$app',
'$action','$method','$query_string','$is_desc','$desc','$ipaddr','$unique_flag');";
return mysql_query($query, $global_skype_log_db_conn);
}
function getAdminAount() {
return $_SESSION['loginAount'];
}
function getAdminNickname() {
return $_SESSION['loginUserName'];
}
function getAdminUserId() {
return $_SESSION[C('USER_AUTH_KEY')];
}
在其他操作类里调用addOperationLog();即可
希望本文所述对大家的ThinkPHP框架程序设计有所帮助。
编程语言
- 如何快速学会编程 如何快速学会ug编程
- 免费学编程的app 推荐12个免费学编程的好网站
- 电脑怎么编程:电脑怎么编程网咯游戏菜单图标
- 如何写代码新手教学 如何写代码新手教学手机
- 基础编程入门教程视频 基础编程入门教程视频华
- 编程演示:编程演示浦丰投针过程
- 乐高编程加盟 乐高积木编程加盟
- 跟我学plc编程 plc编程自学入门视频教程
- ug编程成航林总 ug编程实战视频
- 孩子学编程的好处和坏处
- 初学者学编程该从哪里开始 新手学编程从哪里入
- 慢走丝编程 慢走丝编程难学吗
- 国内十强少儿编程机构 中国少儿编程机构十强有
- 成人计算机速成培训班 成人计算机速成培训班办
- 孩子学编程网上课程哪家好 儿童学编程比较好的
- 代码编程教学入门软件 代码编程教程