Hello, friends! Many hosting servers do not grant permission to access error log files. Therefore, I would like to explain how to create custom log files using PHP.

In the world of web development, encountering server error logs is a common occurrence. They provide invaluable insights into what’s happening behind the scenes, helping developers troubleshoot issues and fine-tune their applications. However, accessing these error logs isn’t always straightforward, as many hosting servers restrict such permissions. But fear not, for in this article, we’ll explore a practical solution: creating custom log files using PHP.

The ability to generate custom log files not only grants developers more control but also offers a tailored approach to tracking errors, events, and other important data within your web application. Whether you’re working on a personal project or managing a professional website, this guide will show you how to harness the power of PHP to create custom logs that fit your specific needs.

The provided script is used to create a custom error log file.

PHP Code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
<?php
error_reporting(0);  // to create  a own error handling

// user defined function

function userErrorHandler($errno, $errmsg, $filename, $linenum, $vars)
{
// timestamp for the error entry.
$dt = date('Y-m-d H:i:s (T)');

// errors types

$errortype = array (
E_ERROR => 'Error',
E_WARNING => 'Warning',
E_PARSE => 'Parsing Error',
E_NOTICE => 'Notice',
E_CORE_ERROR => 'Core Error',
E_CORE_WARNING => 'Core Warning',
E_COMPILE_ERROR => 'Compile Error',
E_COMPILE_WARNING => 'Compile Warning',
E_USER_ERROR => 'User Error',
E_USER_WARNING => 'User Warning',
E_USER_NOTICE => 'User Notice',
E_STRICT => 'Runtime Notice'
);

$user_errors = array(E_USER_ERROR, E_USER_WARNING, E_USER_NOTICE);

$err = "<errorentry>\n";
$err .= "\t<datetime>" .$dt. "</datetime>\n";
$err .= "\t<errornum>" .$errno. "</errornum>\n";
$err .= "\t<errortype>" .$errortype[$errno]. "</errortype>\n";
$err .= "\t<errormsg>" .$errmsg. "</errormsg>\n";
$err .= "\t<scriptname>" .$filename. "</scriptname>\n";
$err .= "\t<scriptlinenum>" .$linenum. "</scriptlinenum>\n";

if (in_array($errno, $user_errors)) {
$err .= "\t<vartrace>" .wddx_serialize_value($vars, 'Variables'). "</vartrace>\n";
}
$err .= "</errorentry>\n\n";

// write the errors to one file that is

error_log($err, 3, 'error_log.log');

}

// to call the user defined function

$old_error_handler = set_error_handler('userErrorHandler');

?>

At the top of each page write the above code or write the above code at another file and include the that file at top of each page.

In conclusion, the art of creating custom log files using PHP is a valuable skill for web developers. It empowers you to gain a deeper understanding of what’s happening within your applications and websites. Whether it’s monitoring errors, tracking user interactions, or logging specific events, custom logs provide a level of control and insight that is often essential in the development and maintenance of web projects.

As you implement the techniques described in this article, you’ll be better equipped to troubleshoot issues, analyze performance, and gather valuable data to enhance your web applications. The ability to create custom logs using PHP not only simplifies the debugging process but also offers a tailored approach to maintaining and improving the functionality and reliability of your web projects. Embrace this skill, and watch your web development prowess grow.

Categories: PHP Code

1 Comment

Mirek · April 12, 2014 at 12:08 pm

This is extremely useful. Thanx man

Leave a Reply

Avatar placeholder

Your email address will not be published. Required fields are marked *