퍼스트몰 db query 로깅
퍼스트몰에서는 개발 가이드나, 메뉴얼이 없는것 같다.
1. 코드에 간단하게 추가하여 사용
print_r($this->db->queries);
실행하여 실행된 쿼리 출력- 쿼리가 실행 안된 경우에는 쿼리다음에
print_r($this->db->error())
를 사용해본다.
2. 로그파일에 기록하는 방식
- 일반적인 로깅에도 사용
app/config/config.php
에 로깅 관련 설정 수정// 2이상으로 하는 경우 다른 로깅정보가 많이 출력됨으로 error로 조정하여 사용 // 로깅을 사용 안하는 경우 0으로 변경 $config['log_threshold'] = 1;
// 후이즈몰의 쓰기 가능한 로그 폴더는 /data/logs
$config['log_path'] = dirname(APPPATH) . '/data/logs/'.date('Y-m-d');
* 실행된 쿼리 하단에 log_message 함수 호출
```php
log_message('error', json_encode($this->db->queries, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE));
3. codeigniter hook 사용
app/config/hooks 설정
$hook['post_system'][] = array( 'class' => 'hook', 'function' => 'manager_log', 'filename' => 'hook.php', 'filepath' => 'hooks' ); // 특정 사용자 아이피에서만 동작하도록 한다. if($_SERVER['REMOTE_ADDR'] === "111.222.111.222") { $hook['post_system'][] = array( 'class' => 'LogQueryHook', 'function' => 'log_queries', 'filename' => 'LogQueryHook.php', 'filepath' => 'hooks'); }
app/hooks/LogQueryHook.php
class LogQueryHook
{
public function log_queries()
{
$ci = &get_instance();
// time required to complete the execution of query
$times = $ci->db->query_times;
$output = null;
//to get all queries that has been executed
$queries = $ci->db->queries;
if (count($queries) == 0) {
$output .= "no queries\n";
} else {
foreach ($queries as $key => $query) {
$output .= $query . "\n";
}
$took = round(doubleval($times[$key]), 3);
$datetime = date('Y-m-d H:i:s');
$output .= "===[took:{$took}]===[datetime:: {$datetime}]\n\n";
}
$logDirectory = dirname(APPPATH) . '/data/logs/'.date('Y-m-d');
// check if directory present ,if not create
if (!is_dir($logDirectory)) {
mkdir($logDirectory, 0775);
}
// Log the queries to a file
$filepath = $logDirectory . '/query-'.date('H') . '.log';
$handle = fopen($filepath, "a+");
fwrite($handle, $output . "\n\n");
fclose($handle);
}
}