Writing your own log from PHP: error_log and a log file you can read

When something fails only in production, your best ally is a log your own code writes: one line with the time, what happened and the data that helps you understand. In PHP you do it with the error_log() function, which writes to the folder’s error log, or with a file of your own, which is easier to read and to clean.

Two ways, and when to use each

error_log('text') A file of your own
Where it writes To PHP’s error log, the folder’s error_log file. Wherever you say, for example a logs folder outside the public folder.
Mixing It lands among PHP’s own warnings and errors. Holds only what you wrote: you read it in one go.
Cleaning It grows forever and there is one per folder. You can have one file per day and delete the old ones.
When A one-off notice, a quick debug. An application’s log or a cron job’s.

A small function that does the job

<?php
function logline(string $level, string $text): void {
  $folder = '/home/ACCOUNT/app-logs';
  $file = $folder . '/app-' . date('Y-m-d') . '.log';
  $line = date('H:i:s') . ' [' . $level . '] ' . $text . PHP_EOL;
  error_log($line, 3, $file);
}

logline('INFO', 'Order 123 saved');
logline('ERROR', 'The API answered 502');

The second argument 3 tells error_log to append to the file you name instead of using the error log, which is why the line break (PHP_EOL) is yours to add. The time comes out in whichever time zone the folder’s PHP is using. If you want another, set it at the top of the script with date_default_timezone_set(), but pick one and keep it.

Getting it working, step by step

1 Create the folder outside the public folder, for example next to public_html. A log inside the site can be downloaded by anyone who guesses the name.
2 Check that PHP can write there. The folder must belong to the account and be writable. If the first line never shows up, it is nearly always the path or the permission.
3 Write a test entry and open the file in the File Manager. In the Terminal, tail -n 50 file.log shows the last fifty lines (the cPanel Terminal).
4 Log the points that matter: the start and end of a task, each failure of an outside call, each decision the code took. Do not log everything.
5 Delete the old ones. With one file per day, a weekly cron job that deletes the oldest keeps the folder from growing without end. See cron jobs and the account’s file limits.
Never write passwords, API keys or personal data to the log. The file sits on disk, goes into backups, and people open it to help. Log the identifier (order 123), not the card, the contact or the whole request body.
To find out why a page is blank, PHP’s error log is the first place (a blank page in PHP). Your own log is for what PHP does not count as an error: an order that did not save, an API that answered badly, a decision the code made.

The log is not created and you cannot tell why? Tell us the path you used and the message in the folder’s error_log.

Open a support ticket

SEE ALSO

Where the PHP error log is

A blank page in PHP: how to see the error

Running a PHP script from a cron job

json_decode returns null: what is wrong with the JSON

RECOMMENDED PRODUCT

Web hosting with cPanel

Domain and SSL included, daily backups and the panel you already know. from 5.940,00 Kz/mo (3-year plan, with coupon)

See plans
  • 0 Users Found This Useful
Was this answer helpful?