0

I am developing a custom module and I want to write debugging output to a log file so I can monitor it in real time, and not wait to navigate to watchdog.

I was using file_save_data() to write to the public file system, but quickly noticed that it was making log1.txt, log2.txt, log3.txt, etc. I want all my log messages to go into the same file. I looked at the documentation, and saw that the only flags that exist are FILE_EXISTS_REPLACE, FILE_EXISTS_RENAME, and FILE_EXISTS_ERROR. Sadly for me, no FILE_EXISTS_APPEND.

What is the drupal way to append data to a file on the file system? Or should I just use native php functions?

1 Answer 1

1

There is no existing way to append data to an existing file. Only modes you have listed in the question.

You will have to go for native PHP functions (a mode in fopen() or FILE_APPEND in file_put_contents()).

Some pointers:

Consider using hook_watchdog(). You can filter what you need to log to the file using the type key of the passed array.

Don't forget that there is also a module, syslog, to log these watchdog entries to syslog.

You can also use dd() function from devel module to easily log specific data to a temp file.

dd()

Logs any variable to a file named “drupal_debug.txt” in the site’s temp directory. All output from this function is appended to the log file, making it easy to see how the contents of a variable change as you modify your code.

If you’re using Mac OS X you can use the Logging Console to monitor the contents of the log file.

If you’re using a flavor of Linux you can use the command “tail -f drupal_debug.txt” to watch the data being logged to the file.

dd($data, $label = NULL)

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.