Good time. You need to implement a log of the following form:

<месяцгод>07.11<\месяцгод> <день>01<\день> [id][description][date_entered]; ... <день>02<\день> [id][description][date_entered]; ... <день>dd<\день> <месяцгод>08.11<\месяцгод> ... <месяцгод>mm.yy<\месяцгод> ... 

And then read the information by month and day. How to implement it competently and with the help of what? Those. for example, write to a text file and then create a function that will search for the desired position and return "[id] [description] [date_entered]; ..." for each day of the month? Or is there a more convenient implementation, XML?

  • <месяцгод>07.11<\месяцгод><день>01<\день> And what's the point of this for xml, if the day does not relate to the month?) Then <месяцгод значение="07.11"><день значение="01" id="01-07-11" date_entered="???">description</день></месяцгод> - Sh4dow
  • Suggested. I want to know a simple implementation, because I’ve already come up with a concise one. I don’t know how to make it more convenient, well, in general, the meaning is such that it would be easy to find records on specific days in the log (there will be several records a day) of the month. - Afipsky

3 answers 3

Simple implementation

 $message = array( 'id' => 15, 'date' => '01.07.2011', 'message' => 'raz raz raz' ) function placeMessage($logfile, $msg) { if (!$f = @fopen($logfile, 'a+')) return false; fwrite($f, $msg['date'].'#'.str_replace("\n", '##N##', serialize($msg))); fclose($f); return true; } function getMessage($logfile, $date) { $result = array(); if (!$f = @fopen($logfile, 'r+')) return $result; while ($str = @fgets($f)) { list($dateMsg, $msg) = explode('#', $str, 2); if ($date == $dateMsg) $result[] = unserialize(str_replace('##N##', "\n", $msg)); } fclose($f); return $result; } 

In general, yes, in your case, the database is more suitable (if there is a possibility).

    Simple text files are better suited for logging. To format the logs, you can use the built-in implementation of fputcsv()

     $log = array( time(), '127.0.0.1', 'Some message' ); $fp = fopen('application.log', 'a'); fputcsv($fp, $args); fclose(); 

    And accordingly search:

     $from = strtotime('2011-15-06'); $to = strtotime('2011-15-07'); $fp = fopen('application.log', 'r'); while ( ($log = fgetcsv($fp)) ) { if ($log[0] < $from) { continue; } if ($log[0] > $to) { break; } $log[0] = date('r', $log[0]); vprintf('[%s] %s: %s', $log); } fclose(); 

      If stored in a file, it is better to get rid of the extra information inherent in the CML format (stored in CVS), so as not to waste time parsing data:

       --year;month;day;id;description;date_entered; 2011;03;12;01;Hello Word;'' 

      If you still want to store in XML, then it would be worth bringing it to a more acceptable form:

       <year value="2011"> <month value="01"> <day value="15"> <!-- вариан А --> <item> <id>a</id> <desc>b</desc> <date>c</date> </item> <item> <id>a2</id> <desc>b2</desc> <date>c2</date> </item> <!-- вариан Б --> <item id="a" desc="b" date="c" /> <item id="a2" desc="b2" date="c2" /> </day> <day value="16"> ... </day> </month> <month value="02"> ... </month> </year> 

      A no-brainer, it is better to choose the names of the tags shorter to reduce the total amount of data, as well as the time spent on their analysis and reading. Therefore, this option will be more optimal:

        <item year="x" month="y" day="z"> <id>a</id> <desc>b</desc> <date>c</date> </item> 

      or

       <items year="x" month="y" day="z"> <item id="a" desc="b" date="c" /> </items> 

      You can easily handle CML with xpath, but if this option does not suit you, you can look for tools that allow you to work with CML as a database (unfortunately, I don’t remember the names).