-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
52e3ecd
commit 29406c4
Showing
1 changed file
with
73 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
<?php | ||
/* | ||
Plugin Name: Floggit | ||
Plugin URI: https://www.designerandgeek.com | ||
Description: Easier logging to debug.log. | ||
Version: 1.1 | ||
Author: Jørn Støylen | ||
Author URI: https://www.designerandgeek.com | ||
*/ | ||
|
||
class FloggitClass { | ||
|
||
// Have we already emptied the log file? | ||
private static $flogged = false; | ||
|
||
/** | ||
* Easier logging to debug.log. | ||
* | ||
* Just a wrapper for error_log that pretty prints arrays and objects | ||
* and includes the filename and line number where the logging happened. | ||
* | ||
* Accepts multiple parameters, prints one line for each parameter. | ||
* | ||
* @param ...$logs One or more things to log. | ||
*/ | ||
public static function loggit ( ...$logs ) | ||
{ | ||
$backtrace = debug_backtrace(); | ||
$level = 0; | ||
while(basename(__FILE__)==basename($backtrace[$level]['file'])) { | ||
$level++; | ||
} | ||
$backtrace = debug_backtrace()[$level]; | ||
$bt_file = basename($backtrace['file']); | ||
$bt_line = $backtrace['line']; | ||
$bt_string = "[$bt_file:$bt_line] "; | ||
foreach( $logs as $log ) { | ||
if ( is_array( $log ) || is_object( $log ) ) { | ||
error_log( $bt_string . PHP_EOL . print_r( $log, true ) ); | ||
} else { | ||
error_log( $bt_string . $log ); | ||
} | ||
} | ||
} | ||
|
||
|
||
/** | ||
* Fresh loggit: Empty debug.log first, then loggit(). | ||
* | ||
* Since this is an extra step in the call stack, set backtrace level to 2. | ||
* | ||
* @param ...$logs One or more things to log. | ||
*/ | ||
public static function floggit( ...$logs ) | ||
{ | ||
if (self::$flogged==false) { | ||
unlink( WP_CONTENT_DIR . '/debug.log' ); | ||
self::$flogged = true; | ||
} | ||
loggit( ...$logs ); | ||
} | ||
} | ||
|
||
function loggit(...$logs) | ||
{ | ||
return FloggitClass::loggit(...$logs); | ||
} | ||
|
||
|
||
function floggit(...$logs) | ||
{ | ||
return FloggitClass::floggit(...$logs); | ||
} |