jision
jision

Reputation: 181

file writes in php for logging is that a performance blocker

I have a piece of code in most of my functions for logging purpose now on production and we see a logs files of 2gb per day is it okay to write directly to log files using file_put_contents. We are speculating this file_get_contents will become a blocking point as this size and traffic increases. Can someone guide me a proper way to do logging in physical files without blocking php. Any other approach is most welcomed.

file_put_contents(
            getcwd() . "debug_log.txt",
            "" . print_r($updateFieldsArray, true) . "\n",
            FILE_APPEND | LOCK_EX
        );

Thanks in advance.

Upvotes: 1

Views: 651

Answers (1)

Joni
Joni

Reputation: 111229

Writing logs to files will eventually become a bottleneck if you keep growing (just because anything can become a bottleneck), but we can't tell you if it's something you should worry about now. You could start using a logging library that allows changing the storage system through configuration, such as monolog.

These libraries are very helpful when developing, too. You can do things like enable debug output but only for the part of the app you're working with.

You can always move the logs to a faster disk or stop logging information you don't need. The harder problem with logging to files is what will you do when you have more than one server (think load balancing, high availability). Now you have to read the logs on all servers to find anything.

A possible solution is having all servers send their logs to a centralized log server. This can be done with syslog. Alternatively each server can have a program that reads the log files as they are produced and stores the information in a central database. This is what logstash does.

Upvotes: 1

Related Questions