| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  | /* | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  |  * This file is part of Yolk - Gamer Network's PHP Framework. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  * Copyright (c) 2014 Gamer Network Ltd. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  *  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  * Distributed under the MIT License, a copy of which is available in the | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  * LICENSE file that was bundled with this package, or online at: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  * https://github.com/gamernetwork/yolk-logger | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | namespace yolk\log; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | use yolk\contracts\log\LoggerFactory; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | use yolk\contracts\log\Logger; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 16 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 17 |  |  | class GenericLoggerFactory implements LoggerFactory { | 
            
                                                                        
                            
            
                                    
            
            
                | 18 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 19 |  |  | 	protected $classes; | 
            
                                                                        
                            
            
                                    
            
            
                | 20 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  | 	public function __construct( array $classes = [] ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  | 		$this->classes = $classes + [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  | 			'php'    => __NAMESPACE__. '\adapter\PHPLogger', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  | 			'file'   => __NAMESPACE__. '\adapter\FileLogger', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  | 			'stderr' => __NAMESPACE__. '\adapter\StdErrLogger', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  | 			'stdout' => __NAMESPACE__. '\adapter\StdOutLogger', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  | 			'syslog' => __NAMESPACE__. '\adapter\SysLogger', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  | 			'null'   => __NAMESPACE__. '\adapter\NullLogger', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  | 		]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |  | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 32 |  |  | 	} | 
            
                                                                        
                            
            
                                    
            
            
                | 33 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  | 	public function create( $config ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  | 		$config = $this->checkConfig($config); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  | 		$factories = [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  | 			'php'    => 'createPHPLogger', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  | 			'file'   => 'createFileLogger', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  | 			'syslog' => 'createSysLogger', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  | 			'stderr' => 'createStdErrLogger', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  | 			'stdout' => 'createStdOutLogger', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  | 			'null'   => 'createNullLogger', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  | 		]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  | 		if( empty($factories[$config['type']]) ) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  | 			throw new Exception("Invalid logger type: {$config['type']}"); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  | 		$factory = $factories[$config['type']]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  | 		if( $config['type'] == 'file' ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  | 			$config = $config + ['file' => '']; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  | 			return $this->$factory($config['file'], $config['threshold']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  | 		elseif( $config['type'] == 'syslog' ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  | 			$config = $config + ['prefix' => '']; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  | 			return $this->$factory($config['prefix'], $config['threshold']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  | 		return $this->$factory($config['threshold']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |  | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 63 |  |  | 	} | 
            
                                                                        
                            
            
                                    
            
            
                | 64 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 65 |  |  | 	/** | 
            
                                                                        
                            
            
                                    
            
            
                | 66 |  |  | 	 * Create a logger that outputs to the standard PHP error log. | 
            
                                                                        
                            
            
                                    
            
            
                | 67 |  |  | 	 * @param  integer $threshold | 
            
                                                                        
                            
            
                                    
            
            
                | 68 |  |  | 	 * @return Logger | 
            
                                                                        
                            
            
                                    
            
            
                | 69 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  | 	public function createPHPLogger( $threshold = LogLevel::WARNING ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  | 		return $this->createLogger('php') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  | 			->setThreshold($threshold); | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 73 |  |  | 	} | 
            
                                                                        
                            
            
                                    
            
            
                | 74 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 75 |  |  | 	/** | 
            
                                                                        
                            
            
                                    
            
            
                | 76 |  |  | 	 * Create a logger that outputs to the specified file. | 
            
                                                                        
                            
            
                                    
            
            
                | 77 |  |  | 	 * @param  integer $threshold | 
            
                                                                        
                            
            
                                    
            
            
                | 78 |  |  | 	 * @return Logger | 
            
                                                                        
                            
            
                                    
            
            
                | 79 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  | 	public function createFileLogger( $file, $threshold = LogLevel::WARNING ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  | 		return $this->createLogger('file', $file) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  | 			->setThreshold($threshold); | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 83 |  |  | 	} | 
            
                                                                        
                            
            
                                    
            
            
                | 84 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 85 |  |  | 	/** | 
            
                                                                        
                            
            
                                    
            
            
                | 86 |  |  | 	 * Create a logger that outputs to Syslog. | 
            
                                                                        
                            
            
                                    
            
            
                | 87 |  |  | 	 * @param  integer $threshold | 
            
                                                                        
                            
            
                                    
            
            
                | 88 |  |  | 	 * @return Logger | 
            
                                                                        
                            
            
                                    
            
            
                | 89 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  | 	public function createSysLogger( $prefix = '', $threshold = LogLevel::WARNING ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  | 		return $this->createLogger('syslog', $prefix) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  | 			->setThreshold($threshold); | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 93 |  |  | 	} | 
            
                                                                        
                            
            
                                    
            
            
                | 94 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 95 |  |  | 	/** | 
            
                                                                        
                            
            
                                    
            
            
                | 96 |  |  | 	 * Create a logger that outputs to StdOut. | 
            
                                                                        
                            
            
                                    
            
            
                | 97 |  |  | 	 * The StdOut logger is only available when running via the command-line. | 
            
                                                                        
                            
            
                                    
            
            
                | 98 |  |  | 	 * @param  integer $threshold | 
            
                                                                        
                            
            
                                    
            
            
                | 99 |  |  | 	 * @return Logger | 
            
                                                                        
                            
            
                                    
            
            
                | 100 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  | 	public function createStdOutLogger( $threshold = LogLevel::WARNING ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  | 		return $this->createLogger('stdout') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  | 			->setThreshold($threshold); | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 104 |  |  | 	} | 
            
                                                                        
                            
            
                                    
            
            
                | 105 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 106 |  |  | 	/** | 
            
                                                                        
                            
            
                                    
            
            
                | 107 |  |  | 	 * Create a logger that outputs to StdErr. | 
            
                                                                        
                            
            
                                    
            
            
                | 108 |  |  | 	 * The StdErr logger is only available when running via the command-line. | 
            
                                                                        
                            
            
                                    
            
            
                | 109 |  |  | 	 * @param  integer $threshold | 
            
                                                                        
                            
            
                                    
            
            
                | 110 |  |  | 	 * @return Logger | 
            
                                                                        
                            
            
                                    
            
            
                | 111 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  | 	public function createStdErrLogger( $threshold = LogLevel::WARNING ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  | 		return $this->createLogger('stderr') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  | 			->setThreshold($threshold); | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 115 |  |  | 	} | 
            
                                                                        
                            
            
                                    
            
            
                | 116 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 117 |  |  | 	/** | 
            
                                                                        
                            
            
                                    
            
            
                | 118 |  |  | 	 * Create a logger that performs no output. | 
            
                                                                        
                            
            
                                    
            
            
                | 119 |  |  | 	 * @param  integer $threshold | 
            
                                                                        
                            
            
                                    
            
            
                | 120 |  |  | 	 * @return Logger | 
            
                                                                        
                            
            
                                    
            
            
                | 121 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  | 	public function createNullLogger( $threshold = LogLevel::WARNING ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  | 		return $this->createLogger('null') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  | 			->setThreshold($threshold); | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 125 |  |  | 	} | 
            
                                                                        
                            
            
                                    
            
            
                | 126 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 127 |  |  | 	/** | 
            
                                                                        
                            
            
                                    
            
            
                | 128 |  |  | 	 * Ensure the configuration is in a consistant format. | 
            
                                                                        
                            
            
                                    
            
            
                | 129 |  |  | 	 * @param  array|string $config | 
            
                                                                        
                            
            
                                    
            
            
                | 130 |  |  | 	 * @return array | 
            
                                                                        
                            
            
                                    
            
            
                | 131 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  | 	protected function checkConfig( $config ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  | 		// if config isn't an array then treat it as the log type | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  | 		// - some adapters don't require configuration options | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  | 		if( !is_array($config) ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  | 			$config = [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  | 				'type' => $config, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  | 			]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  | 		$config = $config + [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 |  |  | 			'type'   => '', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 |  |  | 			'threshold' => LogLevel::WARNING, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 |  |  | 		]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 147 |  |  | 		return $config; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 148 |  |  |  | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 149 |  |  | 	} | 
            
                                                                        
                            
            
                                    
            
            
                | 150 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 151 |  |  | 	/** | 
            
                                                                        
                            
            
                                    
            
            
                | 152 |  |  | 	 * Create a Logger instance. | 
            
                                                                        
                            
            
                                    
            
            
                | 153 |  |  | 	 * @param  string $type | 
            
                                                                        
                            
            
                                    
            
            
                | 154 |  |  | 	 * @param  mixed $arg1 | 
            
                                                                        
                            
            
                                    
            
            
                | 155 |  |  | 	 * @param  mixed $arg2 | 
            
                                                                        
                            
            
                                    
            
            
                | 156 |  |  | 	 * @param  mixed $arg3 | 
            
                                                                        
                            
            
                                    
            
            
                | 157 |  |  | 	 * @return Logger | 
            
                                                                        
                            
            
                                    
            
            
                | 158 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 159 |  |  | 	protected function createLogger( $type, $arg1 = null, $arg2 = null, $arg3 = null ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 160 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 161 |  |  | 		// check the class exists before we try and use it | 
            
                                                                                                            
                            
            
                                    
            
            
                | 162 |  |  | 		if( !class_exists($class = $this->classes[$type]) ) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 163 |  |  | 			throw new Exception(sprintf("Class '\\%s' not found for logger type '%s'", $class, $type)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 164 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 165 |  |  | 		// create a new instance | 
            
                                                                                                            
                            
            
                                    
            
            
                | 166 |  |  | 		$log = new $class($arg1, $arg2, $arg3); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 167 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 168 |  |  | 		// check the instance implements the correct interface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 169 |  |  | 		if( !$log instanceof Logger ) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 170 |  |  | 			throw new Exception(sprintf("Class '\\%s' does not implement yolk\contracts\log\Logger", $class)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 171 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 172 |  |  | 		return $log; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 173 |  |  |  | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 174 |  |  | 	} | 
            
                                                        
            
                                    
            
            
                | 175 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 176 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 177 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 178 |  |  | // EOF |