0

Read the old content into $content, then write $string . $content back into the file: not working, new messages are printed at the end of the file.

Relevant methods in the Logger class:

public function __construct($filename)
{
    $this->filename = $filename;
    $this->fp = fopen($this->filename, "w+");

    if (!$this->fp) throw new Exception("Errore nel file: " . $this->filename);
}

protected function log($severity, $message)
{
    $string = sprintf("[%s] (%s): %s", $severity, date('d/m/Y H:i:s'), $message);
    $content = !filesize($this->filename)? '' :
        fread($this->fp, filesize($this->filename));

    fwrite($this->fp, $string . $content . "\n");

    return $message;
}
hakre
  • 193,403
  • 52
  • 435
  • 836
gremo
  • 47,186
  • 75
  • 257
  • 421
  • 1
    I suggest you to append your message at the end of file like every other loggers and use @mario solution – dynamic Dec 04 '11 at 12:32
  • possible duplicate of [How do I prepend file to begining?](http://stackoverflow.com/questions/3332262/how-do-i-prepend-file-to-begining) – mario Dec 04 '11 at 12:46
  • 1
    You **really should not prepend to logfiles**. It requires re-writing the **whole** file everytime. If you just want an easy way to see the latest log entries, you can always use the `tail` program on the shell to get the last N lines. – ThiefMaster Dec 04 '11 at 14:06

3 Answers3

2

For logging you should use:

file_put_contents($filename, $content, FILE_APPEND|LOCK_EX);

That's not only less code, but also takes care of locking the file (no concurrent access and overwriting of the appended content).

mario
  • 144,265
  • 20
  • 237
  • 291
  • 2
    This will append the line. He wants to prepend it as far as I understand. – favoretti Dec 04 '11 at 12:30
  • that's what I use. And that's why i love php – dynamic Dec 04 '11 at 12:31
  • @favoretti: Ok right, I just went by the question title. Nevertheless prepending is not advisable. That's not what OS filesystem funcs are optimized for. I would rather *for displaying only* read the file in with `tac` or just `array_reverse(file($log))`, rather than slow down the logging or write elaborate code for that. (He'd still need locking.) – mario Dec 04 '11 at 12:35
  • @mario: Totally agree with you. Depends on file size tho. If that's a logfile of some 10-15k, it won't be that much a performance hit. If it's spitting out megabytes of data, for sure that's not the way to go. – favoretti Dec 04 '11 at 12:39
0

How about:

$contents = file_get_contents($file);
$contents = $string . $contents;
... fwrite();
favoretti
  • 29,299
  • 4
  • 48
  • 61
  • While your codes *does* work, I'd shoot any developer doing that in a project I control. Rewriting a potentially huge log file is nothing you ever want do do. Unfortunately there's no other way if prepending is really what the guy wants. – ThiefMaster Dec 04 '11 at 12:34
  • That's besides the point, right? :) He wants to prepend, that's one of the ways to do it. Nowhere in the question are we setting other constraints like size of logfile or anything :) – favoretti Dec 04 '11 at 12:38
  • @ThiefMaster: sorry, forgot to tag you. – favoretti Dec 04 '11 at 12:40
0

try to replace this code

$this->fp = fopen($this->filename, "w+");

with this one:

$this->fp = fopen($this->filename, "a+");
Christopher Pelayo
  • 792
  • 11
  • 30