| 1 |
<?php |
| 2 |
/** |
| 3 |
* DecaLog logger utilities. |
| 4 |
* |
| 5 |
* @package Features |
| 6 |
* @author Pierre Lannoy <https://pierre.lannoy.fr/>. |
| 7 |
* @since 1.0.0 |
| 8 |
*/ |
| 9 |
|
| 10 |
namespace Decalog\Plugin\Feature; |
| 11 |
|
| 12 |
use Decalog\Plugin\Feature\DLogger; |
| 13 |
use DLMonolog\Logger; |
| 14 |
use Decalog\Plugin\Feature\EventTypes; |
| 15 |
|
| 16 |
|
| 17 |
/** |
| 18 |
* Utilities DecaLog class. |
| 19 |
* |
| 20 |
* This class defines all code necessary to log events with DecaLog. |
| 21 |
* |
| 22 |
* @package Features |
| 23 |
* @author Pierre Lannoy <https://pierre.lannoy.fr/>. |
| 24 |
* @since 1.0.0 |
| 25 |
*/ |
| 26 |
class Log { |
| 27 |
|
| 28 |
/** |
| 29 |
* Initialize the class and set its properties. |
| 30 |
* |
| 31 |
* @since 1.0.0 |
| 32 |
*/ |
| 33 |
public function __construct() { |
| 34 |
} |
| 35 |
|
| 36 |
/** |
| 37 |
* Get a new logger instance. |
| 38 |
* |
| 39 |
* @param string $class The class identifier, see Decalog\API\DLogger::$classes. |
| 40 |
* @param string $name Optional. The name of the component. |
| 41 |
* @param string $version Optional. The version of the component. |
| 42 |
* @param string $test Optional. The handler to bootstrap if specified.. |
| 43 |
* @return DLogger The DecaLog logger instance. |
| 44 |
* @since 1.0.0 |
| 45 |
*/ |
| 46 |
public static function bootstrap( $class, $name = null, $version = null, $test = null ) { |
| 47 |
return new DLogger( $class, $name, $version, $test ); |
| 48 |
} |
| 49 |
|
| 50 |
/** |
| 51 |
* Get a level name. |
| 52 |
* |
| 53 |
* @param integer $level The level value. |
| 54 |
* @return string The level name. |
| 55 |
* @since 1.0.0 |
| 56 |
*/ |
| 57 |
public static function level_name( $level ) { |
| 58 |
$result = 'UNKNOWN'; |
| 59 |
if ( array_key_exists( $level, EventTypes::$level_names ) ) { |
| 60 |
$result = EventTypes::$level_names[ $level ]; |
| 61 |
} |
| 62 |
return $result; |
| 63 |
} |
| 64 |
|
| 65 |
/** |
| 66 |
* Get the levels list. |
| 67 |
* |
| 68 |
* @param integer $minimal Optional. The minimal level to add. |
| 69 |
* @param boolean $warn Optional. Adds a warning to the DEBUG string. |
| 70 |
* @return array The level list. |
| 71 |
* @since 1.0.0 |
| 72 |
*/ |
| 73 |
public static function get_levels( $minimal = Logger::DEBUG, $warn = false ) { |
| 74 |
$result = []; |
| 75 |
$warning = ' (' . decalog_esc_html__( 'use this only if you know what you do', 'decalog' ) . ')'; |
| 76 |
foreach ( EventTypes::$level_names as $key => $name ) { |
| 77 |
if ( $key >= $minimal ) { |
| 78 |
if ( Logger::DEBUG == $key && $warn) { |
| 79 |
$result[] = [ $key, $name . $warning, EventTypes::$level_texts[ strtolower( $name ) ] . $warning ]; |
| 80 |
} else { |
| 81 |
$result[] = [ $key, $name, EventTypes::$level_texts[ strtolower( $name ) ] ]; |
| 82 |
} |
| 83 |
} |
| 84 |
} |
| 85 |
return array_reverse( $result ); |
| 86 |
} |
| 87 |
|
| 88 |
} |
| 89 |
|