Ingus
Ingus

Reputation: 1044

How to log errors on hosting that does not support logs?

I want to log errors to a file using this method:

$tpath = "/storage/my-errors.log";
error_log("You messed up!", 3, $tpath);

I have set permissions on the path to 777 and it should save this value "You messed up!" into the file, but I can't figure it out.

I made an error myself, but it did not get saved in the log file, just messaged "You messed up!"!

I also tried:

ini_set("log_errors", 1);
ini_set("error_log", "mypath");
error_log( "Hello, errors!" );

Upvotes: 0

Views: 95

Answers (1)

coderodour
coderodour

Reputation: 1057

You can log using your desired method into a CSV.

Create a re-usable function which will control all your logging:

function logger($log_name, $log_message){

  $todays_date = date('Y-m-d');

  // Open the file to write to it, file is created if it does not exist.
  $output = fopen($log_name."-".$todays_date.".csv", 'w');

  // Write line of log in CSV format to the log.
  fputcsv($output, array("[".$todays_date."] ",$log_message));

  // Close the file.
  fclose($output);
}

You can use this with your process names as the $log_name.

Example

logger("user_login", "You messed up");

This would produce a log called user_login-2017-02-13.csv. The file will be created if it does not exist, and subsequent writes to the file will be appended line by line.

The date part is there to automatically make a new file for each day, so you can alter that to your needs.

Upvotes: 1

Related Questions