10

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);
    }
}
4
  • 2
    you should add an end color mark sequence. Eg: sprintf("\033[33m%s\033[0m", $text) Commented Oct 27, 2012 at 15:16
  • @catalin.costache post it as an answer plz so I could mark it Commented Oct 27, 2012 at 15:28
  • 1
    Thats a nice idea to colorize logs! Thanks :) +1 Commented Oct 27, 2012 at 16:25
  • logs should be colored:) il upload the class tomorrow on github ;) Commented Oct 27, 2012 at 16:26

1 Answer 1

24

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

$colorFormats = array(
        // styles
        // italic and blink may not work depending of your terminal
        'bold' => "\033[1m%s\033[0m",
        'dark' => "\033[2m%s\033[0m",
        'italic' => "\033[3m%s\033[0m",
        'underline' => "\033[4m%s\033[0m",
        'blink' => "\033[5m%s\033[0m",
        'reverse' => "\033[7m%s\033[0m",
        'concealed' => "\033[8m%s\033[0m",
        // foreground colors
        'black' => "\033[30m%s\033[0m",
        'red' => "\033[31m%s\033[0m",
        'green' => "\033[32m%s\033[0m",
        'yellow' => "\033[33m%s\033[0m",
        'blue' => "\033[34m%s\033[0m",
        'magenta' => "\033[35m%s\033[0m",
        'cyan' => "\033[36m%s\033[0m",
        'white' => "\033[37m%s\033[0m",
        // background colors
        'bg_black' => "\033[40m%s\033[0m",
        'bg_red' => "\033[41m%s\033[0m",
        'bg_green' => "\033[42m%s\033[0m",
        'bg_yellow' => "\033[43m%s\033[0m",
        'bg_blue' => "\033[44m%s\033[0m",
        'bg_magenta' => "\033[45m%s\033[0m",
        'bg_cyan' => "\033[46m%s\033[0m",
        'bg_white' => "\033[47m%s\033[0m",
    );

Example usage:

sprintf($colorFormats['green'], $someText)

Sign up to request clarification or add additional context in comments.

3 Comments

hey @catalin.costache I had a similar question. Think you might be able to check it out?
Thank you. You can use this to avoid sending headers error_log(sprintf($colors[$color], $message));.
Use sprintf("\033[30;47m%s\033[0m", xxx) to mix black foreground and white background

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.