把自己在開發過程中的日志記錄寫下來~~
首先,你要建一個日志表,我的這個表裡面的字段 有:`user_id`,`account`,`nickname`,`insert_time`,`app`,`action`, `method`,`query_string`,`is_desc`,`desc`,`ipaddr`,`unique_flag`
// 是否開啟調試模式 (開啟AllInOne模式時該配置無效, 將自動置為false)
‘APP_DEBUG’ => 0,
‘LOG_DB_HOSTNAME’ => $conf['dbhost'], //操作日志所用的數據庫
‘LOG_DB_USERNAME’ => $conf['dbuser'],
‘LOG_DB_PASSWORD’ => $conf['dbpass'],
‘LOG_DB_DATABASE’ => $conf['dbname'],
‘LOG_DB_TABLE’ => ‘tp_logs’,
‘LOG_DB_TYPE’ => ‘mysql’,
還要在Common/common.php裡面進行方法的定義:
/**
* 獲取日志數據庫
* @return type
*/
function getGlobalSkypeLogDbConfig() {
$global_skype_db_config = array(
‘dbms’ => C(‘LOG_DB_TYPE’),
‘username’ => C(‘LOG_DB_USERNAME’),
‘password’ => C(‘LOG_DB_PASSWORD’),
‘hostname’ => C(‘LOG_DB_HOSTNAME’),
‘database’ => C(‘LOG_DB_DATABASE’),
);
return $global_skype_db_config;
}
/**
* 添加操作日志
* @global array $global_skype_db_config
* @staticvar type $global_skype_log_db_conn
* @param type $desc
* @param type $unique_flag
* @param type $app
* @param type $action
* @param type $method
* @return type
*/
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 = intval(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’) . "` (`user_id`,`account`,`nickname`,`insert_time`,`app`,`action`,
`method`,`query_string`,`is_desc`,`desc`,`ipaddr`,`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::get(‘loginAccount’);
}
/**
* 獲取當前管理員的昵稱
* @return type
*/
function getAdminNickname() {
return Session::get(‘loginUserName’);
}
/**
* 獲取當前登錄的用戶數字ID
*/
function getAdminUserId() {
return Session::get(C(‘USER_AUTH_KEY’));
}
當你都做完了以上操作,要實現日志記錄數據庫的時候只要這樣 addOperationLog($info); 就可以了哦~~
*