CodeIgniter Forums
Extending core logger service - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: CodeIgniter 4 (https://forum.codeigniter.com/forumdisplay.php?fid=28)
+--- Forum: CodeIgniter 4 Support (https://forum.codeigniter.com/forumdisplay.php?fid=30)
+--- Thread: Extending core logger service (/showthread.php?tid=90207)



Extending core logger service - sushiguru - 02-22-2024

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));
    }




RE: Extending core logger service - InsiteFX - 02-22-2024

Where's the Logger Interface and handlers?

See:
CodeIgniter User Guide - Creating Core System Classes
Also look at the system/Log/Logger.php


RE: Extending core logger service - sushiguru - 02-22-2024

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)


RE: Extending core logger service - kenjis - 02-22-2024

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


RE: Extending core logger service - sushiguru - 02-23-2024

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


RE: Extending core logger service - kenjis - 02-23-2024

I got it.

See the log_message() code:
https://github.com/codeigniter4/CodeIgniter4/blob/65c6a61d4527acf86882e16430943624558fb1b3/system/Common.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');
    }
}



RE: Extending core logger service - sushiguru - 02-23-2024

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.


RE: Extending core logger service - kenjis - 02-23-2024

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.


RE: Extending core logger service - sushiguru - 02-23-2024

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.