Overview

Namespaces

  • OpenCloud
    • Autoscale
      • Resource
    • CloudMonitoring
      • Exception
      • Resource
    • Common
      • Collection
      • Constants
      • Exceptions
      • Http
        • Message
      • Identity
      • Log
      • Service
    • Compute
      • Constants
      • Exception
      • Resource
    • Database
      • Resource
    • DNS
      • Resource
    • LoadBalancer
      • Resource
    • ObjectStore
      • Constants
      • Exception
      • Resource
      • Upload
    • Orchestration
    • Queues
      • Exception
      • Resource
    • Volume
      • Resource
  • PHP

Classes

  • AbstractLogger
  • Logger
  • LogLevel

Interfaces

  • LoggerInterface
  • Overview
  • Namespace
  • Class
  • Tree
  • Download
  1: <?php
  2: /**
  3:  * PHP OpenCloud library.
  4:  * 
  5:  * @copyright 2013 Rackspace Hosting, Inc. See LICENSE for information.
  6:  * @license   https://www.apache.org/licenses/LICENSE-2.0
  7:  * @author    Jamie Hannaford <jamie.hannaford@rackspace.com>
  8:  */
  9: 
 10: namespace OpenCloud\Common\Log;
 11: 
 12: use OpenCloud\Common\Exceptions\LoggingException;
 13: 
 14: /**
 15:  * Basic logger for OpenCloud which extends FIG's PSR-3 standard logger.
 16:  * 
 17:  * @link https://github.com/php-fig/log
 18:  */
 19: class Logger extends AbstractLogger
 20: {   
 21:     /**
 22:      * Is this debug class enabled or not?
 23:      * 
 24:      * @var bool
 25:      */
 26:     private $enabled;
 27:     
 28:     /**
 29:      * These are the levels which will always be outputted - regardless of 
 30:      * user-imposed settings.
 31:      * 
 32:      * @var array 
 33:      */
 34:     private $urgentLevels = array(
 35:         LogLevel::EMERGENCY,
 36:         LogLevel::ALERT,
 37:         LogLevel::CRITICAL
 38:     );
 39:     
 40:     /**
 41:      * Logging options.
 42:      * 
 43:      * @var array
 44:      */
 45:     private $options = array(
 46:         'outputToFile' => false,
 47:         'logFile'      => null,
 48:         'dateFormat'   => 'd/m/y H:I',
 49:         'delimeter'    => ' - '
 50:     );
 51: 
 52:     public function __construct($enabled = false)
 53:     {
 54:         $this->enabled = $enabled;
 55:     }
 56: 
 57:     public static function newInstance()
 58:     {
 59:         return new static();
 60:     }
 61: 
 62:     /**
 63:      * Determines whether a log level needs to be outputted.
 64:      * 
 65:      * @param  string $logLevel
 66:      * @return bool
 67:      */
 68:     private function outputIsUrgent($logLevel)
 69:     {
 70:         return in_array($logLevel, $this->urgentLevels);
 71:     }
 72:     
 73:     /**
 74:      * Interpolates context values into the message placeholders.
 75:      * 
 76:      * @param string $message
 77:      * @param array $context
 78:      * @return type
 79:      */
 80:     private function interpolate($message, array $context = array())
 81:     {
 82:         // build a replacement array with braces around the context keys
 83:         $replace = array();
 84:         foreach ($context as $key => $val) {
 85:             $replace['{' . $key . '}'] = $val;
 86:         }
 87: 
 88:         // interpolate replacement values into the message and return
 89:         return strtr($message, $replace);
 90:     }
 91:     
 92:     /**
 93:      * Enable or disable the debug class.
 94:      * 
 95:      * @param  bool $enabled
 96:      * @return self
 97:      */
 98:     public function setEnabled($enabled)
 99:     {
100:         $this->enabled = $enabled;
101:         return $this;
102:     }
103:     
104:     /**
105:      * Is the debug class enabled?
106:      * 
107:      * @return bool
108:      */
109:     public function isEnabled()
110:     {
111:         return $this->enabled === true;
112:     }
113:     
114:     /**
115:      * Set an array of options.
116:      * 
117:      * @param array $options
118:      */
119:     public function setOptions(array $options = array())
120:     {
121:         foreach ($options as $key => $value) {
122:             $this->setOption($key, $value);
123:         }
124:         return $this;
125:     }
126:     
127:     /**
128:      * Get all options.
129:      * 
130:      * @return array
131:      */
132:     public function getOptions()
133:     {
134:         return $this->options;
135:     }
136:     
137:     /**
138:      * Set an individual option.
139:      * 
140:      * @param string $key
141:      * @param string $value
142:      */
143:     public function setOption($key, $value)
144:     {
145:         if ($this->optionExists($key)) {
146:             $this->options[$key] = $value;
147:             return $this;
148:         }
149:     }
150:     
151:     /**
152:      * Get an individual option.
153:      * 
154:      * @param  string $key
155:      * @return string|null
156:      */
157:     public function getOption($key)
158:     {
159:         if ($this->optionExists($key)) {
160:             return $this->options[$key];
161:         }
162:     }
163:     
164:     /**
165:      * Check whether an individual option exists.
166:      * 
167:      * @param  string $key
168:      * @return bool
169:      */
170:     private function optionExists($key)
171:     {
172:         return array_key_exists($key, $this->getOptions());
173:     }
174:     
175:     /**
176:      * Outputs a log message if necessary.
177:      * 
178:      * @param string $logLevel
179:      * @param string $message
180:      * @param string $context
181:      */
182:     public function log($level, $message, array $context = array())
183:     {
184:         if ($this->outputIsUrgent($level) || $this->isEnabled()) {
185:             $this->dispatch($message, $context);
186:         }
187:     }
188:     
189:     /**
190:      * Used to format the line outputted in the log file.
191:      * 
192:      * @param  string $string
193:      * @return string
194:      */
195:     private function formatFileLine($string)
196:     {
197:         $format = $this->getOption('dateFormat') . $this->getOption('delimeter');
198:         return date($format) . $string;
199:     }
200:     
201:     /**
202:      * Dispatch a log output message.
203:      * 
204:      * @param string $message
205:      * @param array $context
206:      * @throws LoggingException
207:      */
208:     private function dispatch($message, $context)
209:     {
210:         $output = $this->interpolate($message, $context) . PHP_EOL;
211:         
212:         if ($this->getOption('outputToFile') === true) {
213:             $file = $this->getOption('logFile');
214: 
215:             if (!is_writable($file)) {
216:                 throw new LoggingException(
217:                     'The log file either does not exist or is not writeable'
218:                 );
219:             }
220:             
221:             // Output to file
222:             file_put_contents($file, $this->formatFileLine($output), FILE_APPEND);
223:         } else {
224: 
225:             echo $output;
226:         }
227:     }
228: 
229:     public function deprecated($method, $new)
230:     {
231:         $string = sprintf('The %s method is deprecated, please use %s instead', $method, $new);
232:         return $this->warning($string);
233:     }
234:     
235: }
PHP OpenCloud API API documentation generated by ApiGen 2.8.0