Creating Log file in PHP

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.

Leave a Reply


All material @ copyrighted by chrisranjana.com. If you want to link to this article you are welcome to do so. Unauthorized publication is strictly prohibited. This developer tutorial website contains articles by Php programmers , Software developers, Mysql programmers and asp c# programmers. This website also contains ajax tutorials and advanced mysql sql stored procedures and functions tutorials and sample codes.