I'm writing a PHP log file class but I want to add color to the line that is written to the file.
The issue I'm having is the color changes the color of the terminal as well, what I want to achieve is to change color of line written to the log file only.
class logClass extends Singleton {
private function checkDate() {
return date("onSj");
}
public function logNotice($str) {
$this->write($str, "\033[33m");
}
public function write($string, $color) {
$fileName = $this->checkDate();
$handle = fopen('error.log', 'a');
fwrite($handle, "$color" . date("Y-m-d H:i:s") . $string . "\n");
fclose($handle);
}
}
You should add an end color mark sequence. Eg:
sprintf("\033[33m%s\033[0m", $text)
Here is a list of color codes taken from https://github.com/kevinlebrun/colors.php
Example usage:
sprintf($colorFormats['green'], $someText)