Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
28.57% |
6 / 21 |
|
50.00% |
5 / 10 |
CRAP | |
0.00% |
0 / 1 |
| Log | |
28.57% |
6 / 21 |
|
50.00% |
5 / 10 |
165.77 | |
0.00% |
0 / 1 |
| __construct | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getLogLevel | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| setLogLevel | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| setLogCallback | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| v | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| d | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| w | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| e | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| log | |
33.33% |
1 / 3 |
|
0.00% |
0 / 1 |
5.67 | |||
| defaultLogCallback | |
0.00% |
0 / 10 |
|
0.00% |
0 / 1 |
90 | |||
| 1 | <?php |
| 2 | namespace Ably; |
| 3 | |
| 4 | /** |
| 5 | * Ably logger, this is a static class |
| 6 | */ |
| 7 | class Log { |
| 8 | const NONE = 0; |
| 9 | const ERROR = 1; |
| 10 | const WARNING = 2; |
| 11 | const DEBUG = 3; |
| 12 | const VERBOSE = 4; |
| 13 | |
| 14 | protected static $logLevel; |
| 15 | protected static $logCallback; |
| 16 | |
| 17 | private function __construct() { |
| 18 | // this is a static class! |
| 19 | } |
| 20 | |
| 21 | /** |
| 22 | * @return integer currently set log level |
| 23 | */ |
| 24 | public static function getLogLevel() { |
| 25 | return self::$logLevel; |
| 26 | } |
| 27 | |
| 28 | /** |
| 29 | * Sets the log level |
| 30 | * @param integer $loglevel one of the log level constants, e.g. Log::DEBUG |
| 31 | */ |
| 32 | public static function setLogLevel( $logLevel ) { |
| 33 | self::$logLevel = $logLevel; |
| 34 | } |
| 35 | |
| 36 | /** |
| 37 | * Sets a custom logging function that will be called in place of self::log() |
| 38 | * @param function|null $function Custom function or leave empty to revert to default |
| 39 | */ |
| 40 | public static function setLogCallback( $function = null ) { |
| 41 | self::$logCallback = $function; |
| 42 | } |
| 43 | |
| 44 | /** |
| 45 | * Log verbose level information. |
| 46 | * @param mixed ... Any number of variables or messages to log |
| 47 | */ |
| 48 | public static function v(/*...*/) { |
| 49 | self::log( Log::VERBOSE, func_get_args() ); |
| 50 | } |
| 51 | |
| 52 | /** |
| 53 | * Log debug level information. |
| 54 | * @param mixed ... Any number of variables or messages to log |
| 55 | */ |
| 56 | public static function d(/*...*/) { |
| 57 | self::log( Log::DEBUG, func_get_args() ); |
| 58 | } |
| 59 | |
| 60 | /** |
| 61 | * Log warning level information. |
| 62 | * @param mixed ... Any number of variables or messages to log |
| 63 | */ |
| 64 | public static function w(/*...*/) { |
| 65 | self::log( Log::WARNING, func_get_args() ); |
| 66 | } |
| 67 | |
| 68 | /** |
| 69 | * Log error level information. |
| 70 | * @param mixed ... Any number of variables or messages to log |
| 71 | */ |
| 72 | public static function e(/*...*/) { |
| 73 | self::log( Log::ERROR, func_get_args() ); |
| 74 | } |
| 75 | |
| 76 | /** |
| 77 | * Logs provided message if log level matches requested level. |
| 78 | * Calls either built-in function or a custom callback if provided |
| 79 | * @param integer $level Log level |
| 80 | * @param array $args arguments to dump |
| 81 | */ |
| 82 | public static function log( $level, $args ) { |
| 83 | if ( self::$logLevel >= $level ) { |
| 84 | $function = self::$logCallback; |
| 85 | return $function ? $function( $level, $args ) : self::defaultLogCallback( $level, $args ); |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | /** |
| 90 | * The default logging function |
| 91 | */ |
| 92 | private static function defaultLogCallback( $level, $args ) { |
| 93 | $last = count($args) - 1; |
| 94 | |
| 95 | $timestamp = date( "Y-m-d H:i:s\t" ); |
| 96 | |
| 97 | foreach ($args as $i => $arg) { |
| 98 | if (is_string($arg)) { |
| 99 | file_put_contents( 'php://stdout', $timestamp . $arg . ($i == $last ? "\n" : "\t") ); |
| 100 | } |
| 101 | else if (is_bool($arg)) { |
| 102 | file_put_contents( 'php://stdout', $timestamp . ($arg ? 'true' : 'false') . ($i == $last ? "\n" : "\t") ); |
| 103 | } |
| 104 | else if (is_scalar($arg)) { |
| 105 | file_put_contents( 'php://stdout', $timestamp . $arg . ($i == $last ? "\n" : "\t") ); |
| 106 | } |
| 107 | else { |
| 108 | file_put_contents( 'php://stdout', $timestamp . print_r( $arg, true ). "\n" ); |
| 109 | } |
| 110 | } |
| 111 | } |
| 112 | } |