how to create a logfile in php?
snippet in php

how to create a logfile in php?


function wh_log($log_msg)
    $log_filename = "log";
    if (!file_exists($log_filename)) 
        // create directory/folder uploads.
        mkdir($log_filename, 0777, true);
    $log_file_data = $log_filename.'/log_' . date('d-M-Y') . '.log';
    // if you don't add `FILE_APPEND`, the file will be erased each time you add a log
    file_put_contents($log_file_data, $log_msg . "\n", FILE_APPEND);
// call to function
wh_log("this is my log message");

how to create a logfile in php?


// Send notification through the server log if we can not
// connect to the database.
if (!Ora_Logon($username, $password)) {
    error_log("Oracle database not available!", 0);

// Notify administrator by email if we run out of FOO
if (!($foo = allocate_new_foo())) {
    error_log("Big trouble, we're all out of FOOs!", 1,

// another way to call error_log():
error_log("You messed up!", 3, "/var/tmp/my-errors.log");

how to create a logfile in php?


//Something to write to txt log
$log  = "User: ".$_SERVER['REMOTE_ADDR'].' - '.date("F j, Y, g:i a").PHP_EOL.
        "Attempt: ".($result[0]['success']=='1'?'Success':'Failed').PHP_EOL.
        "User: ".$username.PHP_EOL.

//Save string to log, use FILE_APPEND to append.
file_put_contents('./log_'.date("j.n.Y").'.log', $log, FILE_APPEND);