slimでログを出力

前回インストールしたslim/extrasを利用してログ出力を行う設定のメモ

ログディレクトリを作成

$ mkdir -p logs
$ chmod 777 logs

index.phpを編集

$app = new Slim(array(
            'view' => new Twig,
            'templates.path' => '../templates',
            'log.writer' => new \Slim\Extras\Log\DateTimeFileWriter(array(
                    'path' => '../logs',
                    'name_format' => 'Y-m-d',
                    'message_format' => '%label% - %date% - %message%'
                    ))
                ));

$app->get('/', function() use ($app) {
    $app->getLog()->debug('log'); // - level 4
    $app->getLog()->info('log');  // - level 3
    $app->getLog()->warn('log');  // - level 2
    $app->getLog()->error('log'); // - level 1
    $app->getLog()->fatal('log'); // - level 0

    $app->render('index.html.twig', array('name' => 'taka512'));
  });

index.phpにアクセスした後にログを見てみると出力されていることが確認できます。

$ tail -f logs/2013-06-29.log
DEBUG - 2013-06-29T16:56:47+09:00 - log
INFO - 2013-06-29T16:56:47+09:00 - log
WARN - 2013-06-29T16:56:47+09:00 - log
ERROR - 2013-06-29T16:56:47+09:00 - log
FATAL - 2013-06-29T16:56:47+09:00 - log

今回の作業をした後のディレクトリ構造はこんな感じとなります。

|-- composer.json
|-- composer.lock
|-- logs
|    |-- 2013-06-29.log
|-- templates
|    |-- index.html.twig
|-- vendor
|-- web
     |-- index.php

参考
https://github.com/codeguy/Slim-Extras#log-writers