Skip to content

Instantly share code, notes, and snippets.

@mgronhol
Created September 6, 2012 08:13
Show Gist options
  • Save mgronhol/3652880 to your computer and use it in GitHub Desktop.
Save mgronhol/3652880 to your computer and use it in GitHub Desktop.
Append log for PHP [4|5]
<?php
function append_log_write( $filename, $parametres ){
// Return FALSE if no parametres given
if( count( $parametres ) < 1 ){
return FALSE;
}
// Timestamp in Zebra Time
$line = gmdate( "Y-m-d\TH:i:s" ) . "Z\t";
$index = 0;
for( $index = 0 ; $index < count( $parametres ) ; $index += 1 ){
// Escape field content
$field = addslashes( $parametres[ $index ] );
$field = str_replace( "\n", "\\n", $field );
$field = str_replace( "\r", "\\r", $field );
$field = str_replace( "\t", "\\t", $field );
$line .= $field . "\t";
}
// Escape field content
$field = addslashes( $parametres[ $index ] );
$field = str_replace( "\n", "\\n", $field );
$field = str_replace( "\r", "\\r", $field );
$field = str_replace( "\t", "\\t", $field );
$line .= $field;
$line .= "\n";
// Open file in append mode
$handle = fopen( $filename, "a" );
// Acquire exclusive writing lock to file
flock( $handle, LOCK_EX );
fwrite( $handle, $line );
// Release the lock
flock( $handle, LOCK_UN );
// Close
fclose( $handle );
return TRUE;
}
// Example usage:
//append_log_write( "/var/log/orders.log", array( "127.0.0.1", "mauri-antero", "/tilaukset/unicycle\n" ) );
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment