Imagination of high-performance php logging solution
High-performance php logging solution
The log is completed in two steps:
- Log information is written in real time in the memory cache to minimize performance overhead
- crontab timing from Collect and write to database or file in memory cache
Specific algorithm: (laravel framework)
- Set a constant: imax=10000000, two variables iwrite=1, iread=1. (iwrite Log writing pointer, iread log reading pointer, iread <=iwrite, the two variable values are stored in the memory cache)
- Every time a log is generated, use "Cache::increment (iwrite)" to obtain iwrite Incremental value, if the value is greater than imax, reset to 1, store the log in the cache, key: 'log_'+iwrite, value: log content
-
crontab calls the command line every 30 seconds to collect logs, use "Cache ::increment(iread)" gets the incremental value of iread. If it is equal to iwrite, stop. If the value is greater than imax, reset to 1, read the log from the cache and store it in batches. Finally delete the log from cache.
Memory cache can use apc, xcache, etc.
Cache::increment , belongs to laravel framework
Built-in methods, if it is not the laravel framework, you can encapsulate it yourself.
I don’t have time yet, so I’ll record my thoughts first,
When you have time in the future, practice it and then send out the code.
The above has introduced the idea of a high-performance PHP logging solution, including various aspects. I hope it will be helpful to friends who are interested in PHP tutorials.