Skip to content

Instantly share code, notes, and snippets.

@teslamint
Last active October 10, 2015 02:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save teslamint/3621424 to your computer and use it in GitHub Desktop.
Save teslamint/3621424 to your computer and use it in GitHub Desktop.
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