2

I have a WordPress issue and want to simply write log messages to a text file. I am aware that error_log exists, but want to have a more segregated log file for different messages.

I am using wp_filesystem->put_contents, and it DOES write to the file and succeeds, but it ONLY outputs the last call's data.

I have the following method:

public static function log_message($msg) {
  error_log($msg);
  require_once(ABSPATH . 'wp-admin/includes/file.php');
  global $wp_filesystem;
  if ( ! is_a( $wp_filesystem, 'WP_Filesystem_Base') ){
      $creds = request_filesystem_credentials( site_url() );
      wp_filesystem($creds);
  }

  $bt = debug_backtrace();
  $caller = array_shift($bt);
  $logStr = date("Y-m-d hh:ii A",time())." - ".$caller['file'].":".$caller['line']." - ".$msg;
  $filePathStr = SRC_DIR.DIRECTORY_SEPARATOR.$logFileName;

  $success = $wp_filesystem->put_contents(
      $filePathStr,
      $logStr,
      FS_CHMOD_FILE // predefined mode settings for WP files
  );

  if(!$success) {
      error_log("Writing to file \"".$filePathStr."\" failed.");
  } else {
      error_log("Writing to file \"".$filePathStr."\" succeeded.");
  }
}

I call it using:

log_message("\nTest 1");
log_message("\nTest 2");
log_message("\nTest 3");

The output is ALWAYS ONLY Test 3 with the other invocations being ignored yet, their output appears in the debug.log as well as all the success messages.

Why would this be?

Looking at the WPCodex for the source code of this, it uses fwrite behind the scenes. The file is closed in this code, and I cannot use any "flush" technique.

Is there a way to figure this out?

Nasser Ali Karimi
  • 4,462
  • 6
  • 34
  • 77
Richard Duerr
  • 566
  • 8
  • 24
  • According to [this post](https://wordpress.stackexchange.com/questions/269132/how-to-append-contents-using-wp-filesystem) , the wp_filesystem is very limited. You may want to append to the file using PHP's fwrite with a file handle's append command. – Jamie_D Nov 17 '18 at 19:02
  • Ohhhh, that makes sense. I assumed the function "file_put_contents" was simply for APPENDING to a file... It will CLEAR the file, and put the contents there. This function is meant to calculate/fetch a data stream, and then save it, NOT to be used for file saving. – Richard Duerr Nov 18 '18 at 04:21

1 Answers1

1

I found that the source of WP_Filesystem uses file_put_contents (as the name does suggest), and I assumed this is for APPENDING to the file's data.

This is incorrect.

This function is to take data, and then WRITE it to the file, erasing prior data. Mainly useful for creating resources, downloading a file, etc.

If I want to APPEND to a file, I need to use 'fwrite'.

This post describes that.

This is the example to APPEND to a file:

$filepath = '\path\to\file\';
$filename = 'out.log';
$fullpath = $filepath.$filename;

if(file_exists($fullpath)) {
  $file = fopen($filepath.$filename, "a");//a for append -- could use a+ to create the file if it doesn't exist
  $data = "test message";
  fwrite($file, "\n". $data);
  fclose($file);
} else {
  error_log("The file \'".$fullpath."\' does not exist.");
}

The fopen docs describe this method and it's modes.

Richard Duerr
  • 566
  • 8
  • 24