Creating Log file in PHP
We can create log files through our scripts which is useful to know what is happening in our applications.
<?php
function writelog ($logentry, $lgname)
{
$logfile = @fopen ($lgname, “a+”);
if (!$logfile)
{
echo (”nn ERROR: Failed to open $lgname”);
}
else
{
fwrite ($logfile, “[”.date (”D M d Y h:iA”).”] [$logentry]n”);
fclose ($logfile);
}
}
// example usage
writelog(”Something happened in our application”, “logfile.txt”);
?>
In above example, we can pass message in writelog() funtion. In writelog() funtion we can open the file in append mode and write the log entry with date.
