?
avatar olleharstedt
olleharstedt
20 Oct 2017

Lines 221 and 159 in file libraries/src/Log/Logger/FormattedtextLogger.php throws exception if it can't log. Problem is, I do a lot of logging in my payment software and a bunch of customers ended up without their product due to this exception, since the process halted in the middle. Can we discuss the correct behaviour here? An email send to admin is a better solution for our system. A possible exception is simply unacceptable, unless I have to make a wrapper class which catches any possible exception from JLog.

avatar olleharstedt olleharstedt - open - 20 Oct 2017
avatar joomla-cms-bot joomla-cms-bot - change - 20 Oct 2017
Labels Added: ?
avatar joomla-cms-bot joomla-cms-bot - labeled - 20 Oct 2017
avatar olleharstedt olleharstedt - change - 20 Oct 2017
The description was changed
avatar olleharstedt olleharstedt - edited - 20 Oct 2017
avatar olleharstedt olleharstedt - change - 20 Oct 2017
The description was changed
avatar olleharstedt olleharstedt - edited - 20 Oct 2017
avatar olleharstedt olleharstedt - change - 20 Oct 2017
The description was changed
avatar olleharstedt olleharstedt - edited - 20 Oct 2017
avatar franz-wohlkoenig franz-wohlkoenig - change - 20 Oct 2017
Category Authentication Feature Request
avatar olleharstedt olleharstedt - change - 20 Oct 2017
The description was changed
avatar olleharstedt olleharstedt - edited - 20 Oct 2017
avatar mbabker
mbabker - comment - 20 Oct 2017

And you can't catch the thrown exception because?

avatar olleharstedt
olleharstedt - comment - 20 Oct 2017

I would have to make a

try {
  JLog::add(...);
} catch (Exception $ex) {
  ...
}

around each JLog statement... Someone suggested making a custom logger and inject instead.

But I'm also a bit skeptical if exception is a desired behaviour at all for a logger. Maybe rather return a boolean? Or make it possible to supply an anonymous function which is run at failure.

avatar olleharstedt olleharstedt - change - 20 Oct 2017
Title
Critical: JLog throws exception if it can't log
Critical: JLog throws exception if it write to file
avatar olleharstedt olleharstedt - edited - 20 Oct 2017
avatar olleharstedt olleharstedt - change - 20 Oct 2017
Title
Critical: JLog throws exception if it write to file
Critical: JLog throws exception if it can't write to file
avatar olleharstedt olleharstedt - edited - 20 Oct 2017
avatar mbabker
mbabker - comment - 20 Oct 2017

Yes throwing exceptions is a correct way of raising an error. Monolog's library also throws exceptions for similar conditions.

Also note that an exception may be thrown within the JLog class itself, not just from within the logger classes, so you are not adding a try/catch simply for the default formatted text handler possibly throwing an exception. In fact, depending on what loggers have been registered, it's possible for the exception to come from another source (i.e. if a database logger has been registered and there is a connection/query failure, or a callback logger has been registered and the callback throws one). So your best bet is going to be catching exceptions when attempting to log a message.

avatar olleharstedt
olleharstedt - comment - 20 Oct 2017

Yes throwing exceptions is a correct way of raising an error.

A return code or boolean is also a valid alternative. Just saying.

avatar olleharstedt olleharstedt - change - 20 Oct 2017
Title
Critical: JLog throws exception if it can't write to file
JLog throws exception if it can't write to file
avatar olleharstedt olleharstedt - edited - 20 Oct 2017
avatar mbabker
mbabker - comment - 20 Oct 2017

I don't think we need to be adding more JError style getError()/setError() methods to our APIs, and changing return values is going to introduce B/C breaks or leave the code in a state where everyone just blissfully ignores the fact there has been an error condition. So design wise, I don't feel there is anything to be changed in our API; it is doing what it needs to do and is in line with PHP practices.

avatar olleharstedt
olleharstedt - comment - 20 Oct 2017

I completely disagree, but not sure how to fix it. You should not have to distrust a log statement. Or at least there should be an option to make it less aggressive.

avatar mbabker
mbabker - comment - 20 Oct 2017

Even with an ignore errors flag, what happens? How is it communicated upwards that there was an error that should be checked (because blissfully ignoring errors is worse than not raising one)? Even in that snippet you linked, json_encode() natively returns a boolean false which would tell anything using that formatter that the encoding process failed, so the only thing that changes is the code did not go through the formatter's internal validation and error handling logic.

You are right that you should not have to distrust a log statement, but you should not change the API so that it blissfully ignores error states. Anything making a database call or a filesystem read/write operation (which many of our logging handlers are doing) should be aware that there is the potential for the operation to fail and to be ready to account for that.

avatar rdeutz
rdeutz - comment - 20 Oct 2017

You can make a custom log class where you can handle errors in the way you think it is appropiate for your requirements. But expecptions are the right way for JLog.

avatar olleharstedt
olleharstedt - comment - 20 Oct 2017

Yii 2 actually suppresses a lot of function calls: https://github.com/yiisoft/yii2/blob/master/framework/log/FileTarget.php#L104

Unless the file doesn't exist. Hm.

avatar olleharstedt olleharstedt - change - 20 Oct 2017
Status New Closed
Closed_Date 0000-00-00 00:00:00 2017-10-20 14:41:34
Closed_By olleharstedt
avatar olleharstedt olleharstedt - close - 20 Oct 2017

Add a Comment

Login with GitHub to post a comment