0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

LaminasのServiceManagerを使って、ログをDBに書き出す

Last updated at Posted at 2021-05-30

ServiceManagerを使うやり方で、設定方法にハマッタのでメモ。
MySQLの場合,DateTimeのフォーマットがLaminas側のフォーマットと違うので、
fromatterで変更しておく必要があります。

接続できているDBに、記録用テーブルを作っておく。

table
create table ta00_logger
(
timestamp timestamp,
priority int,
priorityName varchar(50),
message varchar(200)
)

コントローラに、Loggerを注入するコードを書いておく。

Module.php
public function getControllerConfig(): array
    {
        return [
	'factories' => [
                Controller\HomeController::class => function ($container) {
                    return new Controller\HomeController(
                        $container->get("MyLogger"),//<--loggerの注入
                        $container->get(AdapterInterface::class)
                    );
                },
...

これをやっおくと、Controller側からloggerを使える。

HomeConstoller.php
...
public function __constructer($logger, $adapter)
{
	$logger->info(get_class()." Called");
	...
}
...

MyLoggerサービス用のパラメータ

modules.config.php
    'log' =>[
        'MyLogger'=>[
            'writers' =>[
                'db'=>[
                    'name'=>'db',
                    'options'=>[
                        'db'=>\Laminas\Db\Adapter\AdapterInterface::class,
                        'table'=>'ta00_logger',
                        'formatter'=>[
                            'name' => \Laminas\Log\Formatter\Base::class,
                            'options' => [
                                'dateTimeFormat' => 'Y-m-d H:i:s',//<-MySQLフォーマット
                            ],
                        ]
                    ]
                ]
            ]
        ]
    ],
...
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?