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?

【PHPフレームワークFlow】デフォルト以外のLogグループを使う

0
Posted at

はじめに

Flowでログ出力する際、デフォルトで出力されるSystemLogのファイル以外に出力できないかを考えてみました。
新しくLoggerを作成すればできそうなので、試していきます。

デフォルト以外のログを出したい

Flowはデフォルトで用意されている4つのログがあります。

  • SystemLogger
  • SecurityLogger
  • SqlLogger
  • I18nLogger

実装者がログ出力する際はSystemLoggerを使ってログ出力するのが常ですが、一部だけログファイルを分けたい場合などは上記とは別でLoggerを定義する必要があります。

実装する

というわけで実装しましょう。

Settings.yaml

まずはSettings.yamlにLoggerを定義しましょう。
使用するLogBackend、出力するファイル名などの情報を書いておきます

Neos:
  Flow:
    log:
      psr3:
        'Neos\Flow\Log\PsrLoggerFactory':
          customLogger:
            default:
              class: Neos\Welcome\Log\Backend\OtelJsonFileBackend
              options:
                logFileURL: '%FLOW_PATH_DATA%Logs/Custom.log'
                createParentDirectories: true
                severityThreshold: '%LOG_INFO%'
                maximumLogFileSize: 10485760
                logFilesToKeep: 1
                logMessageOrigin: false

Objects.yaml

Objects.yamlにも記載をします。
LoggerをDIする際にNeos.Flow:CustomLoggerという引数だったらこのクラスをInjectするという定義です。
利用側のコードは後述します。

'Neos.Flow:CustomLogger':
  className: Psr\Log\LoggerInterface
  scope: singleton
  factoryObjectName: Neos\Flow\Log\PsrLoggerFactoryInterface
  factoryMethodName: get
  arguments:
    1:
      value: customLogger

ログ出力側

最後にログ出力側のコードです。
クラスの変数$customLogger@Flow\Inject(name="Neos.Flow:CustomLogger")定義したLoggerをInjectします。
あとは普通にログ出力するだけです。

<?php
namespace Neos\Welcome\Controller;

use Neos\Flow\Annotations as Flow;
use Neos\Flow\Mvc\Controller\ActionController;

class ItemController extends ActionController
{

    /**
     * 定義したLoggerをInjectする
     * 
     * @Flow\Inject(name="Neos.Flow:CustomLogger")
     * @var LoggerInterface
     */
     protected $customLogger;

    public function logAction()
    {
        // カスタムログ出力
        $this->customLogger->info("test");

        // レスポンス返す
        $this->view->assign('value', array(
            "item" => 'test',
        ));
    }
}

(上のコードは分かりやすくするために必要なコードなどをかなり省いています。注意です。)

動作確認

実際に試したところ無事にログが出力されました!

root@2f5a2072b5ff:/app/Data/Logs# ls
Custom.json  Query_Development.log  System_Development.log
root@2f5a2072b5ff:/app/Data/Logs# cat Custom.json
{"timestamp":"2026-08-02T02:02:04+00:00","processId":40,"severity":"info","remoteIp":"","message":"test","origin":{"packageKey":null,"className":null,"methodName":null},"traceId":"5e0ed5958257340ce2c83b93e15eb6c2","additionalData":[]}
{"timestamp":"2026-08-02T02:02:05+00:00","processId":40,"severity":"info","remoteIp":"","message":"test","origin":{"packageKey":null,"className":null,"methodName":null},"traceId":"b20f90ee9e50f82cac71c8d02c2d578b","additionalData":[]}
root@2f5a2072b5ff:/app/Data/Logs#

おわりに

今回はデフォルト以外のLoggerを定義してみました。
同じログ出力でも特定の場合だけ他のファイルに出力したといった場合に役立ちそうです。

ここまでご覧いただきありがとうございました!

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?