seoppc
seoppc

Reputation: 2824

write text file with new line

I am using following method to write into log.txt

public function write($message, $file=null, $user=null)
{
    $message = date("d/m/y : H:i:s", time())  .' - '.$message;
    $message .= is_null($file) ? '' : " in $file";
    $message .= is_null($user) ? '' : " by $user";
    $message .= "\n";
    return file_put_contents( $this->logfile, $message, FILE_APPEND );
}

but when i check track.log file, its really messy, but i want now is that every new log comment should be come into new line.

Upvotes: 1

Views: 1986

Answers (2)

javiertoledos
javiertoledos

Reputation: 991

You should use "\r\n" instead of "\n". In some programs (ex. notepad) you will see all in one line if you not put the carriage return escape char sequence "\r" before new line "\n".

When you use the constant PHP_EOL it will vary between operating systems. For example, if you run your script in Windows PHP_EOL has a value of "\r\n", but if you run the same script in linux you will get only "\n".

Upvotes: 2

aurelijusv
aurelijusv

Reputation: 554

Use constant PHP_EOL (maybe even 2 times) in the end of the message for the best new line behavior in text files.

Upvotes: 6

Related Questions