InfinityQuest - Programming Code Tutorials and Examples with Python, C++, Java, PHP, C#, JavaScript, Swift and more

Menu
  • Home
  • Sitemap

Python Programming Language Best Tutorials and Code Examples

Learn Python Right Now!
Home
PHP
Logging Errors in PHP
PHP

Logging Errors in PHP

InfinityCoder December 23, 2016

You want to save program errors to a log. These errors can include everything from parser errors and files not being found to bad database queries and dropped connections.

Use error_log() to write to the error log:

1
2
3
4
// LDAP error
if (ldap_errno($ldap)) {
  error_log("LDAP Error #" . ldap_errno($ldap) . ": " . ldap_error($ldap));
}

Logging errors facilitates debugging. Smart error logging makes it easier to fix bugs. Always log information about what caused the error:

1
2
3
4
5
6
7
$r = mysql_query($sql);
if (! $r) {
  $error = mysql_error();
  error_log('[DB: query @'.$_SERVER['REQUEST_URI']."][$sql]: $error");
} else {
  // process results
}

You’re not getting all the debugging help you could be if you simply log that an error occurred without any supporting information:

1
2
3
4
5
6
$r = mysql_query($sql);
if (! $r) {
  error_log("bad query");
} else {
  // process result
}

Another useful technique is to include the __FILE__, __LINE__, __FUNCTION__, __CLASS__, and __METHOD__ “magic” constants in your error messages:

1
error_log('['.__FILE__.']['.__LINE__."]: $error");

The __FILE__ constant is the current filename, __LINE__ is the current line number, __FUNCTION__ is the current function name, __METHOD__ is the current method name (if any), and __CLASS__ is the current class name (if any).

Starting with PHP 5.3.0, __DIR__ is the directory that __FILE__ is in and __NAMESPACE__ is the current namespace.

Starting in PHP 5.4.0, __TRAIT__ is the current trait name (if any).

Share
Tweet
Email
Prev Article
Next Article

Related Articles

Instantiating Objects in PHP
You want to create a new instance of an object. …

Instantiating Objects in PHP

Determining the User’s Locale in PHP
You want to use the correct locale as specified by …

Determining the User’s Locale in PHP

About The Author

InfinityCoder
InfinityCoder

Leave a Reply

Cancel reply

Recent Tutorials InfinityQuest

  • Adding New Features to bash Using Loadable Built-ins in bash
    Adding New Features to bash Using Loadable …
    June 27, 2017 0
  • Getting to the Bottom of Things in bash
    Getting to the Bottom of Things in …
    June 27, 2017 0

Recent Comments

  • fer on Turning a Dictionary into XML in Python
  • mahesh on Turning a Dictionary into XML in Python

Categories

  • Bash
  • PHP
  • Python
  • Uncategorized

InfinityQuest - Programming Code Tutorials and Examples with Python, C++, Java, PHP, C#, JavaScript, Swift and more

About Us

Start learning your desired programming language with InfinityQuest.com.

On our website you can access any tutorial that you want with video and code examples.

We are very happy and honored that InfinityQuest.com has been listed as a recommended learning website for students.

Popular Tags

binary data python CIDR convert string into datetime python create xml from dict python dictionary into xml python how to create xml with dict in Python how to write binary data in Python IP Address read binary data python tutorial string as date object python string to datetime python

Archives

  • June 2017
  • April 2017
  • February 2017
  • January 2017
  • December 2016
  • November 2016
Copyright © 2021 InfinityQuest - Programming Code Tutorials and Examples with Python, C++, Java, PHP, C#, JavaScript, Swift and more
Programming Tutorials | Sitemap