How to do error logging in CodeIgniter (PHP)
Asked Answered
L

6

109

I want error logging in PHP CodeIgniter. How do I enable error logging?

I have some questions:

  1. What are all the steps to log an error?
  2. How is an error log file created?
  3. How to push the error message into log file (whenever an error occurs)?
  4. How do you e-mail that error to an email address?
Laundromat answered 9/7, 2010 at 4:21 Comment(0)
Y
212

CodeIgniter has some error logging functions built in.

  • Make your /application/logs folder writable
  • In /application/config/config.php set
    $config['log_threshold'] = 1;
    or use a higher number, depending on how much detail you want in your logs
  • Use log_message('error', 'Some variable did not contain a value.');
  • To send an email you need to extend the core CI_Exceptions class method log_exceptions(). You can do this yourself or use this. More info on extending the core here

See http://www.codeigniter.com/user_guide/general/errors.html

Yevetteyew answered 9/7, 2010 at 6:23 Comment(5)
Are there any security implications for this?Entozoic
This dumps data to a php file whose filename is in the format: log-[Y-m-d H:i:s] or whatever value is set on log_date_format key configuration defined on your config file. For as long as you don't get to log/dump sensitive data, it should be pretty safe. This file is relative to an index.html page by default so obviously generated php files wouldn't be publicly accessible- but you'll never know. Make sure to have all of this precautions in place if you change the default application/logs directory.Chessa
Make your /application/logs folder writable <<< that helped mePerplex
When I set "$config['log_threshold'] = 1;", I didn't get any logs in my /application/logs folder. However, when I set "$config['log_threshold'] = 3", a log file appeared. Hope this helps. Thank you! Peace.Ambert
After enabling logging from the config file in the project, now you can use error_log() to print directly to the console.Ovi
V
27

To simply put a line in the server's error log, use PHP's error_log() function. However, that method will not send an e-mail.

First, to trigger an error:

trigger_error("Error message here", E_USER_ERROR);

By default, this will go in the server's error log file. See the ErrorLog directive for Apache. To set your own log file:

ini_set('error_log', 'path/to/log/file');

Note that the log file you choose must already exist and be writable by the server process. The simplest way to make the file writable is to make the server user the owner of the file. (The server user may be nobody, _www, apache, or something else, depending on your OS distribution.)

To e-mail the error, you need to set up a custom error handler:

function mail_error($errno, $errstr, $errfile, $errline) {
  $message = "[Error $errno] $errstr - Error on line $errline in file $errfile";
  error_log($message); // writes the error to the log file
  mail('[email protected]', 'I have an error', $message);
}
set_error_handler('mail_error', E_ALL^E_NOTICE);

Please see the relevant PHP documentation for more info.

Vermont answered 9/7, 2010 at 5:22 Comment(0)
U
4

Also make sure that you have allowed codeigniter to log the type of messages you want in a config file.

i.e $config['log_threshold'] = [log_level ranges 0-4];

Unprintable answered 30/1, 2016 at 13:43 Comment(0)
M
4
In config.php add or edit the following lines to this:
------------------------------------------------------
$config['log_threshold'] = 4; // (1/2/3)
$config['log_path'] = '/home/path/to/application/logs/';

Run this command in the terminal:
----------------------------------
sudo chmod -R 777 /home/path/to/application/logs/
Mozza answered 14/7, 2020 at 10:11 Comment(2)
It is totally unsafe to give the 777 permission to any folderTransmutation
Yes, 775 is enough.Loireatlantique
P
1

More oin regards to question part 4 How do you e-mail that error to an email address? The error_log function has email destination too. http://php.net/manual/en/function.error-log.php

Agha, here I found an example that shows a usage. Send errors message via email using error_log()

error_log($this->_errorMsg, 1, ADMIN_MAIL, "Content-Type: text/html; charset=utf8\r\nFrom: ".MAIL_ERR_FROM."\r\nTo: ".ADMIN_MAIL);
Perimorph answered 22/1, 2017 at 13:16 Comment(0)
M
0

Enable logging

Machine : Mac OS - Intel chip

Yes you can enable it for localhost. just go to src/customers/application/config/config.php and add

$config['log_threshold'] = 1;
$config['log_path'] = '/Applications/XAMPP/logs/';

And Restart the server enter image description here

Example :
I have added below log_message statement in file : src/customers/application/views/admin/orders/view.php

log_message('info', 'Inside view.php file');
log_message('debug', 'Inside view.php file');
log_message('error', 'Inside view.php file');

Generated log file format : log-YYYY-MM-DD.php inside location /Applications/XAMPP/logs/

enter image description here

Manhole answered 29/9, 2023 at 3:59 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.