Mail log writer factory config example

I see that there is an open issune on Github regarding missing configuration documentation for factories.

Can I have and example of how to configure the Zend Log Writer Mail factory to be used with SMTP and Gmail?

Here is my config file using only the stream writer now.

 'psr_log' => [                   // <-- NOTE: key change! it's not 'log'
        'default' => [
            'writers' => [
                'stream' => [
                    'name' => 'stream',
                    'priority' => 1,
                    'options' => [
                        'stream' => 'data/log/application.log',
                        'formatter' => [
                            'name' => Simple::class,
                            'options' => [
                                'format' => '%timestamp% %priorityName% (%priority%): %message% %extra%',
                                'dateTimeFormat' => 'c',
                            ],
                        ],
                        'filters' => [
                            'priority' => [
                                'name' => 'priority',
                                'options' => [
                                    'operator' => '<=',
                                    'priority' => Logger::INFO,
                                ],
                            ],
                        ],
                    ],
                ],
            ],
            'processors' => [
                'requestid' => [
                    'name' => RequestId::class,
                ],
            ],
        ],

How do I configure Mail log writer here? (assuming I don’t wan’t to instantitate objects right away).

Hello and welcome to our forums! :smiley:

Here is an example for a writer with mail and SMTP transport:

$logger = new Laminas\Log\Logger(
    [
        'writers' => [
            [
                'name'     => Laminas\Log\Writer\Mail::class,
                'priority' => Laminas\Log\Logger::INFO,
                'options'  => [
                    'subject_prepend_text' => 'Log: ',
                    'mail'                 => [
                        'from' => 'from@example.com',
                        'to'   => 'to@example.com',
                    ],
                    'transport'            => [
                        'type'    => Laminas\Mail\Transport\Smtp::class,
                        'options' => [
                            'host'              => '…',
                            'connection_class'  => Laminas\Mail\Protocol\Smtp\Auth\Plain::class,
                            'connection_config' => [
                                'username' => '…',
                                'password' => '…',
                            ],
                        ],
                    ],
                ],
            ],
        ],
    ]
);

$logger->info('Test');

The mail writer of laminas-log uses Laminas\Mail\MessageFactory to create the message and Laminas\Mail\Transport\Factory to create the transport.
The option keys are translated to method names which means subject_prepend_text is mapped to setSubjectPrependText and from to setFrom. (See available methods of Laminas\Mail\Message and Laminas\Mail\Transport\SmtpOptions.)

References: