Skip to content

Commit

Permalink
Make log_event() calls print to stdout when running from CLI
Browse files Browse the repository at this point in the history
This simple modification to the logging_api allows command-line scripts
such as send_emails.php to easily provide detailed information about
their operations, provided that the appropriate log_level is set in
config_inc.php.

This output can then be redirected to log files, etc.

Fixes #15382
  • Loading branch information
dregad committed Jan 17, 2013
1 parent ad5f2f7 commit 0e2c6b4
Showing 1 changed file with 9 additions and 4 deletions.
13 changes: 9 additions & 4 deletions core/logging_api.php
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
* Log an event * Log an event
* @param int $p_level Valid debug log level * @param int $p_level Valid debug log level
* @param string|array $p_msg Either a string, or an array structured as (string,execution time) * @param string|array $p_msg Either a string, or an array structured as (string,execution time)
* @param object $p_backtrace [Optional] debug_backtrace() stack to use * @param object $p_backtrace [Optional] debug_backtrace() stack to use
* @return null * @return null
*/ */
function log_event( $p_level, $p_msg, $p_backtrace = null ) { function log_event( $p_level, $p_msg, $p_backtrace = null ) {
Expand Down Expand Up @@ -99,14 +99,16 @@ function log_event( $p_level, $p_msg, $p_backtrace = null ) {
if ( is_blank( $t_log_destination ) ) { if ( is_blank( $t_log_destination ) ) {
$t_destination = ''; $t_destination = '';
} else { } else {
# Use @ to avoid error when there is no delimiter in log destination
@list( $t_destination, $t_modifiers ) = explode( ':', $t_log_destination, 2 ); @list( $t_destination, $t_modifiers ) = explode( ':', $t_log_destination, 2 );
} }


$t_php_event = $t_now . ' ' . $t_level . ' ' . $s_msg;

switch( $t_destination ) { switch( $t_destination ) {
case 'none': case 'none':
break; break;
case 'file': case 'file':
$t_php_event = $t_now . ' ' . $t_level . ' ' . $s_msg;
error_log( $t_php_event . PHP_EOL, 3, $t_modifiers ); error_log( $t_php_event . PHP_EOL, 3, $t_modifiers );
break; break;
case 'page': case 'page':
Expand All @@ -124,17 +126,20 @@ function log_event( $p_level, $p_msg, $p_backtrace = null ) {
if( $firephp === null ) { if( $firephp === null ) {
$firephp = FirePHP::getInstance(true); $firephp = FirePHP::getInstance(true);
} }
$t_php_event = $t_now . ' ' . $t_level . ' ' . $s_msg;
$firephp->log( $p_msg, $t_php_event ); $firephp->log( $p_msg, $t_php_event );
return; return;
} }
// if firebug is not available, fall through // if firebug is not available, fall through
default: default:
# use default PHP error log settings # use default PHP error log settings
$t_php_event = $t_now . ' ' . $t_level . ' ' . $s_msg;
error_log( $t_php_event . PHP_EOL ); error_log( $t_php_event . PHP_EOL );
break; break;
} }

# If running from command line, echo log event to stdout
if( $t_destination != 'none' && php_sapi_name() == 'cli' ) {
echo $t_php_event . PHP_EOL;
}
} }


function log_print_to_page() { function log_print_to_page() {
Expand Down

0 comments on commit 0e2c6b4

Please sign in to comment.