Wednesday, January 12, 2011

12:26 AM
1
As you certainly know, PHP can be used for command line scripting. In my case, I caught myself writing log PHP function many times so I finally decided to write Logging PHP class. After Logging class initialization, first call of lwrite method will open log file for writing. Log file will be closed implicitly when PHP script ends.



How to use Logging class:


// Logging class initialization
$log = new Logging();
// write message to the log file
$log->lwrite('Test message');


Logging class:



/**
 * Logging class:
 * - contains lopen and lwrite methods
 * - lwrite will write message to the log file
 * - first call of the lwrite will open log file implicitly
 * - message is written with the following format: hh:mm:ss (script name) message
 */
class Logging{
// define log file
private $log_file = '/tmp/logfile.txt';
// define file pointer
private $fp = null;
// write message to the log file
public function lwrite($message){
// if file pointer doesn't exist, then open log file
if (!$this->fp) $this->lopen();
// define script name
$script_name = pathinfo($_SERVER['PHP_SELF'], PATHINFO_FILENAME);
// define current time
$time = date('H:i:s');
// write current time, script name and message to the log file
fwrite($this->fp, "$time ($script_name) $message\n");
}
// open log file
private function lopen(){
// define log file path and name
$lfile = $this->log_file;
// define the current date (it will be appended to the log file name)
$today = date('Y-m-d');
// open log file for writing only; place the file pointer at the end of the file
// if the file does not exist, attempt to create it
$this->fp = fopen($lfile . '_' . $today, 'a') or exit("Can't open $lfile!");
}
}


For PHP prior to version 5.2.0 you will have to replace or change line with built-in PHP function pathinfo()because PATHINFO_FILENAME constant was added in PHP 5.2.0



// for PHP 5.2.0+
$script_name = pathinfo($_SERVER['PHP_SELF'], PATHINFO_FILENAME);

// for PHP before version 5.2.0
$script_name = basename($_SERVER['PHP_SELF']);
$script_name = substr($script_name, 0, -4); // php extension and dot are not needed


If you want to send a message to the PHP's system logger, please see the error_log PHP command.

1 comments:

MrBertie said...

Here's my current logging/debugging module:

cached log file
* @staticvar int $start => first time function was called during this request
* @param bool $debug => true if debug
* @param [multiple] $message => any number of variables|strings, to be displayed
*/
function logger($debug, $message) {
static $log_file;
static $start = 0;

$print_header = ($start == 0);
if ($start == 0) {
$start = microtime(true);
$time = 0;
} else {
$time = microtime(true) - $start;
}
$time = sprintf('%0.06f ms', $time);
$args = func_get_args();
$debug = $args[0];
array_shift($args);

if(!empty($args)) {
if(!$log_file) {
$file_path = APP_PATH . ($debug) ? config('debug_file') : config('log_file');
$open_type = 'a';
$log_file = fopen($file_path, $open_type) or exit("Cannot open Log file: ".$file_path);
}
if ($print_header) {
fwrite($log_file, "\n\nLog File: " . date('Y-m-d H:i:s') . "\n" .
str_repeat('=', 30) . "\n");
}
for ($i = 0; $i < count($args); $i++) {
$args[$i] = var_export($args[$i], true);
}
$message = ($args !== null && is_array($args)) ? implode("\t", $args) : $args;
fwrite($log_file, '[' . $time . ']' . "\t" . $message . "\n");
}
}