├── .gitignore ├── README ├── lib ├── dd_logging_Util.php ├── dd_logging_SimpleFileLogger.php ├── dd_logging_ILogger.php ├── dd_logging_LogFactory.php ├── dd_logging_AbstractLogger.php └── dd_logging_SimpleLogger.php └── LICENSE /.gitignore: -------------------------------------------------------------------------------- 1 | .buildpath 2 | .project 3 | .settings 4 | ._* 5 | .*.swp 6 | build 7 | dist 8 | repos 9 | -------------------------------------------------------------------------------- /README: -------------------------------------------------------------------------------- 1 | Dragonfly Development PHP Logging Library 2 | http://dflydev.com/d2code/php/dd-logging-php/ 3 | 4 | 5 | dd-logging provides a generic logging interface enabling PHP developers 6 | to enjoy some of the benefits provided Java users by way of the Apache 7 | Commons Logging library. 8 | 9 | 10 | Inspired by Apache Commons Logging 11 | http://commons.apache.org/logging/ 12 | -------------------------------------------------------------------------------- /lib/dd_logging_Util.php: -------------------------------------------------------------------------------- 1 | 19 | -------------------------------------------------------------------------------- /lib/dd_logging_SimpleFileLogger.php: -------------------------------------------------------------------------------- 1 | getFileHandle(); 16 | fwrite($fh, '[' . date('r') . '] ' . $message . "\n"); 17 | } 18 | protected function getFileHandle() { 19 | if ( $this->fh === null ) { 20 | if ( self::$FILE_HANDLE === null ) { 21 | self::$FILE_HANDLE = fopen(self::$FILE_NAME, 'a'); 22 | } 23 | $this->fh = self::$FILE_HANDLE; 24 | self::$FILE_HANDLE_COUNT++; 25 | } 26 | return self::$FILE_HANDLE; 27 | } 28 | public function __destruct() { 29 | if ( $this->fh !== null ) { 30 | if ( --self::$FILE_HANDLE_COUNT == 0 ) { 31 | if ( self::$FILE_HANDLE === null ) { 32 | fclose(self::$FILE_HANDLE); 33 | } 34 | self::$FILE_HANDLE = $this->fh = null; 35 | } 36 | } 37 | } 38 | } 39 | 40 | ?> 41 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright (c) 2010, Dragonfly Development Inc 2 | All rights reserved. 3 | 4 | Redistribution and use in source and binary forms, with or without 5 | modification, are permitted provided that the following conditions are met: 6 | 7 | * Redistributions of source code must retain the above copyright notice, 8 | this list of conditions and the following disclaimer. 9 | * Redistributions in binary form must reproduce the above copyright 10 | notice, this list of conditions and the following disclaimer in the 11 | documentation and/or other materials provided with the distribution. 12 | * Neither the name of Dragonfly Development Inc nor the names of its 13 | contributors may be used to endorse or promote products derived from 14 | this software without specific prior written permission. 15 | 16 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 17 | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19 | ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 20 | LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 21 | CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 22 | SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 23 | INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 24 | CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 25 | ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 26 | POSSIBILITY OF SUCH DAMAGE. 27 | -------------------------------------------------------------------------------- /lib/dd_logging_ILogger.php: -------------------------------------------------------------------------------- 1 | 97 | -------------------------------------------------------------------------------- /lib/dd_logging_LogFactory.php: -------------------------------------------------------------------------------- 1 | 0 ) { array_shift($args); } 59 | $reflectionObject = new ReflectionClass($className); 60 | return $reflectionObject->newInstanceArgs($args); 61 | } 62 | 63 | /** 64 | * Set the default logger 65 | * @param $className 66 | */ 67 | static public function SET_DEFAULT_LOGGER($className) { 68 | self::$CLASS_NAME = $className; 69 | } 70 | 71 | } 72 | 73 | ?> 74 | -------------------------------------------------------------------------------- /lib/dd_logging_AbstractLogger.php: -------------------------------------------------------------------------------- 1 | implementsInterface('dd_logging_ILogger') ) and 38 | $testClass !== 'ReflectionClass' and 39 | $testClass !== 'dd_logging_LogFactory' 40 | ) { 41 | $className = $testClass; 42 | break; 43 | } 44 | } 45 | } 46 | 47 | 48 | } 49 | $this->className = $className; 50 | $this->modifiedClassName = implode('.', explode('_', $this->className)); 51 | } 52 | 53 | 54 | protected static $LEVEL_NAMES = array( 55 | E_ERROR => 'E_ERROR', 56 | E_WARNING => 'E_WARNING', 57 | E_PARSE => 'E_PARSE', 58 | E_NOTICE => 'E_NOTICE', 59 | E_CORE_ERROR => 'E_CORE_ERROR', 60 | E_CORE_WARNING => 'E_CORE_WARNING', 61 | E_COMPILE_ERROR => 'E_COMPILE_ERROR', 62 | E_COMPILE_WARNING => 'E_COMPILE_WARNING', 63 | E_USER_ERROR => 'E_USER_ERROR', 64 | E_USER_WARNING => 'E_USER_WARNING', 65 | E_USER_NOTICE => 'E_USER_NOTICE', 66 | E_DEPRECATED => 'E_DEPRECATED', 67 | E_USER_DEPRECATED => 'E_USER_DEPRECATED', 68 | E_RECOVERABLE_ERROR => 'E_RECOVERABLE_ERROR', 69 | E_STRICT => 'E_STRICT', 70 | ); 71 | 72 | /** 73 | * (non-PHPdoc) 74 | * @see dd_logging_ILogger::handleException() 75 | */ 76 | public function handleException(Exception $e) { 77 | $this->error($e->getMessage()); 78 | foreach ( $e->getTrace() as $trace ) { 79 | $this->error($trace); 80 | } 81 | } 82 | 83 | /** 84 | * (non-PHPdoc) 85 | * @see dd_logging_ILogger::handleError() 86 | */ 87 | public function handleError($errno, $errstr, $errfile, $errline, $errcontext) { 88 | 89 | $message = '[' . self::$LEVEL_NAMES[$errno] . '] ' . $errstr . ' in ' . $errfile . ' at line ' . $errline; 90 | 91 | $die = false; 92 | $method = null; 93 | 94 | switch($errno) { 95 | 96 | case E_COMPILE_ERROR: 97 | case E_ERROR: 98 | case E_CORE_ERROR: 99 | case E_USER_ERROR: 100 | $method = 'fatal'; 101 | $die = true; 102 | break; 103 | 104 | case E_PARSE: 105 | 106 | $method = 'error'; 107 | $die = true; 108 | 109 | break; 110 | 111 | case E_WARNING: 112 | case E_CORE_WARNING: 113 | case E_COMPILE_WARNING: 114 | case E_USER_WARNING: 115 | 116 | $method = 'warn'; 117 | 118 | break; 119 | 120 | case E_NOTICE: 121 | case E_USER_NOTICE: 122 | 123 | $method = 'warn'; 124 | 125 | break; 126 | 127 | case E_STRICT: 128 | 129 | //$method = 'warn'; 130 | 131 | break; 132 | 133 | 134 | $method = 'info'; 135 | 136 | break; 137 | 138 | case E_DEPRECATED: 139 | case E_USER_DEPRECATED: 140 | case E_RECOVERABLE_ERROR: 141 | 142 | $method = 'warn'; 143 | 144 | break; 145 | 146 | default: 147 | 148 | $method = 'debug'; 149 | 150 | break; 151 | 152 | } 153 | 154 | if ( $method !== null ) { 155 | $this->$method($message); 156 | } 157 | 158 | if ( $die ) { die(); } 159 | 160 | } 161 | 162 | } 163 | 164 | ?> 165 | -------------------------------------------------------------------------------- /lib/dd_logging_SimpleLogger.php: -------------------------------------------------------------------------------- 1 | array( 19 | 'warn' => true, 20 | 'error' => true, 21 | 'fatal' => true, 22 | ), 23 | ); 24 | 25 | /** 26 | * Configure the level settings for a specific class. 27 | * @param $className 28 | * @param $config 29 | */ 30 | static public function CONFIGURE($className, $config = null) { 31 | if ( ! isset(self::$CLASS_CONFIGURATION[$className]) ) { 32 | self::$CLASS_CONFIGURATION[$className] = array(); 33 | } 34 | foreach ( $config as $level => $enabled ) { 35 | if ( $enabled === null ) { 36 | unset(self::$CLASS_CONFIGURATION[$className][$level]); 37 | } else { 38 | self::$CLASS_CONFIGURATION[$className][$level] = $enabled; 39 | } 40 | } 41 | } 42 | 43 | /** 44 | * Configure the default level settings 45 | * @param $config 46 | */ 47 | static public function CONFIGURE_DEFAULTS($config = null) { 48 | self::CONFIGURE(self::$CLASS_DEFAULT, $config); 49 | } 50 | 51 | /** 52 | * Constructor 53 | * @param $className 54 | * @param $config 55 | */ 56 | public function __construct($className = null, $config = null) { 57 | parent::__construct($className); 58 | if ( $config ) { 59 | self::CONFIGURE($this->className, $config); 60 | } 61 | } 62 | 63 | /** 64 | * (non-PHPdoc) 65 | * @see dd_logging_ILogger::trace() 66 | */ 67 | public function trace($message) { 68 | if ( $this->isEnabled('trace') ) 69 | $this->logMessage('trace', $message); 70 | } 71 | 72 | /** 73 | * (non-PHPdoc) 74 | * @see dd_logging_ILogger::debug() 75 | */ 76 | public function debug($message) { 77 | if ( $this->isEnabled('debug') ) 78 | $this->logMessage('debug', $message); 79 | } 80 | 81 | /** 82 | * (non-PHPdoc) 83 | * @see dd_logging_ILogger::info() 84 | */ 85 | public function info($message) { 86 | if ( $this->isEnabled('info') ) 87 | $this->logMessage('info', $message); 88 | } 89 | 90 | /** 91 | * (non-PHPdoc) 92 | * @see dd_logging_ILogger::warn() 93 | */ 94 | public function warn($message) { 95 | if ( $this->isEnabled('warn') ) 96 | $this->logMessage('warn', $message); 97 | } 98 | 99 | /** 100 | * (non-PHPdoc) 101 | * @see dd_logging_ILogger::error() 102 | */ 103 | public function error($message) { 104 | if ( $this->isEnabled('error') ) 105 | $this->logMessage('error', $message); 106 | } 107 | 108 | /** 109 | * (non-PHPdoc) 110 | * @see dd_logging_ILogger::fatal() 111 | */ 112 | public function fatal($message) { 113 | if ( $this->isEnabled('fatal') ) 114 | $this->logMessage('fatal', $message); 115 | } 116 | 117 | /** 118 | * (non-PHPdoc) 119 | * @see dd_logging_ILogger::isTraceEnabled() 120 | */ 121 | public function isTraceEnabled() { 122 | return $this->isEnabled('trace'); 123 | } 124 | 125 | /** 126 | * (non-PHPdoc) 127 | * @see dd_logging_ILogger::isDebugEnabled() 128 | */ 129 | public function isDebugEnabled() { 130 | return $this->isEnabled('debug'); 131 | } 132 | 133 | /** 134 | * (non-PHPdoc) 135 | * @see dd_logging_ILogger::isInfoEnabled() 136 | */ 137 | public function isInfoEnabled() { 138 | return $this->isEnabled('info'); 139 | } 140 | 141 | /** 142 | * (non-PHPdoc) 143 | * @see dd_logging_ILogger::isDebugEnabled() 144 | */ 145 | public function isWarnEnabled() { 146 | return $this->isEnabled('warn'); 147 | } 148 | 149 | /** 150 | * (non-PHPdoc) 151 | * @see dd_logging_ILogger::isDebugEnabled() 152 | */ 153 | public function isErrorEnabled() { 154 | return $this->isEnabled('error'); 155 | } 156 | 157 | /** 158 | * (non-PHPdoc) 159 | * @see dd_logging_ILogger::isFatalEnabled() 160 | */ 161 | public function isFatalEnabled() { 162 | return $this->isEnabled('fatal'); 163 | } 164 | 165 | /** 166 | * Is logging enabled for a specific level? 167 | * @param $level 168 | * @param $default 169 | * @return bool 170 | */ 171 | protected function isEnabled($level) { 172 | if ( isset(self::$CLASS_CONFIGURATION[$this->className][$level]) ) 173 | return self::$CLASS_CONFIGURATION[$this->className][$level]; 174 | if ( isset(self::$CLASS_CONFIGURATION[self::$CLASS_DEFAULT][$level]) ) 175 | return self::$CLASS_CONFIGURATION[self::$CLASS_DEFAULT][$level]; 176 | return false; 177 | } 178 | 179 | /** 180 | * Log a message 181 | * @param $level 182 | * @param $message 183 | */ 184 | protected function logMessage($level, $message) { 185 | $this->writeLogMessage($this->modifiedClassName . ' - ' . $level . ': ' . $message); 186 | } 187 | 188 | /** 189 | * Write a log message. 190 | * @param $message 191 | */ 192 | protected function writeLogMessage($message) { 193 | error_log($message); 194 | } 195 | 196 | } 197 | 198 | ?> 199 | --------------------------------------------------------------------------------