{"id":169637,"date":"2020-01-21T10:13:40","date_gmt":"2020-01-21T02:13:40","guid":{"rendered":"https:\/\/gulass.cn\/?p=169637"},"modified":"2020-01-13T13:19:26","modified_gmt":"2020-01-13T05:19:26","slug":"writing-log-files","status":"publish","type":"post","link":"https:\/\/gulass.cn\/writing-log-files.html","title":{"rendered":"\u8bba\u52a0\u9501\u5426\uff1a\u7a0b\u5e8f\u65e5\u5fd7"},"content":{"rendered":"\n\n\n
\u5bfc\u8bfb<\/td>\n\u751f\u6d3b\u4e2d\u7684\u65e5\u5fd7\u662f\u8bb0\u5f55\u4f60\u751f\u6d3b\u7684\u70b9\u70b9\u6ef4\u6ef4\uff0c\u8ba9\u5b83\u628a\u4f60\u5185\u5fc3\u7684\u4e16\u754c\u8868\u9732\u51fa\u6765\uff0c\u66f4\u597d\u7684\u8be0\u91ca\u81ea\u5df1\u7684\u5185\u5fc3\u4e16\u754c\uff0c\u800c\u7535\u8111\u91cc\u7684\u65e5\u5fd7\u53ef\u4ee5\u662f\u6709\u4ef7\u503c\u7684\u4fe1\u606f\u5b9d\u5e93\uff0c\u4e5f\u53ef\u4ee5\u662f\u6beb\u65e0\u4ef7\u503c\u7684\u6570\u636e\u6ce5\u6f6d\u3002<\/strong><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n
\u65e5\u5fd7\uff08log\uff09<\/strong><\/span><\/div>\n

\u4e3a\u4e86\u8ba9\u81ea\u5df1\u7684\u601d\u8def\u66f4\u52a0\u6e05\u6670\uff0c\u4e0b\u9762\u6211\u90fd\u4f1a\u79f0\u65e5\u5fd7\u4e3a log\u3002\u56e0\u4e3a\u65e5\u5fd7\u8fd9\u4e2a\u8bcd\u6709\u4e24\u79cd\u542b\u4e49\u3002<\/p>\n

\u65e5\u8bb0\u7684\u53e6\u4e00\u79cd\u8bf4\u6cd5\u3002\u201c\u5fd7\u201d\u5b57\u672c\u8eab\u4e3a\u201c\u8bb0\u5f55\u201d\u7684\u610f\u601d\uff0c\u65e5\u5fd7\u5c31\u4e3a\u6bcf\u65e5\u7684\u8bb0\u5f55\uff08\u901a\u5e38\u662f\u8ddf\u4f5c\u8005\u6709\u5173\u7684\uff09\u3002<\/p>\n

\u670d\u52a1\u5668\u65e5\u5fd7\uff08server log\uff09\uff0c\u8bb0\u5f55\u670d\u52a1\u5668\u7b49\u7535\u8111\u8bbe\u5907\u6216\u8f6f\u4ef6\u7684\u8fd0\u4f5c\u3002<\/p>\n

\u6211\u4eec\u8fd9\u91cc\u8bf4\u7684\u5f53\u7136\u662f\u670d\u52a1\u5668\u65e5\u5fd7\uff0c\u4e5f\u5c31\u662f server log \u3002<\/p>\n

\u5199\u5165 log<\/strong><\/span><\/div>\n

\u4e00\u822c\u5199\u5165 log \u90fd\u4f1a\u9075\u5faa\u4ee5\u4e0b\u6b65\u9aa4\uff1a<\/p>\n

int fd = open(path)\r\nwrite(fd, sign_append)\r\nfclose(fd)\r\n<\/pre>\n

\u89e3\u91ca\u4e00\u4e0b\u4e0a\u9762\u7684\u4ee3\u7801\uff1a<\/p>\n

int fd = open(path) \r\n<\/pre>\n

\u4f1a\u901a\u8fc7\u7cfb\u7edf\u8c03\u7528\u6253\u5f00\u4e00\u4e2a\u6587\u4ef6\u63cf\u8ff0\u7b26\uff0c\u6216\u8005\u5728\u5176\u4ed6\u8bed\u8a00\u4e2d\u4e5f\u53ef\u4ee5\u79f0\u4f5c\u8d44\u6e90\u63cf\u8ff0\u7b26\uff0c\u8d44\u6e90\u7c7b\u578b\uff0c\u6216\u53e5\u67c4\u3002<\/p>\n

write(fd, append = 1)\r\n<\/pre>\n

write \u7cfb\u7edf\u8c03\u7528\uff0c\u5e76\u52a0\u4e0a append \u6807\u5fd7\uff0c\u4f1a\u6267\u884c seek \u548c write \u4e24\u4e2a\u7cfb\u7edf\u8c03\u7528\uff0c\u4f46\u662f\u8fd9\u79cd\u7cfb\u7edf\u8c03\u7528\u662f\u539f\u5b50\u6027\u7684\u3002<\/p>\n

\u539f\u5b50\u6027\u610f\u5473\u7740 seek \u548c write \u4f1a\u540c\u65f6\u6267\u884c\uff0c\u4e0d\u4f1a\u6709\u4e24\u4e2a\u7ebf\u7a0b\u4ea7\u751f\u4ea4\u53c9\uff0c\u5fc5\u987b a \u7ebf\u7a0b\u6267\u884c\u5b8c seek \u548c write \uff0cb \u7ebf\u7a0b\u624d\u80fd\u7ee7\u7eed\u6267\u884c\uff08\u8fd9\u91cc\u8bf4\u7ebf\u7a0b\uff0c\u662f\u56e0\u4e3a\u7ebf\u7a0b\u624d\u662f cpu \u8c03\u5ea6\u7684\u57fa\u672c\u5355\u4f4d\uff09\u3002<\/p>\n

\u6240\u4ee5\u5728 nginx \u4e2d\uff0c\u6211\u4eec\u52a0\u4e0a append \u6807\u5fd7\uff0c\u5c31\u4e0d\u7528\u5bf9\u7ebf\u7a0b\u4e0a\u9501\u4e86\u3002<\/p>\n

fclose(fd)\r\n<\/pre>\n

\u5173\u95ed\u63cf\u8ff0\u7b26\u3002<\/p>\n

linux \u4e00\u822c\u5bf9\u6253\u5f00\u7684\u6587\u4ef6\u63cf\u8ff0\u7b26\u6709\u4e00\u4e2a\u6700\u5927\u6570\u91cf\u7684\u9650\u5236\uff0c\u5982\u679c\u4e0d\u5173\u95ed\u63cf\u8ff0\u7b26\uff0c\u5f88\u6709\u53ef\u80fd\u9020\u6210\u5927 bug\u3002<\/p>\n

\u67e5\u770blinux \u4e2d\u9650\u5236\u7684\u65b9\u6cd5<\/strong><\/span><\/div>\n
$ ulimit -a\r\ncore file size (blocks, -c) 0\r\ndata seg size (kbytes, -d) unlimited\r\nscheduling priority (-e) 0\r\nfile size (blocks, -f) unlimited\r\npending signals (-i) 15732\r\nmax locked memory (kbytes, -l) 64\r\nmax memory size (kbytes, -m) unlimited\r\nopen files (-n) 1024\r\npipe size (512 bytes, -p) 8\r\nPOSIX message queues (bytes, -q) 819200\r\nreal-time priority (-r) 0\r\nstack size (kbytes, -s) 8192\r\ncpu time (seconds, -t) unlimited\r\nmax user processes (-u) 15732\r\nvirtual memory (kbytes, -v) unlimited\r\nfile locks (-x) unlimited\r\n<\/pre>\n

\"\"<\/p>\n

\u6240\u4ee5\uff0c\u5982\u679c\u662f\u7cfb\u7edf\u8c03\u7528\uff0c\u90a3\u4e48 append \u4e0d\u7528\u52a0\u9501\u3002<\/p>\n

\u4e3a\u4ec0\u4e48 php \u8bed\u8a00\u5199\u65e5\u5fd7\u65f6\u7528\u4e86 append \u4e5f\u8981\u52a0\u9501\uff1f<\/p>\n

\u5982\u679c\u6839\u636e\u4e0a\u9762\u7684\u8bf4\u6cd5\uff0c\u54b1\u4eec\u53ef\u4ee5\u8bbe\u7f6e\u597d write \u7684 append \u6807\u5fd7\uff0c\u7136\u540e\u5c31\u53ef\u4ee5\u7761\u5927\u89c9\u53bb\u4e86\uff0c\u6587\u4ef6\u6c38\u8fdc\u4e0d\u4f1a\u51b2\u7a81\u3002<\/p>\n

\u4f46\u662f\uff08\u4e00\u822c\u90fd\u6709\u4e2a\u4f46\u662f\uff09\u4f60\u53bb\u770b php \u7684\u6846\u67b6\u4e2d\u90fd\u4f1a\u5728 file_put_contents \u7684 append \u4e4b\u524d\u52a0\u9501\u3002<\/p>\n

\u4e8e\u662f\uff0c\u6000\u7591\u662f\u56e0\u4e3a file_put_contents \u7684\u5e95\u5c42\u5b9e\u73b0\u6ca1\u6709\u5b9e\u73b0\u539f\u5b50\u6027\u3002<\/p>\n

\u8ddf\u8fdb\u6e90\u7801\uff08\u975e php \u7a0b\u5e8f\u5458\u6216\u8005\u5bf9 php \u5e95\u5c42\u6e90\u7801\u65e0\u5174\u8da3\u7684\u53ef\u4ee5\u8df3\u8fc7\u4e86\uff09\uff1a<\/p>\n

file_put_contents\u5e95\u5c42\u5b9e\u73b0<\/strong><\/span><\/div>\n
 \r\n\/\/ file.c\r\n\/* {{{ proto int|false file_put_contents(string file, mixed data [, int flags [, resource context]])\r\n   Write\/Create a file with contents data and return the number of bytes written *\/\r\nPHP_FUNCTION(file_put_contents)\r\n{\r\n...\r\ncase IS_STRING:\r\n   if (Z_STRLEN_P(data)) {\r\n      numbytes = php_stream_write(stream, Z_STRVAL_P(data), Z_STRLEN_P(data));\r\n      if (numbytes != Z_STRLEN_P(data)) {\r\n         php_error_docref(NULL, E_WARNING, \"Only %zd of %zd bytes written, possibly out of free disk space\", numbytes, Z_STRLEN_P(data));\r\n         numbytes = -1;\r\n      }\r\n   }\r\n   break;\r\n...\r\n}\r\n\r\n\/\/ php_streams.h\r\nPHPAPI ssize_t _php_stream_write(php_stream *stream, const char *buf, size_t count);\r\n#define php_stream_write_string(stream, str)   _php_stream_write(stream, str, strlen(str))\r\n#define php_stream_write(stream, buf, count)   _php_stream_write(stream, (buf), (count))\r\n\r\n\/\/ streams.c\r\nPHPAPI ssize_t _php_stream_write(php_stream *stream, const char *buf, size_t count)\r\n{\r\n\u3000\u3000...\r\n   if (stream->writefilters.head) {\r\n      bytes = _php_stream_write_filtered(stream, buf, count, PSFS_FLAG_NORMAL);\r\n   } else {\r\n      bytes = _php_stream_write_buffer(stream, buf, count);\r\n   }\r\n\r\n   if (bytes) {\r\n      stream->flags |= PHP_STREAM_FLAG_WAS_WRITTEN;\r\n   }\r\n\r\n   return bytes;\r\n}\r\n\r\n\/* Writes a buffer directly to a stream, using multiple of the chunk size *\/\r\nstatic ssize_t _php_stream_write_buffer(php_stream *stream, const char *buf, size_t count){\r\n...\r\nwhile (count > 0) {\r\n   ssize_t justwrote = stream->ops->write(stream, buf, count);\r\n   if (justwrote <= 0) { \/* If we already successfully wrote some bytes and a write error occurred * later, report the successfully written bytes. *\/ if (didwrite == 0) { return justwrote; } return didwrite; } buf += justwrote; count -= justwrote; didwrite += justwrote; \/* Only screw with the buffer if we can seek, otherwise we lose data * buffered from fifos and sockets *\/ if (stream->ops->seek && (stream->flags & PHP_STREAM_FLAG_NO_SEEK) == 0) {\r\n      stream->position += justwrote;\r\n   }\r\n}\r\n\r\n}\r\n\r\n\/\/ php_streams.h\r\n\/* operations on streams that are file-handles *\/\r\ntypedef struct _php_stream_ops  {\r\n   \/* stdio like functions - these are mandatory! *\/\r\n   ssize_t (*write)(php_stream *stream, const char *buf, size_t count);\r\n   ssize_t (*read)(php_stream *stream, char *buf, size_t count);\r\n   int    (*close)(php_stream *stream, int close_handle);\r\n   int    (*flush)(php_stream *stream);\r\n\r\n   const char *label; \/* label for this ops structure *\/\r\n\r\n   \/* these are optional *\/\r\n   int (*seek)(php_stream *stream, zend_off_t offset, int whence, zend_off_t *newoffset);\r\n   int (*cast)(php_stream *stream, int castas, void **ret);\r\n   int (*stat)(php_stream *stream, php_stream_statbuf *ssb);\r\n   int (*set_option)(php_stream *stream, int option, int value, void *ptrparam);\r\n} php_stream_ops;\r\n \r\n\/\/ plain_wrapper.c\r\nstatic ssize_t php_stdiop_write(php_stream *stream, const char *buf, size_t count)\r\n{\r\n   php_stdio_stream_data *data = (php_stdio_stream_data*)stream->abstract;\r\n\r\n   assert(data != NULL);\r\n\r\n   if (data->fd >= 0) {\r\n#ifdef PHP_WIN32\r\n      ssize_t bytes_written;\r\n      if (ZEND_SIZE_T_UINT_OVFL(count)) {\r\n         count = UINT_MAX;\r\n      }\r\n      bytes_written = _write(data->fd, buf, (unsigned int)count);\r\n#else\r\n      ssize_t bytes_written = write(data->fd, buf, count);\r\n#endif\r\n      if (bytes_written < 0) { if (errno == EWOULDBLOCK || errno == EAGAIN) { return 0; } if (errno == EINTR) { \/* TODO: Should this be treated as a proper error or not? *\/ return bytes_written; } php_error_docref(NULL, E_NOTICE, \"write of %zu bytes failed with errno=%d %s\", count, errno, strerror(errno)); } return bytes_written; } else { #if HAVE_FLUSHIO if (data->is_seekable && data->last_op == 'r') {\r\n         zend_fseek(data->file, 0, SEEK_CUR);\r\n      }\r\n      data->last_op = 'w';\r\n#endif\r\n\r\n      return (ssize_t) fwrite(buf, 1, count, data->file);\r\n   }\r\n}\r\n<\/pre>\n

\u8fd9\u4e2a\u51fd\u6570\u6700\u7ec8\u8c03\u7528\u7684\u662f\u51fd\u6570 php_stdiop_write<\/p>\n

\u51fd\u6570 _php_stream_write_buffer \u4e2d\u4f1a\u5c06\u5b57\u7b26\u4e32\u5206\u6210\u591a\u4e2a chunksize \uff0c\u6bcf\u4e2a chunksize \u4e3a 8192 \uff088K\uff09 \u5b57\u8282\uff0c\u5206\u522b\u8fdb\u884c write\u3002<\/p>\n

\u5982\u679c\u4e0d\u52a0\u9501\uff0c\u90a3\u4e48\u8d85\u8fc7 8192 \u5b57\u8282\u4e4b\u540e\uff0c\u591a\u4e2a\u8fdb\u7a0b\u5199\u65e5\u5fd7\u5c31\u4f1a\u51fa\u73b0\u6df7\u4e71\u3002<\/p>\n

\u800c\u4e14\uff0cphp \u6587\u6863\u4e5f\u8bf4\u660e\u4e86\uff1a<\/p>\n

\"\"<\/p>\n

\"\"<\/p>\n","protected":false},"excerpt":{"rendered":"

\u4e3a\u4e86\u8ba9\u81ea\u5df1\u7684\u601d\u8def\u66f4\u52a0\u6e05\u6670\uff0c\u4e0b\u9762\u6211\u90fd\u4f1a\u79f0\u65e5\u5fd7\u4e3a log\u3002\u56e0\u4e3a\u65e5\u5fd7\u8fd9\u4e2a\u8bcd\u6709\u4e24\u79cd\u542b\u4e49\u3002 \u65e5\u8bb0\u7684\u53e6\u4e00\u79cd\u8bf4\u6cd5\u3002\u201c\u5fd7\u201d\u5b57\u672c […]<\/p>\n","protected":false},"author":668,"featured_media":169643,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[55],"tags":[],"class_list":["post-169637","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-thread"],"acf":[],"_links":{"self":[{"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/posts\/169637","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/users\/668"}],"replies":[{"embeddable":true,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/comments?post=169637"}],"version-history":[{"count":3,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/posts\/169637\/revisions"}],"predecessor-version":[{"id":169974,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/posts\/169637\/revisions\/169974"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/media\/169643"}],"wp:attachment":[{"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/media?parent=169637"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/categories?post=169637"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/tags?post=169637"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}