Skip to content

Instantly share code, notes, and snippets.

@teslamint teslamint/log.php
Last active Oct 10, 2015

Embed
What would you like to do?
custom log function for CodeIgniter
<?php
class example extends CI_Controller {
/**
* leave log message
*
* @access private
* @param string $func (default: __FUNCTION__)
* @param string $level (default: 'debug')
* @param string $message (default: '')
* @return void
*/
private function __log() {
$num_arg = func_num_args();
$trace = debug_backtrace( DEBUG_BACKTRACE_IGNORE_ARGS | DEBUG_BACKTRACE_PROVIDE_OBJECT );
$func = $trace[1]['function'];
$line = $trace[0]['line'];
$object = $trace[1]['object'];
if ( is_object( $object ) )
$class = get_class( $object );
else
$class = get_called_class();
$level = 'debug';
$message = '';
switch ( $num_arg ) {
case 1:
$message = func_get_arg( 0 );
break;
case 2:
$message = func_get_arg( 1 );
$func = method_exists( $this, func_get_arg( 0 ) ) ? func_get_arg( 0 ) : $func;
$level = ( strcasecmp( $func, func_get_arg( 0 ) ) === 0 ) ? $level : func_get_arg( 0 );
break;
default:
list( $func, $level, $message ) = func_get_args();
if ( ! method_exists( $class, $func ) ) {
// maybe swapped
$temp = $func;
$func = $level;
$level = $func;
}
}
$message = str_replace( PHP_EOL, '', $message );
log_message( $level, "{$class}::{$func}({$line}): {$message}" );
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.