Skip to content

Instantly share code, notes, and snippets.

@gokhanbarisaker
Created September 24, 2012 12:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save gokhanbarisaker/3775653 to your computer and use it in GitHub Desktop.
Save gokhanbarisaker/3775653 to your computer and use it in GitHub Desktop.
PHP log utility class for more descriptive logging...
<?php
//////////////////////////////////////////////////////////////////
//
// log.php
// Log
//
// Copyright 2012 Gökhan Barış Aker
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
//////////////////////////////////////////////////////////////////
class Log
{
public static function dump_log($log_tag,$log_details)
{
// Generate log file path
$log_root_directory_path = $_SERVER['DOCUMENT_ROOT'];
$log_sub_directory_path = '/logs'.'/';
// $log_sub_directory_path = '/logs/';
$log_file_name = 'test.txt';
$log_file_path = $log_root_directory_path
.$log_sub_directory_path
.$log_file_name;
// Time stamp, one of the most unique and ordered identifier :)
// Possible output is similar to: Mon, 24 Sep 12 12:20:50 +0200
$log_time_stamp = date(DATE_RFC822);
// Generate log string.
// Possible log string output is:
// - [Mon, 24 Sep 12 12:20:50 +0200] LOG_TAG_WILL_BE_HERE: LOG_DESCRIPTION_WILL_BE_HERE @file:"/Applications/MAMP/htdocs/caller_php_file.php", object:"caller class", line:93
$log_string = "\n- [$log_time_stamp]\t$log_tag:\t$log_details\t@".Log::backtrace_caller();
// Open log file in append mode
Log::make_path(dirname($log_file_path));
$log_file = fopen($log_file_path, 'a') or die("can't create log file: $log_file_path");
// Write (dump) log to file
fputs($log_file, $log_string);
// Close log file
fclose($log_file);
}
protected static function backtrace_caller($backtrace_level = 1)
{
// Fetch debug backtrace stack
$trace = debug_backtrace();
// Extract file, line, class data
// from from stack with given level
$file = $trace[$backtrace_level]['file'];
$line = $trace[$backtrace_level]['line'];
$object = $trace[$backtrace_level]['object'];
// If we are at the root class
if ($object == null)
{
// Define it as root instead of '' or null.
$object = 'root';
}
else if(is_object($object))
{
// Get objects class, if it is an object.
// it is, isn't it. it must be...
$object = get_class($object);
}
return "file:\"".$file."\", object:\"".$object."\", line:".$line;
}
protected static function make_path($path)
{
// Test if path extist
if(is_dir($path) || file_exists($path))
{
return;
}
else
{
// Create it
mkdir($path, 0777, true);
}
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment