Blame view

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

31b7f2792   Kload   Upgrade to ownclo...
224
225
226
              echo $output;
          }
      }
6d9380f96   Cédric Dupont   Update sources OC...
227
228
229
230
231
232
  
      public function deprecated($method, $new)
      {
          $string = sprintf('The %s method is deprecated, please use %s instead', $method, $new);
          return $this->warning($string);
      }
31b7f2792   Kload   Upgrade to ownclo...
233
234
      
  }