Skip to content

Commit

Permalink
Added CliLogger
Browse files Browse the repository at this point in the history
  • Loading branch information
janpecha committed Oct 22, 2017
1 parent 713c021 commit 3226f3b
Showing 1 changed file with 59 additions and 0 deletions.
59 changes: 59 additions & 0 deletions src/CliLogger.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php

namespace CzProject\Logger;


class CliLogger implements ILogger
{
/** @var int */
private $level;

/** @var bool */
private $colored;

/** @var array */
protected static $colors = array(
ILogger::SUCCESS => '0;32',
ILogger::ERROR => '0;31',
ILogger::EXCEPTION => '0;31',
ILogger::CRITICAL => '0;31',
ILogger::WARNING => '0;33',
ILogger::DEBUG => '1;30',
);


/**
* @param int
*/
public function __construct($level = ILogger::INFO, $colored = NULL)
{
$this->level = $level;
$this->colored = $colored !== NULL ? $colored : self::detectColoredOutput();
}


public function log($msg, $level = ILogger::INFO)
{
if ($level >= $this->level) {
if ($this->colored && isset(self::$colors[$level])) {
echo "\033[", self::$colors[$level], 'm', $msg, "\033[0m\n";

} else {
echo $msg, "\n";
}
}
}


/**
* @return bool
*/
public static function detectColoredOutput()
{
// Code from Tracy (from Nette Framework)
// see https://github.com/nette/tracy/blob/master/src/Tracy/Dumper.php#L315-L317
return (getenv('ConEmuANSI') === 'ON'
|| getenv('ANSICON') !== FALSE
|| (defined('STDOUT') && function_exists('posix_isatty') && posix_isatty(STDOUT)));
}
}

0 comments on commit 3226f3b

Please sign in to comment.