了解过PHP内核的同学都知道,PHP的一次请求的生命周期

1.启动Apache后,PHP解释程序也随之启动。PHP调用各个扩展的MINIT方法,从而使这些扩展切换到可用状态

2.当一个页面请求发生时,SAPI层将控制权交给PHP层。于是PHP设置了用于回复本次请求所需的环境变量。同时,它还建立一个变量表,用来存放执行过程 中产生的变量名和值。PHP调用各个模块的RINIT方法,即“请求初始化”。RINIT方法可以看作是一个准备过程, 在程序执行之间就会自动启动

3.如同PHP启动一样,PHP的关闭也分两步。一旦页面执行完毕(无论是执行到了文件末尾还是用exit或die函数中止),PHP就会启动清理程序。它会按顺序调用各个模块的RSHUTDOWN方法。 RSHUTDOWN用以清除程序运行时产生的符号表,也就是对每个变量调用unset函数。

4.最后,所有的请求都已处理完毕,SAPI也准备关闭了,PHP开始执行第二步:PHP调用每个扩展的MSHUTDOWN方法,这是各个模块最后一次释放内存的机会

以上是运行在Apache服务中,而Nginx+FastCgi似乎不是这样,这一点我会后期深究,今天主要探讨日志功能,便也后期学习


首先我们按照我的这篇博文(在Linux下编写php扩展)生成一个扩展

然后我们在myext.c文件中添加一个日志函数,然后分别在

PHP_MINIT_FUNCTION,PHP_MSHUTDOWN_FUNCTION,PHP_RINIT_FUNCTION,PHP_RSHUTDOWN_FUNCTION四个函数中添加调用日志的函数

然后编译扩展,重新服务,允许等操作,看看是否有日志

