Seldaek / monolog

Sends your logs to files, sockets, inboxes, databases and various web services
https://seldaek.github.io/monolog/
MIT License
21.01k stars 1.9k forks source link

cannot access private method #1866

Closed buexplain closed 6 months ago

buexplain commented 9 months ago

Monolog version 3

PHP Warning:  Invalid callback Monolog\Handler\RotatingFileHandler::customErrorHandler, cannot access private method Monolog\Handler\RotatingFileHandler::customErrorHandler()
Seldaek commented 6 months ago

This is weird.. I can't reproduce it (https://3v4l.org/ZHA87) and I don't really see why it would do this as the callback is only used within the context of StreamHandler /cc @corentin-cres

lcharette commented 6 months ago

I was able to replicate this UserFrosting by removing write permission to the log file, which resulted in a similar error message :

Fatal error: Uncaught Error: Invalid callback Monolog\Handler\StreamHandler::customErrorHandler, cannot access private method Monolog\Handler\StreamHandler::customErrorHandler()

Using the example above (https://3v4l.org/ZHA87) on the same environment and on the same file (without write access) returned the proper output :

string(40) "Failed to open stream: Permission denied" string(9) "Test test" 

The difference is our app use a custom StreamWrapper. So the core PHP fopen isn't actually called by Monolog's StreamHandler, but our own. And both will invoke customErrorHandler, which won't work on our side since the method is private. Therefore, customErrorHandler must be public.

Note the actual process when customErrorHandler is public is similar to this :

  1. Monolog StreamHandler call fopen
  2. Our own StreamHandler calls fopen
  3. fopen trows an exception
  4. Our StreamHandler invoke customErrorHandler (even if we use @fopen), then do the rest of it's job and return false.
  5. Monolog invoke customErrorHandler again
lcharette commented 6 months ago

Here's a way to replicate : https://3v4l.org/PjtpI

(Forget the open_basedir restriction in effect error... that means it's working, simply I can't replicate a non-writable file on the editor)

There we go. Both should return "Failed to open stream" : https://3v4l.org/LsBph

Seldaek commented 6 months ago

Great, thanks for the investigative work there! I guess maybe using a closure instead would be better, otherwise public method with an internal phpdoc.. Anyway I'll look at this next week

Seldaek commented 6 months ago

@lcharette can you please check using dev-main if this problem is fixed for you?

lcharette commented 6 months ago

I confirm problem is fixed with dev-main. Thanks !

Seldaek commented 6 months ago

Great

interactiveRob commented 4 weeks ago

just in case anyone else from Userfrosting winds up here and is wondering which log file @lcharette is talking about above. --

That would be the /logs/userfrosting.log file. In my case, the app did not have write permission on the log which triggered the same private method issue discussed here.