Logging to App Engine syslog(LOG_ERR, etc

Hi I am using Slim as a front end to App engine. Awesome results but trying to improve the logging. Currently injecting Monolog with the following
$container[‘logger’] = function($c) {
$logger = new \Monolog\Logger(‘my_logger’);
$file_handler = new \Monolog\Handler\StreamHandler(‘php://stdout’, \Monolog\Logger::DEBUG);
$logger->pushHandler($file_handler);
// $logger->pushHandler(new \Monolog\Handler\ErrorLogHandler());
return $logger;
};
But the only way to get it into the classes seems to be to inject it from the parent route file (index), and keep injecting it all the way down to where you need it along with any branches. The result of all this is a very ugly but viewable log entry in app engines logs. I already have syslog being called in all these places like
syslog(LOG_WARNING, print_r($e, true));
is there anyway to pipe that rather than rewrite it all ? Awesome results by the way.

No, you don’t have to keep passing your logger through all your function calls. The point of the container is that you’re able to do this:

global $app;
$container = $app->getContainer();
$logger = $container->get('logger');
$logger->log('some message goes here');

As such, the container gives you a global instance from which you can retrieve common/basic application components anywhere in your code without having to pass stuff around.

But the only way to get it into the classes seems to be to inject it from the parent route file (index), and keep injecting it all the way down to where you need it along with any branches.

Do you make use of constructor dependency injection? If yes, this would be fine and should not interfere the logging entry itself.

The result of all this is a very ugly but viewable log entry in app engines logs.

Ugly is relative. What do you mean exactly with “ugly”. Do you have an example log entry?

I already have syslog being called in all these places like syslog(LOG_WARNING, print_r($e, true));

Why do you use syslog if you already use molog? Monlog has a syslog handler too.

is there anyway to pipe that rather than rewrite it all ?

Yes, just add the Monolog SyslogHandler.

$logger = new \Monolog\Logger('my_logger');

// Add the SyslogHandler
$ident = 'SilverStripe_log';
$facility = LOG_USER;
$syslog = new \Monolog\Handler\SyslogHandler($ident, $facility, \Monolog\Logger::DEBUG);
$formatter = new \Monolog\Formatter\LineFormatter("%level_name%: %message% %context%");
$syslog->setFormatter($formatter);
$logger->pushHandler($syslog);

// Optional add other handler
$filename = 'tmp/error.log';
$logger->pushHandler(new RotatingFileHandler($filename, 0,  Logger::ERROR, true, 0775));

return $logger;

I am passing the logger into static methods of a class, so I have to get the logger into the class somehow right ? either by injection it into the call (MyClass::mymethod(logger), or declaring it global at the start of the static procedure no?, Global would obviously be cleaner but if I declare it global at the start of the static class method it doesn’t see it. Trying to see all this in terms of dependency injection in angular but brain still goes tilt on it :slight_smile: Are you saying inject it into the class constructor somehow?