Tag Archives: PHP - Page 5

Custom log to file function with log rotation and log levels

I often need logging in my applications so instead of inventing the wheel every time I decided to write a reusable log function. This is what I came up with (PHP 5.3.3)

/**
 * Log function with log file rotation
 * and loglevel restrictions
 *
 * @param <int> $level
 * @param <string> $event
 * @param <string> $text
 */
function logger($level, $event, $text = null){
        $maxsize = 5242880; //Max filesize in bytes (e.q. 5MB)
        $dir = "log/";
        $filename = "myapplication.log";
        $loglevel = 5;

        if(file_exists($dir.$filename) &&
                  filesize($dir.$filename) > $maxsize){
            $nb = 1;
            $logfiles = scandir($dir);
            foreach ($logfiles as $file) {                
                $tmpnb = substr($file, strlen($filename));
                if($nb < $tmpnb){
                    $nb = $tmpnb;
                }
            }
            rename($dir.$filename, $dir.$filename.($nb + 1));
        }
        if($level <= $loglevel) { 
           $data = date('Y-m-d H:i:s')." LEVEL: ".$level." ";
           $data .= "EVENT: ".$event." ".$text.PHP_EOL;
           file_put_contents($dir.$filename, $data, FILE_APPEND);
        }
}

This function will write a log row with a timestamp, error level, event name and error text (optional) to the logfile specified if the error level is below or equal to the general log level ($loglevel). If the logfile is bigger then $maxsize it vill rename the logfile by appending the next inline number to the end and create a new logfile.

Using PHP curl to PUT a string to a web service

With PHP curl function it is easy to PUT a file to a web service. However doing a PUT on a string is a little trickier. This is how I did it in PHP v5.1.6:

// The message
$message = "My little string of characters";

// Get the curl resource handle
$session = curl_init("http://localhost/myservice");

// Prepare message for PUT
$put_data = tmpfile(); //Get temporary filehandle
fwrite($put_data, $message); //Write the string to the temporary file
fseek($put_data, 0); //Put filepointer to the beginning of the temporary file

// Set the POST options
curl_setopt($session, CURLOPT_PUT, true); // Use PUT
curl_setopt($session, CURLOPT_HEADER, true); // Send header
curl_setopt($session, CURLOPT_RETURNTRANSFER, true); // Get response
curl_setopt($session, CURLOPT_INFILE, $put_data); // Set string data
curl_setopt($session, CURLOPT_INFILESIZE, strlen($message)); //Set data size

// Do the PUT and show the response
$response = curl_exec($session);
echo $response;

This will simply write my string ($message) to a php temporary file ($put_data). I then use this file when sending the data to the web service. NOTE: CURLOPT_HEADER and CURLOPT_RETURNTANSFER are optional.

Run simple php functions direct on command line

If you want to run/test oneliners in PHP on the command line you can use the -r option.

php -r 'echo "Hello World";' 

Will simply output “Hello World” on the command line

I sometimes use this method to test regexp expressions

php -r 'echo preg_match("/^Test/", "This is a test");'

Here I check if $string starts with the string ‘Test’ (the answer is no).

Using onliners on the command line can sometimes be very handy