下面我给出一个完整的文件

  1 /*
  2   +----------------------------------------------------------------------+
  3   | PHP Version 5                                                        |
  4   +----------------------------------------------------------------------+
  5   | Copyright (c) 1997-2016 The PHP Group                                |
  6   +----------------------------------------------------------------------+
  7   | This source file is subject to version 3.01 of the PHP license,      |
  8   | that is bundled with this package in the file LICENSE, and is        |
  9   | available through the world-wide-web at the following url:           |
 10   | http://www.php.net/license/3_01.txt                                  |
 11   | If you did not receive a copy of the PHP license and are unable to   |
 12   | obtain it through the world-wide-web, please send a note to          |
 13   | license@php.net so we can mail you a copy immediately.               |
 14   +----------------------------------------------------------------------+
 15   | Author:                                                              |
 16   +----------------------------------------------------------------------+
 17 */
 18
 19 /* $Id$ */
 20
 21 #ifdef HAVE_CONFIG_H
 22 #include "config.h"
 23 #endif
 24
 25 #include "php.h"
 26 #include "php_ini.h"
 27 #include "ext/standard/info.h"
 28 #include "php_myext.h"
 29
 30 /* If you declare any globals in php_myext.h uncomment this:
 31 ZEND_DECLARE_MODULE_GLOBALS(myext)
 32 */
 33
 34 /* True global resources - no need for thread safety here */
 35 static int le_myext;
 36 void save_log();
 37 /* {{{ PHP_INI
 38  */
 39 /* Remove comments and fill if you need to have entries in php.ini
 40 PHP_INI_BEGIN()
 41     STD_PHP_INI_ENTRY("myext.global_value",      "42", PHP_INI_ALL, OnUpdateLong, global_value, zend_myext_globals, myext_globals)
 42     STD_PHP_INI_ENTRY("myext.global_string", "foobar", PHP_INI_ALL, OnUpdateString, global_string, zend_myext_globals, myext_globals)
 43 PHP_INI_END()
 44 */
 45 /* }}} */
 46
 47 /* Remove the following function when you have successfully modified config.m4
 48    so that your module can be compiled into PHP, it exists only for testing
 49    purposes. */
 50
 51 /* Every user-visible function in PHP should document itself in the source */
 52 /* {{{ proto string confirm_myext_compiled(string arg)
 53    Return a string to confirm that the module is compiled in */
 54 PHP_FUNCTION(confirm_myext_compiled)
 55 {
 56     char *arg = NULL;
 57     int arg_len, len;
 58     char *strg;
 59
 60     if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &arg, &arg_len) == FAILURE) {
 61         return;
 62     }
 63
 64     len = spprintf(&strg, 0, "Congratulations! You have successfully modified ext/%.78s/config.m4. Module %.78s is now compiled into PHP.", "myext", arg);
 65     RETURN_STRINGL(strg, len, 0);
 66 }
 67
 68 void save_log(char *str)
 69 {
 70     time_t rawtime;
 71     struct tm * timeinfo;
 72     time ( &rawtime );
 73     timeinfo = localtime ( &rawtime );
 74
 75     char buf[512];
 76     sprintf(buf, "%s", asctime (timeinfo));
 77
 78
 79     FILE *fp = fopen("/Users/zongshuai/log/c.log", "a+");
 80     ////这里写一个绝对路径,请改成一下,大家还得注意权限问题,如果发现打印不出内容,检查一下权限
 81     if (fp==0) {
 82         printf("can't open file\n");
 83         return;
 84     }
 85
 86      strcat(str,"\r\n");
 87
 88     fseek(fp, 0,SEEK_END);
 89     fwrite(buf, strlen(buf) - 1, 1, fp);
 90     fwrite("    ", 4, 1, fp);
 91     fwrite(str, strlen(str) * sizeof(char), 1, fp);
 92     fclose(fp);
 93     return;
 94 }
 95
 96 PHP_FUNCTION(sum_two_num)
 97 {
 98     long x,y,z;
 99
100     if(zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ll", &x,&y) == FAILURE) {
101         RETURN_FALSE;
102     }
103
104     char str[80] = "EXECED MYFUNCTION";
105     save_log(str);
106
107     z = x * y;
108
109     RETURN_LONG(z);
110 }
111 /* }}} */
112 /* The previous line is meant for vim and emacs, so it can correctly fold and
113    unfold functions in source code. See the corresponding marks just before
114    function definition, where the functions purpose is also documented. Please
115    follow this convention for the convenience of others editing your code.
116 */
117
118
119 /* {{{ php_myext_init_globals
120  */
121 /* Uncomment this function if you have INI entries
122 static void php_myext_init_globals(zend_myext_globals *myext_globals)
123 {
124     myext_globals->global_value = 0;
125     myext_globals->global_string = NULL;
126 }
127 */
128 /* }}} */
129
130 /* {{{ PHP_MINIT_FUNCTION
131  */
132 PHP_MINIT_FUNCTION(myext)
133 {
134     /* If you have INI entries, uncomment these lines
135     REGISTER_INI_ENTRIES();
136     */
137     char str[80] = "EXECED PHP_MINIT_FUNCTION";
138     save_log(str);
139     return SUCCESS;
140 }
141 /* }}} */
142
143 /* {{{ PHP_MSHUTDOWN_FUNCTION
144  */
145 PHP_MSHUTDOWN_FUNCTION(myext)
146 {
147     /* uncomment this line if you have INI entries
148     UNREGISTER_INI_ENTRIES();
149     */
150     char str[80] = "EXECED PHP_MSHUTDOWN_FUNCTION";
151     save_log(str);
152     return SUCCESS;
153 }
154 /* }}} */
155
156 /* Remove if there's nothing to do at request start */
157 /* {{{ PHP_RINIT_FUNCTION
158  */
159 PHP_RINIT_FUNCTION(myext)
160 {
161     char str[80] = "EXECED PHP_RINIT_FUNCTION";
162     save_log(str);
163     return SUCCESS;
164 }
165 /* }}} */
166
167 /* Remove if there's nothing to do at request end */
168 /* {{{ PHP_RSHUTDOWN_FUNCTION
169  */
170 PHP_RSHUTDOWN_FUNCTION(myext)
171 {
172     char str[80] = "EXECED PHP_RSHUTDOWN_FUNCTION";
173     save_log(str);
174     return SUCCESS;
175 }
176 /* }}} */
177
178 /* {{{ PHP_MINFO_FUNCTION
179  */
180 PHP_MINFO_FUNCTION(myext)
181 {
182     php_info_print_table_start();
183     php_info_print_table_header(2, "myext support", "enabled");
184     php_info_print_table_end();
185
186     /* Remove comments if you have entries in php.ini
187     DISPLAY_INI_ENTRIES();
188     */
189 }
190 /* }}} */
191
192 /* {{{ myext_functions[]
193  *
194  * Every user visible function must have an entry in myext_functions[].
195  */
196 const zend_function_entry myext_functions[] = {
197     PHP_FE(confirm_myext_compiled,    NULL)        /* For testing, remove later. */
198     PHP_FE(sum_two_num,    NULL)
199     PHP_FE_END    /* Must be the last line in myext_functions[] */
200 };
201 /* }}} */
202
203 /* {{{ myext_module_entry
204  */
205 zend_module_entry myext_module_entry = {
206     STANDARD_MODULE_HEADER,
207     "myext",
208     myext_functions,
209     PHP_MINIT(myext),
210     PHP_MSHUTDOWN(myext),
211     PHP_RINIT(myext),        /* Replace with NULL if there's nothing to do at request start */
212     PHP_RSHUTDOWN(myext),    /* Replace with NULL if there's nothing to do at request end */
213     PHP_MINFO(myext),
214     PHP_myext_VERSION,
215     STANDARD_MODULE_PROPERTIES
216 };
217 /* }}} */
218
219 #ifdef COMPILE_DL_myext
220 ZEND_GET_MODULE(myext)
221 #endif
222
223 /*
224  * Local variables:
225  * tab-width: 4
226  * c-basic-offset: 4
227  * End:
228  * vim600: noet sw=4 ts=4 fdm=marker
229  * vim<600: noet sw=4 ts=4
230  */

转载于:https://www.cnblogs.com/xiaozong/p/5823380.html

