Welcome Guest, Not a member yet? Register   Sign In
Extending core logger service
#1

I'm trying to extend the core logger class to send myself an email on critical errors. So far I have the following which is not working, and I'm not sure why...
My logger class lives in the app\Libraries folder. Ignore the fact that the email call is commented out - at this point the proof of concept of my class extending the core class would be any 'info' log would result in my hard-coded test, but that's not happening, making me think that my extension to the class is being ignored.
My Logger class:
PHP Code:
<?php

namespace App\Libraries;

use 
CodeIgniter\Log\Logger as BaseLogger;

class 
VS_Logger extends BaseLogger {

    public function info($message, array $context = []): bool
    
{
//        $this->email_error('info', $message, $context);
//        return $this->log('info', $message, $context);
        return $this->log('info''My class');
    }

    private function email_error($level$message$context){
        $vs = new VS();
        $subject "My App Log: $level";
        $body $message "\n\r" json_encode$context );
        $vs->sendgrid($subject$body'[email protected]');
    }



app\Config\Services.php
PHP Code:
<?php

namespace Config;

use 
App\Libraries\VS_Logger;
use 
CodeIgniter\Config\BaseService;
use 
Config\Logger as LoggerConfig;

class 
Services extends BaseService
{
    public static function logger($getShared true)
    {
        if($getShared){
            return static::getSharedInstance('logger');
        }

        return new VS_Logger(config(LoggerConfig::class));
    }

Reply
#2

Where's the Logger Interface and handlers?

See:
CodeIgniter User Guide - Creating Core System Classes
Also look at the system/Log/Logger.php
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#3

(This post was last modified: 02-22-2024, 08:27 AM by sushiguru. Edit Reason: Added clarification )

Sorry - I don't understand the question...

My understanding of the docs was that, provided the service was loaded in app/services, and the new class was extending the base class, then all methods are available and anything with a matching name replaces the base class method.

(I perhaps wasn't clear enough - this is extending the base logger class, not replacing it)
Reply
#4

@sushiguru Your code seems to work.
I don't know why it does not work.
Reply
#5

Which is why this post exists! On paper it _should_ work...
Reply
#6

(This post was last modified: 02-23-2024, 04:56 AM by kenjis.)

I got it.

See the log_message() code:
https://github.com/codeigniter4/CodeIgni...n.php#L796

It uses only the log() method.

<?php

namespace App\Libraries;

use CodeIgniter\Log\Logger as BaseLogger;

class VS_Logger extends BaseLogger
{
    public function log($level, $message, array $context = []): bool
    {
        return parent::log($level, 'My class');
    }
}
Reply
#7

Hmm - still not sure I'm understanding this one. By adding the log function you suggest, yes, my class is now doing the logging BUT the parent functions I was looking to extend are still doing nothing. I think I can see a way to work around this, but it still isn't quite behaving as documented.
Reply
#8

If you use log_message() function, (probably you use it), only Logger::log() method is used.
So if you extend Logger::info(), it will not be called at all.
Therefor you must extend Logger::log() method.
Reply
#9

Riiiiight - got you; yes, I'm generally not using $this->logger->info, etc - just log_message('info', 'message'), etc. Good to know, and this is now doing what I need it to do.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB