2

I am completelly new to Slim. I have used php for the last 3-4 years but I have always done everything from scratch. I want to learn this frameworks for some rest services I have to do.

I have followed a tutorial on the slim webpage to get a simple rest service working but I want to add a log system to see what is happening when something goes wrong or wathever.

This is what I have right know:

<?php
use \Psr\Http\Message\ServerRequestInterface as Request;
use \Psr\Http\Message\ResponseInterface as Response;
require '../Slim/Slim.php';

\Slim\Slim::requisterAutoloader();

$application = new \Slim\App();
$logger = $application->log;
$logger->setEnabled(true);
$logger->setLevel(\Slim\Log::DEBUG);

$application->get(
  '/hello/user',
  function ()
  {
    GLOBAL $logger;
    $logger->info("starting the handling function");
    echo "<data>response</data>";
    $logger->info("ending handling function");
  });

$application->run();
?>

I also tried with monolog but I didn't get it working.

<?php
use \Psr\Http\Message\ServerRequestInterface as Request;
use \Psr\Http\Message\ResponseInterface as Response;

use Monolog\Logger;
use Monolog\Handler\StreamHandler;

require '../vendor/autoload.php';


$logger = new \Flynsarmy\SlimMonolog\Log\MonologWriter(array(
    'handlers' => array(
        new \Monolog\Handler\StreamHandler('./logs/'.date('Y-m-d').'.log'),
    ),
));

$app = new Slim\App(array(
 'log.writer' => $logger,
));


$app->get('/hello/{name}', function (Request $request, Response $response){
    $name = $request->getAttribute('name');
    $response->getBody()->write("Hello, $name");
    //$app->log->writer("hola");
    $this->logger->info("Slim-Skeleton '/' route");
    return $response;
    });
    $app->run();

What I would really want is to have a daily log with warnings, debug, info... in the same file. Changing the file every day.

3
  • What version of slim are you trying to use. You appear to be trying to combine but slim 3 and 2 code.
    – meun5
    Commented Apr 2, 2017 at 3:52
  • Log file is writable by web-server?
    – Max P.
    Commented Apr 2, 2017 at 6:26
  • I am using 3.8. I think it is the latest one.
    – Biribu
    Commented Apr 2, 2017 at 8:19

1 Answer 1

5

The slim-skeleton application shows how to integrate monolog.

  1. Register the logger with the DI container:

    $container['logger'] = function ($c) {
        $settings = $c->get('settings')['logger'];
        $logger = new \Monolog\Logger('test-app');
        $logger->pushHandler(new Monolog\Handler\StreamHandler('php://stdout', \Monolog\Logger::DEBUG));
        return $logger;
    };
    
  2. Use it in your closure:

    $app->get('/hello/{name}', function (Request $request, Response $response){
        $name = $request->getAttribute('name');
        $response->getBody()->write("Hello, $name");
        $this->get('logger')->info("Slim-Skeleton '/' route");
        return $response;
    });
    

The logs will out output to stdout, so you'll see the in your apache error log or if you're using the built-in PHP server, in the terminal.

0

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.