[Production][Logging] Get lower log level messages in case an exceptions was raised

Hi :slight_smile:
Simetimes a user is able to produce an excpetion. However, that happens in production and is hard to retrace.

I have EmailTarget enabled which is for errors only. Exists a way to include ALL logged events in order to that email? The normal FileTarget only includes error and warnings, no info and no debug.

Or even better: Some systems generate a “number” which holds more information about the occured error. Would be great if I were able to look into the debug-console in the case when an exceptions was raised.

Any ideas?

  1. Take a look at https://sentry.io/ and https://rollbar.com/.
  2. Yes, it’s possible to set whatever error level to a log target. See https://www.yiiframework.com/doc/api/2.0/yii-log-target#$levels-detail

Thanks to @CeBe who had a nice idea for this. One could simply overwrite the EmailTarget::export() function to check each messages level. If at least one message matches my criteria, the parent export will be called. Otherwise the messages will be discarded.

Note, that all/none levels must be configured then.

components/EmailTarget

<?php


namespace common\components;


use yii\log\Logger;

class EmailTarget extends \yii\log\EmailTarget {

	public function export() {

		foreach ( $this->messages as $message ) {
			if ( $message[1] <= Logger::LEVEL_ERROR ) {
				return parent::export();
			}
		}
		return false;
	}

}

The needed target:

'email' => [
	 'class' => EmailTarget::class, //use common\components\EmailTarget;
	 'message' => [
	 'to' => [ 'mail@example.tld' ]
	 ],
 ]

:slight_smile:

1 Like