Note: Use this Appender with command-line php scripts.
* On web scripts this appender has no effects.
*
* Configurable parameters of this appender are:
*
* - layout - The layout (required)
* - target - "stdout" or "stderr"
*
* An example php file:
*
* {@example ../../examples/php/appender_console.php 19}
*
* An example configuration file:
*
* {@example ../../examples/resources/appender_console.properties 18}
*
* @version $Revision: 883108 $
* @package log4php
* @subpackage appenders
*/
class LoggerAppenderConsole extends LoggerAppender {
const STDOUT = 'php://stdout';
const STDERR = 'php://stderr';
/**
* Can be 'php://stdout' or 'php://stderr'. But it's better to use keywords STDOUT and STDERR (case insensitive).
* Default is STDOUT
* @var string
*/
private $target = self::STDOUT;
/**
* @var boolean
* @access private
*/
protected $requiresLayout = true;
/**
* @var mixed the resource used to open stdout/stderr
* @access private
*/
protected $fp = null;
public function __destruct() {
$this->close();
}
/**
* Set console target.
* @param mixed $value a constant or a string
*/
public function setTarget($value) {
$v = trim($value);
if ($v == self::STDOUT || strtoupper($v) == 'STDOUT') {
$this->target = self::STDOUT;
} elseif ($v == self::STDERR || strtoupper($v) == 'STDERR') {
$this->target = self::STDERR;
}
}
public function activateOptions() {
$this->fp = fopen($this->target, 'w');
if(is_resource($this->fp) && $this->layout !== null) {
fwrite($this->fp, $this->layout->getHeader());
}
$this->closed = (bool)is_resource($this->fp) === false;
}
/**
* @see LoggerAppender::close()
*/
public function close() {
if($this->closed != true) {
if (is_resource($this->fp) && $this->layout !== null) {
fwrite($this->fp, $this->layout->getFooter());
fclose($this->fp);
}
$this->closed = true;
}
}
public function append(LoggerLoggingEvent $event) {
if (is_resource($this->fp) && $this->layout !== null) {
fwrite($this->fp, $this->layout->format($event));
}
}
}