0

I added Monolog v.1.24.0 to my project.

Create a logger:

$logger = new Logger('db');
$logger->setTimezone(DateTime::getTimezone()); 
$logger->pushHandler(new StreamHandler(ROOT.'/log/db.log', Logger::DEBUG, 600));

Now at some point later in the code I construct a message and send it to browser:

// $result contains my array
$tmp = \json_encode($result, JSON_UNESCAPED_UNICODE | JSON_PRESERVE_ZERO_FRACTION | JSON_UNESCAPED_SLASHES);
echo $tmp;
$logger->notice($tmp);

My problem is that I want to extract the time info contained in the log stored at db.log and include to that echoed to the user.

How is that possible?

PS: I can construct the time by myself but then it's better to delete the whole Monolog project and use my own!

centurian
  • 1,168
  • 13
  • 25

1 Answers1

1

You are trying to use monolog and generally a PSR-3 logger for something that it is not meant to be used in this way...

What happens internally in monolog or more specifically in this case to a PSR-3 client is out of scope of what you are trying to achieve. Most probably your business domain says that you need to store a record for an action that the user did.. Then do so.. It does not say that you want to log it or if it does say that you need to log it these two should not be mixed.

Moreover you shouldn't be mixing what monolog internally does with what you need to display to the user.

So in the end the solution would be:

// $result contains my array
$tmp = \json_encode($result, JSON_UNESCAPED_UNICODE | JSON_PRESERVE_ZERO_FRACTION | JSON_UNESCAPED_SLASHES);
echo $tmp;

//
// write here custom code that saves my $msg where ever I need to and that it contains also the timestamp
//

// Then come here and log that my record is saved..
$logger->notice('Record is saved');

You have to understand that logger is an abstract layer for LOGGING.. You are violating the Liskov Principle of SOLID... What will happen if in the future you remove the DB/Stream Handler from your monolog? And replace it with something else that does not have timestamp.

Your code will fail.

gmponos
  • 2,157
  • 4
  • 22
  • 33
  • what I try to achieve is to log DDL commands from a DB manipulation web interface. So, I thought why not store sth like "db copied, 4 create commands(1 failed), 4 insert commands(1 failed) etc." to a specific log file like "db.log" – centurian Dec 19 '18 at 13:00
  • You can do that.. but that's not the place where you will get the timestamp from. – gmponos Dec 19 '18 at 13:05
  • No.. I am not saying this.. Use monolog.. it is a great library for logging.. but the time that the logs is stored.. has nothing to do with the time that DB records are saved. – gmponos Dec 19 '18 at 13:07
  • Also if you intent to use PSR-3 interfaces you can never be sure that a timestamp for logs always exists. – gmponos Dec 19 '18 at 13:09
  • It's a mini php framework 7 & I'm trying to avoid complexity. Mainly it writes to user's home-even for logging. Thanks for your effort! – centurian Dec 19 '18 at 13:12