daily_log.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 | <?php // 로그 파일을 설정함 $logPath = __DIR__; if( !is_dir($logPath) ) { die('디렉터리가 존재하지 않음'); } date_default_timezone_set('Asia/Seoul'); $dateObj = new DateTime(); $date = $dateObj->format('Ymd'); $fileName = realpath($logPath) . '/' . $date . '.log'; // 입력할 문자열을 준비 $accessTime = $dateObj->format('Y-m-d H:i:s'); $accessFile = $_SERVER['SCRIPT_FILENAME']; $log = "$accessTime $accessFile에 접근함\r\n"; // 파일을 추가 기록 모드로 연다 $fp = fopen($fileName, 'ab'); if( !is_resource($fp) ) { die('파일을 열지 못함'); } // 파일을 잠금(독점 잠금) flock($fp, LOCK_EX); // 로그를 입력함 fwrite($fp, $log); // 파일의 잠금을 해제함 fflush($fp); flock($fp, LOCK_UN); // 파일을 닫음 fclose($fp); |
105.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | <!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>날짜별로 로그 파일을 작성하고 싶을 때</title>
<link href="style.css" rel="stylesheet">
</head>
<body>
<div>
<?php
// 로그 파일을 작성할 프로그램을 읽음
include './daily_log.php';
echo "이하를 로그 파일에 기록함.<br>";
echo $log;
?>
</div>
</body>
</html>
|
20180516.log
2018-05-16 09:10:41 접근함 2018-05-16 09:12:07 접근함 2018-05-16 09:12:07 접근함 2018-05-16 09:12:09 접근함 2018-05-16 09:12:42 접근함 2018-05-16 09:12:43 접근함 2018-05-16 09:12:43 접근함 2018-05-16 09:12:43 접근함 2018-05-16 09:12:45 접근함

댓글 없음:
댓글 쓰기