Version: @(#) $Id: log_watcher_class.php,v 1.4 2004/07/27 07:55:36 mlemos Exp $
Log watcher
Manuel Lemos (mlemos@acm.org)
Copyright © (C) Manuel Lemos 2003
@(#) $Id: log_watcher_class.php,v 1.4 2004/07/27 07:55:36 mlemos Exp $
Watch a log file and send the newly added log lines to a given e-mail address.
Set the log_file_name variable with the name of the log file that is meant to be watched.
Call the function OpenLog to start watching a new log file or to resume watching a previously analysed log file.
Call the function MailNews to send the newly added log file lines to the e-mail address specified by the variable watcher_email.
Call the function CloseLog to close the log file and store the position of the current end of log line in the pointer file. The pointer file name is defined by the log_file_name variable appending the log_pointer_extension configuration variable value.
string
''
Store the message that is returned when an error occurs.
Check this variable to understand what happened when a call to any of the class functions has failed.
string
''
Name of the log file to watch.
Set this variable to the full or relative path of the log file before it is watched by the class functions.
string
'.ptr'
Extension to append to the log file name to define the name of the file where is stored a pointer that records the last log line that was processed.
Change this variable only of the default is not suitable.
string
''
E-mail address of the person that will be notified about the new log lines.
Set this variable with the address to where the report of the new log lines is sent by the function MailNews.
string
''
Name of the person that will be notified about the new log lines.
Setting this variable is optional.
string
''
E-mail address of the system that will be sending the new log lines report messages.
Set this variable with the address that will be used by the function MailNews as sender of the messages. If possible, this address will also serve as bounce address in case it is not possible to deliver the messages for some reason.
string
'Log Watcher'
Name of the person that will be notified about the new log lines.
Setting this variable is optional.
string
'http://www.phpclasses.org/logwatcher $Revision: 1.4 $'
Identification of the mailer system that is specified in the X-Mailer: headers as sender of the report e-mail messages.
Do not change this variable unless you have a reason to change the identification of the mailer system that is sending the report messages.
string
''
Name of the global function that will be called to send e-mail messages.
If for some reason the PHP mail() function does not work in your environment or you prefer to use a better alternative, set this variable to the name of the alternative global function. It should take the same parameters as the PHP mail() function.
If this variable is set to an empty string, the PHP mail() function will be used.
string
'Hello {WATCHER},{BREAK}{BREAK}The log file {LOG} has the following new lines:{BREAK}{BREAK}{LINES}{BREAK}{SYSTEM}{BREAK}'
Template that defines how the new log lines report messages are formatted.
Change this variable only if it is necessary to change the default report message template.
The template is a string that has special marks that are replaced by specific values. These are the currently supported marks:
{BREAK} - message line break. Used this instead of hardcoded line breaks as the class will replace these marks by the appropriate line breaking character sequence according to the system sending the messages.
{LINES} - Text of the newly added lines.
{LOG} - Name of the log file as defined by the log_file_name variable.
{SYSTEM} - Name of the system that is sending the report messages as defined by the system_name variable.
{WATCHER} - Name of the person to send the report messages as defined by the watcher_name variable.
bool OpenLog()
Open the log file and seek to the position of the line that was read for the last time, or to the end of the file if the log is being opened for the first time.
Call this function first to start processing the log file. Specify the name of the log file in the log_file_name variable before calling this function.
Indicates whether the log file was successfully opened.
bool GetNewLogLine(
Retrieve the next new line of the log if it was not yet reached the end of the file. This function is mostly meant to assist the function MailNews or others that may be implemented in the future.
If you need to use this function, call it after successfully calling the OpenLog function. Check the value of the end_of_log argument after calling this function to determine if it was reached the end of the log file. If the end of the log file was not yet reached, the line argument will return the log line that was read, already without any end of line characters.
line - Return the next line that to be read from the log file.
end_of_log - Return a flag value that indicates if it was reached the end of the log file.
Indicates whether the next log line was retrieved successfully or it was reached the end of the log file.
bool MailNews(
Read all the new lines of the log file and send them in a message to the address defined in the watcher_email variable. If there are no new lines in the log file, nothing happens.
Call this function after successfully calling the OpenLog function. Before calling this function, specify the addresses of the watcher person that will receive the report messages and the system that is sending them in the variables watcher_email and system_email respectively.
subject - Subject of the message to be sent.
Indicates whether the message was successfully sent or there were no new log files to notify.
bool CloseLog()
Close a previously opened log file and stores the end of log file position in the log pointer file.
Call this function after calling the function MailNews successfully.
Indicates whether the closing the log file and storing the log pointer position was done successfully.