导读:今天编程笔记来给各位分享关于php怎么读取日志文件的相关内容,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在开始吧!
本文目录一览:
1、php 一个10g的日志文件如何取出出现次数最多得ip
2、PHP每小时读取1个小时前的日志写入数据库中删除数据库中两天前的日志就是数据库里只保留最近两天的日志
3、如何用php抓取windows下“事件查看器中的log”?谢谢!
4、怎么用php读取日志文件?并把需要的字段取出来保存到数据库?
5、PHP读取大日志文件,怎么玩
php 一个10g的日志文件如何取出出现次数最多得ip
//php直接用file_get_contents去读取10g的文件内存可以会爆,建议使用fgets函数一行一行取
$file = fopen("1.log","r");
while(!feof($file))
{
echo fgets($file). "br /br /br /";
}
fclose($file);
PHP每小时读取1个小时前的日志写入数据库中删除数据库中两天前的日志就是数据库里只保留最近两天的日志
如果你是两天前的日志文件完全不想要了可以每隔一小时生成一个日志文件,并把两天前的日志文件删除掉。用filectime函数可以判断文件时间。如果你想隔一个小时生成一个文件,可以给日志起一个通用的名字,例如:errorlog_当天的日期.log,然后每次写日志时判断这个文件的时间是不是1小时前的,是1小时前的就把这个文件改个名,不是就继续写入。
然后用file函数获取到你想要的时间的日志文件的内容做你需要的处理。
如果两天前的日志文件还想要,那么你就要先用file函数取得日志文件的内容,通过日志内容的里的时间和现在时间的比对取得你要的数据并处理。
把你对日志文件的处理些到php文件里。
如果是linux服务器,建议使用 crontab定时执行这个写好的PHP文件
如果是windows服务器,建议使用内置的 计划任务功能定时执行这个写好的PHP文件
如何用php抓取windows下“事件查看器中的log”?谢谢!
windows的log文件一般存放在C:\WINDOWS\System32\Config\下,后缀为.evt,记事本打开会乱码,直接读写应该没有问题,但是乱码的问题怎么解决我不太清楚.
下面是在网上找到的, 大概意思是调用windows api, 我没试过,所以发原文你自己理解吧.
在注册表中的位置,在HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Eventlog\Category::File中记录。
Category为Application,Security和System。如果计算机是域控制器,事件日志还包括目录服务和DNS服务的相关部分。
在程序中读取Event Log,可以使用标准Windows API函数实现。在
MSDN Library/Platform SDK/Base Services/Denugging and Error Handlings/Event Logging中可以查到详尽的信息。
使用API函数ReadEventLog()
Platform SDK: Debugging and Error Handling
ReadEventLog
The ReadEventLog function reads a whole number of entries from the specified event log. The function can be used to read log entries in chronological or reverse chronological order.
BOOL ReadEventLog(
HANDLE hEventLog, // handle to event log
DWORD dwReadFlags, // how to read log
DWORD dwRecordOffset, // offset of first record
LPVOID lpBuffer, // buffer for read data
DWORD nNumberOfBytesToRead, // bytes to read
DWORD *pnBytesRead, // number of bytes read
DWORD *pnMinNumberOfBytesNeeded // bytes required
);
Parameters
hEventLog
[in] Handle to the event log to read. This handle is returned by the OpenEventLog function.
dwReadFlags
[in] Specifies how the read operation is to proceed. This parameter must include one of the following values. Value Meaning
EVENTLOG_SEEK_READ The read operation proceeds from the record specified by the dwRecordOffset parameter.
This flag cannot be used with EVENTLOG_SEQUENTIAL_READ.
EVENTLOG_SEQUENTIAL_READ The read operation proceeds sequentially from the last call to the ReadEventLog function using this handle.
This flag cannot be used with EVENTLOG_SEEK_READ.
If the buffer is large enough, more than one record can be read at the specified seek position; you must specify one of the following flags to indicate the direction for successive read operations. Value Meaning
EVENTLOG_FORWARDS_READ The log is read in chronological order.
This flag cannot be used with EVENTLOG_BACKWARDS_READ.
EVENTLOG_BACKWARDS_READ The log is read in reverse chronological order.
This flag cannot be used with EVENTLOG_FORWARDS_READ.
dwRecordOffset
[in] Specifies the log-entry record number at which the read operation should start. This parameter is ignored unless dwReadFlags includes the EVENTLOG_SEEK_READ flag.
lpBuffer
[out] Pointer to a buffer for the data read from the event log. This parameter cannot be NULL, even if the nNumberOfBytesToRead parameter is zero.
The buffer will be filled with an EVENTLOGRECORD structure.
nNumberOfBytesToRead
[in] Specifies the size, in bytes, of the buffer. This function will read as many whole log entries as will fit in the buffer; the function will not return partial entries, even if there is room in the buffer.
pnBytesRead
[out] Pointer to a variable that receives the number of bytes read by the function.
pnMinNumberOfBytesNeeded
[out] Pointer to a variable that receives the number of bytes required for the next log entry. This count is valid only if ReadEventLog returns zero and GetLastError returns ERROR_INSUFFICIENT_BUFFER.
Return Values
If the function succeeds, the return value is nonzero.
If the function fails, the return value is zero. To get extended error information, call GetLastError.
Remarks
When this function returns successfully, the read position in the error log is adjusted by the number of records read. Only a whole number of event log records will be returned.
Note The configured filename for this source may also be the configured filename for other sources (several sources can exist as subkeys under a single logfile). Therefore, this function may return events that were logged by more than one source.
For example, see Reading the Event Log.
Requirements
Windows NT/2000 or later: Requires Windows NT 3.1 or later.
Windows 95/98/Me: Unsupported.
Header: Declared in Winbase.h; include Windows.h.
Library: Use Advapi32.lib.
Unicode: Implemented as Unicode and ANSI versions on Windows NT/2000.
See Also
Event Logging Overview, Event Logging Functions, ClearEventLog, CloseEventLog, EVENTLOGRECORD, OpenEventLog, ReportEvent
Platform SDK Release: February 2001 Contact Platform SDK Order a Platform SDK CD Online
Requirements
Windows NT/2000 or later: Requires Windows NT 3.1 or later.
Windows 95/98/Me: Unsupported.
Header: Declared in Winbase.h; include Windows.h.
Library: Use Advapi32.lib.
Unicode: Implemented as Unicode and ANSI versions on Windows NT/2000.
See Also
Event Logging Overview, Event Logging Functions, ClearEventLog, CloseEventLog, EVENTLOGRECORD, OpenEventLog, ReportEvent
怎么用php读取日志文件?并把需要的字段取出来保存到数据库?
这个哪有具体的写法,
1、打开文件(file_get_contents,或其他)
2、正则表达式匹配需要的内容(preg_match,或其他)
3、整理数据保存数据库
PHP读取大日志文件,怎么玩
应该一行一行进行处理,不能一次加载到内存。
1 $line = '';
2
3 $f = fopen('data.txt', 'r');
4 $cursor = -1;
5
6 fseek($f, $cursor, SEEK_END);
7 $char = fgetc($f);
8
9 /**
10 * Trim trailing newline chars of the file
11 */
12 while ($char === "\n" || $char === "\r") {
13 fseek($f, $cursor--, SEEK_END);
14 $char = fgetc($f);
15 }
16
17 /**
18 * Read until the start of file or first newline char
19 */
20 while ($char !== false $char !== "\n" $char !== "\r") {
21 /**
22 * Prepend the new char
23 */
24 $line = $char . $line;
25 fseek($f, $cursor--, SEEK_END);
26 $char = fgetc($f);
27 }
28
29 echo $line;
结语:以上就是编程笔记为大家整理的关于php怎么读取日志文件的全部内容了,感谢您花时间阅读本站内容,希望对您有所帮助,更多关于php怎么读取日志文件的相关内容别忘了在本站进行查找喔。