로그 생성이 안되어 테스트 및 수정본

This commit is contained in:
2026-03-20 09:33:30 +09:00
parent 80cb9451d2
commit bb07396abf
12 changed files with 205 additions and 23 deletions

View File

@@ -17,6 +17,9 @@ class NaverWorker extends BaseCommand
// DB 객체를 담을 변수 선언
protected $db;
protected $redisHost;
protected $redisPort;
protected $redisDatabase;
public function run(array $params)
{
@@ -29,13 +32,13 @@ class NaverWorker extends BaseCommand
$redis = new \Redis();
try {
// 두 가지 환경 변수 형식 지원 (REDIS_HOST 또는 redis.default.host)
$redisHost = getenv('REDIS_HOST') ?: (getenv('redis.default.host') ?: '127.0.0.1');
$redisPort = getenv('REDIS_PORT') ?: (getenv('redis.default.port') ?: 6379);
$redisDatabase = getenv('REDIS_DATABASE') ?: (getenv('redis.default.database') ?: 9);
$this->redisHost = getenv('REDIS_HOST') ?: (getenv('redis.default.host') ?: '127.0.0.1');
$this->redisPort = getenv('REDIS_PORT') ?: (getenv('redis.default.port') ?: 6379);
$this->redisDatabase = getenv('REDIS_DATABASE') ?: (getenv('redis.default.database') ?: 9);
$redis->connect($redisHost, (int)$redisPort);
$redis->select((int)$redisDatabase);
CLI::write(CLI::color('🟢 Naver Worker running... (Redis: ' . $redisHost . ':' . $redisPort . ' DB:' . $redisDatabase . ')', 'green'));
$redis->connect($this->redisHost, (int)$this->redisPort);
$redis->select((int)$this->redisDatabase);
CLI::write(CLI::color('🟢 Naver Worker running... (Redis: ' . $this->redisHost . ':' . $this->redisPort . ' DB:' . $this->redisDatabase . ')', 'green'));
} catch (\Exception $e) {
CLI::error("Redis 연결 불가: " . $e->getMessage());
return;
@@ -45,7 +48,37 @@ class NaverWorker extends BaseCommand
while (true) {
$result = $redis->brPop(['naver:raw_queue'], 30);
// Redis brPop with retry logic
$maxRetries = 3;
$retryCount = 0;
$result = null;
while ($retryCount < $maxRetries) {
try {
$result = $redis->brPop(['naver:raw_queue'], 30);
break; // 성공하면 루프 탈출
} catch (\RedisException $e) {
$retryCount++;
CLI::write(CLI::color("⚠️ Redis error (attempt {$retryCount}/{$maxRetries}): " . $e->getMessage(), 'yellow'));
if ($retryCount >= $maxRetries) {
CLI::error("❌ Redis reconnection failed after {$maxRetries} attempts");
break;
}
// Redis 재연결 시도
try {
CLI::write(CLI::color('🔄 Reconnecting to Redis...', 'yellow'));
$redis->close();
$redis->connect($this->redisHost, (int)$this->redisPort);
$redis->select((int)$this->redisDatabase);
CLI::write(CLI::color('✅ Redis reconnected', 'green'));
} catch (\Exception $reconnectError) {
CLI::error("Redis reconnection error: " . $reconnectError->getMessage());
sleep(2); // 재연결 실패 시 잠시 대기
}
}
}
if (!$result) {
// 데이터가 없어서 타임아웃 난 경우.

32
app/Commands/TestLog.php Normal file
View File

@@ -0,0 +1,32 @@
<?php
namespace App\Commands;
use CodeIgniter\CLI\BaseCommand;
use CodeIgniter\CLI\CLI;
class TestLog extends BaseCommand
{
protected $group = 'Test';
protected $name = 'test:log';
protected $description = 'Test log_message function';
public function run(array $params)
{
CLI::write('Testing log_message()...', 'yellow');
log_message('error', '===== TEST LOG MESSAGE FROM CLI ===== ' . date('Y-m-d H:i:s'));
log_message('debug', 'Debug level test');
log_message('info', 'Info level test');
$logFile = WRITEPATH . 'logs/log-' . date('Y-m-d') . '.log';
CLI::write('Log file: ' . $logFile);
CLI::write('Exists: ' . (file_exists($logFile) ? 'YES' : 'NO'));
if (file_exists($logFile)) {
CLI::write('Last 10 lines:');
CLI::write(shell_exec('tail -10 ' . escapeshellarg($logFile)));
}
}
}

View File

@@ -39,7 +39,7 @@ class Logger extends BaseConfig
*
* @var int|list<int>
*/
public $threshold = (ENVIRONMENT === 'production') ? 4 : 9;
public $threshold = 9; // Always log everything for debugging
/**
* --------------------------------------------------------------------------

View File

@@ -23,7 +23,7 @@ class Receipt extends BaseController
public function lists(): string
{
error_log('========== Receipt::lists() CALLED ==========');
log_message('error', '========== Receipt::lists() CALLED ==========');
$usr_id = $this->request->getGet('usr_id') ?: '';
$sBonbu = $this->request->getGet('bonbu') ?: '';
@@ -56,7 +56,7 @@ class Receipt extends BaseController
public function getResultList()
{
error_log('========== Receipt::getResultList() CALLED ==========');
log_message('info', '========== Receipt::getResultList() CALLED ==========');
$start = (int) $this->request->getGet('start') ?: 0;
$end = (int) $this->request->getGet('length') ?: 10;
@@ -96,13 +96,13 @@ class Receipt extends BaseController
'srchTxt' => $this->request->getGet('srchTxt'), // 검색어
];
error_log('[Receipt::getResultList] START - start=' . $start . ' length=' . $end);
log_message('info', '[Receipt::getResultList] START - start=' . $start . ' length=' . $end);
$totalCount = $this->model->getTotalCount($data);
error_log('[Receipt::getResultList] TotalCount = ' . $totalCount);
log_message('info', '[Receipt::getResultList] TotalCount = ' . $totalCount);
$datas = $this->model->getResultList($start, $end, $data);
error_log('[Receipt::getResultList] END - returned ' . count($datas) . ' rows');
log_message('info', '[Receipt::getResultList] END - returned ' . count($datas) . ' rows');
return $this->response->setJSON(body: [
'recordsTotal' => $totalCount,

View File

@@ -0,0 +1,27 @@
<?php
namespace App\Controllers;
class LogTest extends BaseController
{
public function index()
{
log_message('error', '===== WEB LOG TEST ===== ' . date('Y-m-d H:i:s'));
log_message('info', 'Info from web');
log_message('debug', 'Debug from web');
$logFile = WRITEPATH . 'logs/log-' . date('Y-m-d') . '.log';
echo "ENVIRONMENT: " . ENVIRONMENT . "<br>";
echo "Log file: " . $logFile . "<br>";
echo "Exists: " . (file_exists($logFile) ? 'YES' : 'NO') . "<br>";
echo "Writable: " . (is_writable(dirname($logFile)) ? 'YES' : 'NO') . "<br>";
if (file_exists($logFile)) {
echo "<h3>Last 20 lines:</h3>";
echo "<pre>";
echo htmlspecialchars(shell_exec('tail -20 ' . escapeshellarg($logFile)));
echo "</pre>";
}
}
}

View File

@@ -0,0 +1,35 @@
<?php
if (!function_exists('write_query_log')) {
/**
* SQL 쿼리 로그를 writable/logs/sql-query-{date}.log에 기록
*
* @param string $message 로그 메시지
* @param string $sql SQL 쿼리 (선택)
*/
function write_query_log(string $message, string $sql = '')
{
$logDir = WRITEPATH . 'logs';
$logFile = $logDir . '/sql-query-' . date('Y-m-d') . '.log';
$timestamp = date('Y-m-d H:i:s');
$logMessage = "[{$timestamp}] {$message}";
if (!empty($sql)) {
$logMessage .= "\nSQL: {$sql}";
}
$logMessage .= "\n" . str_repeat('-', 80) . "\n";
// 로그 디렉토리 확인 및 생성
if (!is_dir($logDir)) {
mkdir($logDir, 0755, true);
}
// 파일에 쓰기
file_put_contents($logFile, $logMessage, FILE_APPEND | LOCK_EX);
// log_message도 호출 (작동하면 좋고, 안되도 파일에는 기록됨)
log_message('error', $message);
}
}

View File

@@ -198,7 +198,7 @@ class ReceiptModel extends Model
$builder->select("COUNT(*) AS cnt", false);
$builder->join('result b', 'b.rcpt_sq = a.rcpt_sq', 'inner');
$builder->join('region_codes c', 'a.rcpt_dong = c.region_cd', 'inner');
$builder->join('region_codes c', 'a.rcpt_dong = c.region_cd', 'left');
$builder->join('departments d', 'b.dept_sq = d.dept_sq', 'left');
$builder->join('users u', 'b.usr_sq = u.usr_sq', 'left');
$builder->join('result_imgs e', "e.rsrv_sq = b.rsrv_sq AND e.img_type = 'I1' AND e.use_yn = 'Y'", 'left');
@@ -410,7 +410,7 @@ class ReceiptModel extends Model
}
$row = $builder->get()->getRowArray();
error_log('[getTotalCount] SQL = ' . $this->db->getLastQuery());
// log_message('debug', '[getTotalCount] SQL = ' . $this->db->getLastQuery());
return (int) ($row['cnt'] ?? 0);
}
@@ -536,7 +536,7 @@ class ReceiptModel extends Model
", false);
$builder->join('result b', 'b.rcpt_sq = a.rcpt_sq', 'inner');
$builder->join('region_codes c', 'a.rcpt_dong = c.region_cd', 'inner');
$builder->join('region_codes c', 'a.rcpt_dong = c.region_cd', 'left outer');
$builder->join('departments d', 'b.dept_sq = d.dept_sq', 'left outer');
$builder->join('users u', 'b.usr_sq = u.usr_sq', 'left outer');
$builder->join('result_imgs e', "e.rsrv_sq = b.rsrv_sq AND e.img_type = 'I1' AND e.use_yn = 'Y'", 'left outer');
@@ -754,8 +754,8 @@ class ReceiptModel extends Model
$result = $builder->get()->getResultArray();
error_log('[getResultList] SQL = ' . $this->db->getLastQuery());
error_log('[getResultList] Result count = ' . count($result));
// log_message('debug', '[getResultList] SQL = ' . $this->db->getLastQuery());
// log_message('info', '[getResultList] Result count = ' . count($result));
return $result;
@@ -826,7 +826,7 @@ class ReceiptModel extends Model
", false);
$builder->join('result b', 'b.rcpt_sq = a.rcpt_sq', 'inner');
$builder->join('region_codes c', 'a.rcpt_dong = c.region_cd', 'inner');
$builder->join('region_codes c', 'a.rcpt_dong = c.region_cd', 'left');
$builder->join('departments d', 'b.dept_sq = d.dept_sq', 'left');
$builder->join('users u', 'b.usr_sq = u.usr_sq', 'left');
$builder->join('result_imgs e', "e.rsrv_sq = b.rsrv_sq AND e.img_type = 'I1' AND e.use_yn = 'Y'", 'left');
@@ -1213,7 +1213,7 @@ class ReceiptModel extends Model
", false);
$builder->join('result b', 'b.rcpt_sq = a.rcpt_sq', 'inner');
$builder->join('region_codes c', 'a.rcpt_dong = c.region_cd', 'inner');
$builder->join('region_codes c', 'a.rcpt_dong = c.region_cd', 'left');
$builder->join('departments d', 'b.dept_sq = d.dept_sq', 'left');

View File

@@ -880,14 +880,16 @@ $usr_nm = session('usr_nm');
function fn_addr_render(data, type, row) {
var str = "";
var addr = row.addr || '';
var lineBreak = addr ? "<br/>" : "";
if (row.rcpt_jibun_addr == null || row.rcpt_jibun_addr == "") {
str = row.addr + "<br/>" + row.rcpt_hscp_nm + " " + row.rcpt_dtl_addr + " " + row.rcpt_ho;
str = addr + lineBreak + (row.rcpt_hscp_nm || '') + " " + (row.rcpt_dtl_addr || '') + " " + (row.rcpt_ho || '');
} else {
str = row.addr + "<br/>" + row.rcpt_hscp_nm + " " + row.rcpt_li_addr + " " + row.rcpt_jibun_addr + " " + row.rcpt_etc_addr;
str = addr + lineBreak + (row.rcpt_hscp_nm || '') + " " + (row.rcpt_li_addr || '') + " " + (row.rcpt_jibun_addr || '') + " " + (row.rcpt_etc_addr || '');
}
return str;
return str.trim();
}
function fn_prd_render(data, type, row) {

2
public/check_env.php Normal file
View File

@@ -0,0 +1,2 @@
<?php
phpinfo(INFO_ENVIRONMENT);

4
public/test.php Normal file
View File

@@ -0,0 +1,4 @@
<?php
// 웹 브라우저에서 실행해서 결과 확인
$f = @fsockopen("192.168.10.243", 6379, $e, $s, 5);
echo $f ? "Web: ✅ Redis Connection Success" : "Web: ❌ Redis Connection Failed ($s)";

29
public/test_ci_log.php Normal file
View File

@@ -0,0 +1,29 @@
<?php
// CodeIgniter 로그 테스트
require __DIR__ . '/../vendor/autoload.php';
$app = require_once FCPATH . '../app/Config/App.php';
$paths = require_once FCPATH . '../app/Config/Paths.php';
// Bootstrap the application
require_once $paths->systemDirectory . 'bootstrap.php';
$app = Config\Services::codeigniter();
$app->initialize();
echo "ENVIRONMENT: " . ENVIRONMENT . "<br>\n";
// 로그 테스트
log_message('error', '===== TEST CI LOG MESSAGE ===== ' . date('Y-m-d H:i:s'));
echo "log_message() 호출 완료<br>\n";
$logFile = WRITEPATH . 'logs/log-' . date('Y-m-d') . '.log';
echo "로그 파일: " . $logFile . "<br>\n";
echo "로그 파일 존재: " . (file_exists($logFile) ? 'YES' : 'NO') . "<br>\n";
if (file_exists($logFile)) {
echo "<pre>";
echo "마지막 10줄:\n";
echo shell_exec("tail -10 " . escapeshellarg($logFile));
echo "</pre>";
}

18
public/test_log.php Normal file
View File

@@ -0,0 +1,18 @@
<?php
// 간단한 테스트
$writablePath = realpath(__DIR__ . '/../writable/logs');
echo "writable/logs 경로: " . $writablePath . "<br>";
echo "writable/logs 쓰기 가능: " . (is_writable($writablePath) ? 'YES' : 'NO') . "<br>";
echo "writable/logs 소유자: ";
system("ls -ld " . $writablePath);
echo "<br>";
// 직접 파일 쓰기 테스트
$testFile = $writablePath . '/test-' . date('Y-m-d') . '.log';
$result = file_put_contents($testFile, date('Y-m-d H:i:s') . " - Direct write test\n", FILE_APPEND);
echo "직접 쓰기 결과: " . ($result !== false ? 'SUCCESS' : 'FAILED') . "<br>";
echo "테스트 파일: " . $testFile . "<br>";
if (file_exists($testFile)) {
echo "파일 내용:<pre>" . file_get_contents($testFile) . "</pre>";
}