Write log file on your Slim 3 application with Monolog logger.
Via Composer
$ composer require projek-xyz/slim-monolog --prefer-dist
Requires Slim micro framework 3 and PHP 5.5.0 or newer.
// Create Slim app
$app = new \Slim\App();
// Fetch DI Container
$container = $app->getContainer();
// Register Monolog helper:
// Option 1, using MonologProvider
$container->register(new \Projek\Slim\MonologProvider);
// Option 2, using Closure
$container['logger'] = function ($c) {
$settings = [
// Path to log directory
'directory' => 'path/to/logs',
// Log file name
'filename' => 'my-app.log',
// Your timezone
'timezone' => 'Asia/Jakarta',
// Log level
'level' => 'debug',
// List of Monolog Handlers you wanna use
'handlers' => [],
];
return new \Projek\Slim\Monolog('slim-app', $settings);
};
// Define a log middleware
$app->add(function ($req, $res, $next) {
$return = $next($req, $res);
$this->logger->info('Something happen');
return $return;
});
// Run app
$app->run();
NOTE:
- If you are using option 1 please make sure you already have
$container['settings']['logger']
in your configuration file. $settings['filename']
only required if you have$settings['directory']
- Set
$settings['directory']
tosyslog
to use System Log.
Please see CONTRIBUTING and CONDUCT for details.
This library is open-sourced software licensed under MIT license.