-
-
Save geofmureithi-zz/320c34b719076e355947ec4f5dba892f to your computer and use it in GitHub Desktop.
PHP log utility class for more descriptive logging...
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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