Solved thread

This post is marked as solved. If you think the information contained on this thread must be part of the official documentation, please contribute submitting a pull request to its repository.

StreamAdapter Logger to stderr is not working for me.

I can't get logging to stderr to work. I want to send messages to the web server log in the same way that error_log works.

// This doesn't work
$logger = new StreamAdapter("php://stderr");
$logger->log('test');

// This does work
error_log('test');

Funny enough I can get Firelogger working but not this simple thing.



85.4k
Accepted
answer
edited Nov '14

I don't think passing something to error_log and sending messages to php://stderr are the same thing. error_log is able to send messages to the SAPI (php-fpm, iis, etc), an email, a network socket, etc. Probably messages passed to error_log are being stored by the SAPI as you can see them in the web server logs

http://lxr.php.net/xref/PHP_5_5/ext/standard/basic_functions.c#_php_error_log_ex



48.3k

Is there a simple way that I can log to the normal web server error log using existing Phalcon tech? I'll use error_log for quick and dirty logging but for longer sessions I will setup with the DI multiple stream logger and it will split to Firelogger.



48.3k

Sorry, I think that I understand better now but is there already a simple wrapper for error_log so that multiple streams can be used? I like to use both and in some cases having Firelogger open will not be convienient. For example if I'm testing things like Oauth that require me to be constantly logging in over and over again, clearing cookies, etc and I'll already have Chromium open as well. Just for versatility I'd like to send to the PHP error log at the same time. I don't want to switch between methods.

This seems like such a basic thing that an error_log wrapper should exist somewhere?



48.3k

@Phalcon too bad that Zephir isn't more complete because then I could add this to Phalcon 2.0. ;)

<?php
namespace Webird\Logger\Adapter;

use Phalcon\Logger\Adapter,
    Phalcon\Logger\AdapterInterface,
    Phalcon\Logger\Formatter\Line as Formatter;

/**
 * Webird\Logger\Adapter\Error
 * Sends messages to PHP error log
 *
 */
class Error extends Adapter implements AdapterInterface
{

    /**
     * {@inheritdoc}
     *
     * @return \Phalcon\Logger\Formatter\Line
     */
    public function getFormatter()
    {
        if (!$this->_formatter) {
            $this->_formatter = new Formatter('%message%');
        }

        return $this->_formatter;
    }

    /**
     */
    public function logInternal($message, $type, $time, $context = [])
    {
        $entry = $this->getFormatter()->format($message, $type, $time, $context);
        error_log($entry);
    }

    /**
     * {@inheritdoc}
     *
     * @return boolean
     */
    public function close()
    {
    }

    /**
     * {@inheritdoc}
     */
    public function begin()
    {
    }

    /**
     * {@inheritdoc}
     * Encodes all collected messages into HTTP headers. This method is registered as a shutdown handler,
     * so transactions will get committed even if you forget to commit them yourself.
     */
    public function commit()
    {
    }

    /**
     * {@inheritdoc}
     */
    protected function flush()
    {
    }
}


85.4k

What do you mean by "too bad that Zephir isn't more complete"?

@Phalcon too bad that Zephir isn't more complete because then I could add this to Phalcon 2.0. ;)

<?php
namespace Webird\Logger\Adapter;

use Phalcon\Logger\Adapter,
   Phalcon\Logger\AdapterInterface,
   Phalcon\Logger\Formatter\Line as Formatter;

/**
* Webird\Logger\Adapter\Error
* Sends messages to PHP error log
*
*/
class Error extends Adapter implements AdapterInterface
{

   /**
    * {@inheritdoc}
    *
    * @return \Phalcon\Logger\Formatter\Line
    */
   public function getFormatter()
   {
       if (!$this->_formatter) {
           $this->_formatter = new Formatter('%message%');
       }

       return $this->_formatter;
   }

   /**
    */
   public function logInternal($message, $type, $time, $context = [])
   {
       $entry = $this->getFormatter()->format($message, $type, $time, $context);
       error_log($entry);
   }

   /**
    * {@inheritdoc}
    *
    * @return boolean
    */
   public function close()
   {
   }

   /**
    * {@inheritdoc}
    */
   public function begin()
   {
   }

   /**
    * {@inheritdoc}
    * Encodes all collected messages into HTTP headers. This method is registered as a shutdown handler,
    * so transactions will get committed even if you forget to commit them yourself.
    */
   public function commit()
   {
   }

   /**
    * {@inheritdoc}
    */
   protected function flush()
   {
   }
}


48.3k

Just playing around. Anyways it is just a trivial wrapper.



48.3k

Here is my solution. I use the error_log wrapper for the CLI interface. Otherwise it logs to /var/log/nginx and to Firelogger.

$di->setShared('debug', function() use ($di) {
    $config = $di->getConfig();

    $logger = new MultipleStreamLogger();
    switch (ENVIRONMENT) {
        case 'dev':
            if ('cli' == php_sapi_name()) {
                $logger->push(new ErrorLogger());
            } else {
                $debugLogFile = str_replace('{{name}}', $config->site->domains[0],
                    $config->dev->path->debugLog);
                $fileLogger = new FileLogger($debugLogFile);
                $fileLogger->getFormatter()->setFormat('%message%');
                $logger->push($fileLogger);

                $logger->push(new Firelogger());
            }
        break;
    }

    return $logger;
});