0

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!" );
Félix Gagnon-Grenier
  • 7,344
  • 10
  • 45
  • 58
Ingus
  • 943
  • 8
  • 29
  • 1
    So do you want to throw an error? Or what exactly is it that you are asking? – David Wolters Feb 13 '17 at 20:25
  • @DavidWolters i want to log php and mysql errors if they exit – Ingus Feb 13 '17 at 20:27
  • 2
    The path that you see as `/storage` is most likely not just `/storage` but something like `/users/i/n/g/ingus/storage/` or similar with your username. You can likely figure it out by dumping the `$_SERVER['DOCUMENT_ROOT']` variable. Chances are you don't have write to anything above your own personal directory but they have chroot'ed (or locked the ftp) above that so you see `/` as your root when it really isn't the root of the server. – Jonathan Kuhn Feb 13 '17 at 20:27
  • @JonathanKuhn i just added it short i have the full path and as i sayed that message is inserting in my log file.its just that i cant add php/mysql errors in it – Ingus Feb 13 '17 at 20:28
  • 1
    If you just need to direct normal PHP errors to your specific log, check this: http://stackoverflow.com/questions/3531703/how-to-log-errors-and-warnings-into-a-file – Don't Panic Feb 13 '17 at 20:29
  • 1
    how about: `ini_set("log_errors","1"); ini_set("error_log","/path/to/file/errors.txt");` –  Feb 13 '17 at 20:31
  • 1
    If you have access to create your php.ini (some hosts do allow) or setup an htaccess file you can also set all of those directives (from Don't Panic's comment) there which would allow you to capture fatal and syntax errors. – Jonathan Kuhn Feb 13 '17 at 20:31
  • 1
    This looks like it _might_ be a duplicate of that, but I haven't voted to close because I'm not totally sure if you're asking the same thing. – Don't Panic Feb 13 '17 at 20:31
  • @Don'tPanic i thin that could be duplicate also i saw that post before but there nothing helped me :( the last straw was to post new message :( – Ingus Feb 13 '17 at 20:38
  • 1
    My main point is, `error_log` will log an error (either to a specified log or the server's default), but specifying a log path in a call to `error_log` doesn't set the log path for general PHP errors, only for that specific call. To direct other errors to your custom path, you'll need to use one of the various ini methods, whether it's `ini_set`, or one of the other possibilities that Jonathan mentioned. – Don't Panic Feb 13 '17 at 20:42
  • 1
    Also keep in mind that if you use `ini_set`, it will change the setting only during execution of the script (not permanently), so you'll have to be sure to call it with every request. – Don't Panic Feb 13 '17 at 20:45
  • ok going to try to do it ASAP thanks guys! – Ingus Feb 13 '17 at 20:46
  • cant make it work :( ini_set("log_errors", 1); ini_set("error_log", "/storage/my-errors.log"); error_log( "Hello, errors!" ); – Ingus Feb 13 '17 at 20:50

1 Answers1

1

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.

coderodour
  • 1,017
  • 7
  • 16
  • This is really cool !Thanks!But i want to lop php/mysql errors as i have no accses to them – Ingus Feb 13 '17 at 21:36