Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP internals - what error logging function to use?

Tags:

c

php

I'm writing a PHP extension (although its starting to look a lot like a zend extension). There are instances where it needs to write to a logging interface.

I see that zend_error() is being used elsewhere however:

  • reading the source code on github I found zend_error declared in zend/zend.h but I couldn't find the corresponding definition of the function

  • looking at the contexts in which zend_error is used, I suspect that calls will be rerouted/diverted by set_error_handler

Normally, the logging will happen in MINIT and MSHUTDOWN (where presumably an error handler defined by a script cannot have any influence) but there may be times in between as well - and I'm looking for some consistency. Hence trying to understand how the mechanism works.

Is it safe to use zend_error() in MINIT/MSHUTDOWN?

How do I ensure that I am always calling the the default error handler?

like image 733
symcbean Avatar asked Sep 04 '18 15:09

symcbean


People also ask

How do I log errors in PHP?

To log errors in PHP, open the php. ini file and uncomment/add the following lines of code. If you want to enable PHP error logging in individual files, add this code at the top of the PHP file. ini_set('display_errors', 1); ini_set('display_startup_errors', 1); error_reporting(E_ALL);

What are error logging functions?

Advertisements. These are functions dealing with error handling and logging. They allow you to define your own error handling rules, as well as modify the way the errors can be logged. This allows you to change and enhance error reporting to suit your needs.

How can we log error messages to an email in PHP?

PHP error_log() Function The error_log() function sends an error message to a log, to a file, or to a mail account.


1 Answers

Well, I found this in the PHP Wiki. I think this is old, but I imagine it still applies to Zend3:

Don't use zend_error

zend_error() should only be used inside the engine. Inside PHP extensions only PHP's error functions shoudl be used. Typically php_error_docref() is the best choice. php_error_docref() will extend the error message by extra information, like the current function name and properly escape output where needed. zend_error() is used by the Zend Engine due to PHP's modular architecture where the Zend Engine and TSRM should be compilable without other parts of PHP. The engine therefore can not use PHP level APIs. This limitation does not exist in extensions.

What this says to me is that zend_error was written for use in the engine and not designed as one of the tools for use in building extensions. For that reason, you are unlikely to find documentation covering the details you are asking about, and, even if it DOES work reliably for you, it may not continue to do so.

like image 111
wordragon Avatar answered Oct 24 '22 04:10

wordragon