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:

Thank you.
Is there an option to configure the contents of the subject of the message?
Eg. include the error message in the subject (eg. 404 Not found).

Since there’s setSubject() in Laminas/Mail/Message I’m taking an educated guess here and say that you could add a ‘subject’ => ‘thing’ beneath ‘subject_prepend_text’:

$logger = new Laminas\Log\Logger(
    [
        'writers' => [
            [
                'name'     => Laminas\Log\Writer\Mail::class,
                'priority' => Laminas\Log\Logger::INFO,
                'options'  => [
                    'subject_prepend_text' => 'Log: ',
                    'subject' => 'thing' //like this
                    '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');

It’s worth noting that I haven’t used that component yet, that’s about what I can do to help you, sorry.