使用日志记录功能查看PHP扩展的执行过程相关推荐

  1. 如何自行给指定的SAP OData服务添加自定义日志记录功能

    有的时候,SAP标准的OData实现或者相关的工具没有提供我们想记录的日志功能,此时可以利用SAP系统强大的扩展特性,进行自定义日志功能的二次开发. 以SAP CRM Fiori应用"My ...

  2. HAproxy增加日志记录功能和自定义日志输出内容、格式

    http://blog.51cto.com/eric1/1854574 一.增加haproxy日志记录功能   1.1 由于数据分析的需要,我们必须打开haproxy日志,记录相关信息. 在配置前,我 ...

  3. 文件服务器导出共享文件列表,服务器共享文件访问记录方法、共享文件操作日志记录功能实现...

    现在很多单位都有共享文件服务器,经常会在文件共享服务器上存储单位一些重要的共享文件,让局域网用户访问使用,为了保护共享文件的安全,管理员常常需要设置共享文件访问权限,同时还需要记录共享文件访问日志,如 ...

  4. 服务器文件管理流程,服务器共享文件管理和共享文件操作日志记录功能的实现...

    现在很多单位都有共享文件服务器,经常会在文件共享服务器上存储单位一些重要的共享文件,让局域网用户访问使用,为了保护共享文件的安全,管理员常常需要设置共享文件访问权限,同时还需要记录共享文件访问日志,如 ...

  5. Xshell 7设置及使用日志记录功能

    设置日志记录功能 右击已建立的会话,选择底部属性,点击左下角日志记录,进入日志记录功能设置对话窗.一般按照如下图所示设置即可. 使用日志记录功能 菜单栏"文件-日志-启动/停止", ...

  6. php实现项目的日志记录功能,tp5框架使用composer实现日志记录功能示例

    本文实例讲述了tp5框架使用composer实现日志记录功能.分享给大家供大家参考,具体如下: tp5实现日志记录 1.安装 psr/log composer require psr/log 它的作用 ...

  7. 配置Haproxy增加日志记录功能

    2019独角兽企业重金招聘Python工程师标准>>> CentOS 7上yum安装的Haproxy,默认没有记录日志.需要做一下配置才能记录日志. 1.创建日志文件/var/log ...

  8. java core日志在哪里_java-如何在未启用日志记录功能的情况下在...

    我已使用CXF 3.0.0 Milestone1创建了Rest服务,并且试图在SEND阶段的Out Interceptor中获取HTTP响应的正文,并将其放入String变量中,而未在xml配置文件中 ...

  9. java日志怎么实现_JAVA项目中怎么实现一个通用日志记录功能

    JAVA项目中怎么实现一个通用日志记录功能 发布时间:2020-11-21 17:04:50 来源:亿速云 阅读:53 作者:Leah 今天就跟大家聊聊有关JAVA项目中怎么实现一个通用日志记录功能, ...

最新文章

  1. 字节跳动教育大规模裁员:赔付方式N+2
  2. PHPStudy下Apache SSL证书安装教程
  3. Android NDK: WARNING: APP_PLATFORM android-14 is larger than android:minSdkVersion 8
  4. 属性文件有一个多行值
  5. Sequence Classification with LSTM Recurrent Neural Networks in Python with Keras-学习笔记
  6. 【DevCloud · 敏捷智库】如何进行需求优先级管理?
  7. 中大东校小米路由器mini实现inode上网,ipv6 wifi【中大】【东校】【inode】【ipv6】...
  8. Protel技巧之设计原理图模块化
  9. DFS----深度优先搜索与记忆化数组例题分析
  10. 汽车之家所有车型价格爬虫
  11. Windows邮箱登录QQ邮箱
  12. 【20220318】执行脚本提示killed
  13. matlab 图片雾化代码,雾化效果图片制作代码汇总
  14. mysql桌面型数据库下载_桌面数据库下载v2019.12.001 安装版-西西软件下载
  15. 神经网络的三种训练方法,如何训练一个神经网络
  16. 汇编指令: JO、JNO、JB、JNB、JE、JNE、JBE、JA、JS、JNS、JP、JNP、JL、JNL、JNG、JG、JCXZ、JECXZ、JMP、JMP...
  17. 无法打开到主机的连接。 在端口 23: 连接失败
  18. 共享办公室租赁和传统写字楼之间,如何抉择
  19. 数据预处理中的归一化与反归一化
  20. C语言入门题目2——对于给定字符,输出该字符的前驱字符和后继字符

热门文章

  1. CTFshow php特性 web89
  2. 关闭Visual Studio中的自动代码分析
  3. Simulink仿真 第二节 输入输出和子系统
  4. set和multiset容器
  5. Logistic and Softmax Regression (逻辑回归和Softmax回归)
  6. php el表达式,JSP EL表达式学习
  7. php this 代表什么,php中$this-是什么意义
  8. MPI派生数据类型发送接收,降低发送时间
  9. 无法载入增效工具_作业大队工具车间利用修旧赚“钱”
  10. C++ FAQs 第二版