cheeesus cheeesus - 2 years ago 146
PHP Question

Get unique worker/thread/process/request ID in PHP

In multi-threaded environments (like most web platforms) I often include some sort of thread ID to the logs of my apps. This enables me to tell exactly what log entry came from which request/thread, when there are multiple requests at once which are simultaneously writing to the same log.

In .NET/C#, this can be done by the formatters of log4net, which by default include the current thread's

(a number) or
(a given name). These properties uniquely identify a thread (see for example: How to log correct context with Threadpool threads using log4net?

In PHP, I have not found anything similar (I asked Google, PHP docs and SO). Does it exist?

Answer Source


int zend_thread_id ( void ) 

This function returns a unique identifier for the current thread.


This function is only available if PHP has been built with ZTS (Zend Thread Safety) support and debug mode (--enable-debug).

You could also try yo call mysql_thread_id(), when you use that API for your database access (or mysqli::$thread_id when using mysqli).

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download