Note that there are some explanatory texts on larger screens.

plurals
  1. POOpinions on implemention of event IDs in log messages
    text
    copied!<p>I have a logger class (in PHP, but that does not matter) which spits out log messages. The logger is a custom implementation and works well. However, I would like to extend it to provide a event ID with each <em>type</em> of logger message. Such that "User logged in messages" are event ID 1, "Form validation failed" is event ID 2, for example. </p> <p>The purpose of this event ID is to filter unimportant events when a long list of logs is viewed. I believe it would be quicker to categorize logs by event ID and faster to query than doing substring searches in a database.</p> <p>I have the following ideas, but welcome viable alternatives. I used PHP in this example, but something that is generic enough that it could be applied to most languages such as PHP/Java/C#/etc would be useful. </p> <p><strong>1) Include an ID implicitly in the log function call:</strong></p> <pre><code>abstract class EventId { const LOGIN = 1; const VALIDATION_FAILURE = 2; // etc } Logger::messageDebug(EventID::LOGIN, $username . " logged in"); Logger::messageWarning(EventID::VALIDATION_FAILURE, "Form failed to validate, etc."); </code></pre> <p>The advantages of this are simplicity, but I fear it could get a little messy. </p> <p><strong>2) Pass variable parameters to log messages as separate arguments, then hash the first string</strong></p> <pre><code>Logger::messageDebug("%s logged in", $username); Logger::messageWarning("The %s form failed to validate", $form); </code></pre> <p>The advantages are simplicity, the disadvantages are the event ID depends upon hashing (or some other function) of the first string to differentiate the types of log messages. </p> <p><strong>3) Backtrace and do some nastyness</strong></p> <pre><code>function messageDebug($message) { $trace = obtainTraceOfCallToMessageDebug(); $callToMessageDebug = $trace[0]; $eventId = ""; $eventId .= $eventId-&gt;file; $eventId .= $eventId-&gt;line; $eventId = sha1($eventId); messageImpl($eventId, $message); } </code></pre> <p>This is pretty nasty, but it does have an advantage in that existing log messages do not need to be changed, additionally it is less fragile when writing new log messages. In PHP, obtaining a backtrace is easy and sort of cheap, in other languages you may have to throw an Exception which is super nasty. Anyway, it's an idea.</p> <p>Looking forward to your opinions. My main requirement is maintainability rather than speed. I am currently looking towards number 1 - keep it simple. </p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload