![]() |
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 app\Config\Services.php PHP Code: <?php
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 )
02-22-2024, 06:55 AM
(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)
@sushiguru Your code seems to work.
I don't know why it does not work.
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'); } }
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.
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.
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.
|
Welcome Guest, Not a member yet? Register Sign In |