Ignore:
Timestamp:
Apr 24, 2015, 7:06:47 PM (9 years ago)
Author:
mistic100
Message:

feature 3221 Lazy log file open, clean code

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/include/Logger.class.php

    r31102 r31103  
    9696  private $_fileHandle = null;
    9797
     98
    9899  /**
    99100   * Class constructor.
     
    106107    $this->options = array_merge($this->options, $options);
    107108   
    108     if (is_string($this->options['severity'])) {
     109    if (is_string($this->options['severity']))
     110    {
    109111      $this->options['severity'] = self::codeToLevel($this->options['severity']);
    110112    }
    111113
    112     if ($this->options['severity'] === self::OFF) {
     114    if ($this->options['severity'] === self::OFF)
     115    {
    113116      return;
    114117    }
     
    116119    $this->options['directory'] = rtrim($this->options['directory'], '\\/') . DIRECTORY_SEPARATOR;
    117120
    118     if ($this->options['filename'] == null) {
     121    if ($this->options['filename'] == null)
     122    {
    119123      $this->options['filename'] = 'log_' . date('Y-m-d') . '.txt';
    120124    }
     
    122126    $this->options['filePath'] = $this->options['directory'] . $this->options['filename'];
    123127
    124     if (!file_exists($this->options['directory'])) {
    125       mkgetdir($this->options['directory'], MKGETDIR_DEFAULT|MKGETDIR_PROTECT_HTACCESS);
    126     }
    127 
    128     if (file_exists($this->options['filePath']) && !is_writable($this->options['filePath'])) {
    129       $this->_logStatus = self::STATUS_OPEN_FAILED;
    130       throw new RuntimeException(self::$_messages['writefail']);
    131       return;
    132     }
    133 
    134     if (($this->_fileHandle = fopen($this->options['filePath'], 'a'))) {
    135       $this->_logStatus = self::STATUS_LOG_OPEN;
    136     }
    137     else {
    138       $this->_logStatus = self::STATUS_OPEN_FAILED;
    139       throw new RuntimeException(self::$_messages['openfail']);
    140     }
    141 
    142     if ($this->options['archiveDays'] != self::ARCHIVE_NO_PURGE && rand() % 97 == 0) {
     128    if ($this->options['archiveDays'] != self::ARCHIVE_NO_PURGE && rand() % 97 == 0)
     129    {
    143130      $this->purge();
    144131    }
    145132  }
     133 
     134  /**
     135   * Open the log file if not already oppenned
     136   */
     137  private function open()
     138  {
     139    if ($this->status() == self::STATUS_LOG_CLOSED)
     140    {
     141      if (!file_exists($this->options['directory']))
     142      {
     143        mkgetdir($this->options['directory'], MKGETDIR_DEFAULT|MKGETDIR_PROTECT_HTACCESS);
     144      }
     145
     146      if (file_exists($this->options['filePath']) && !is_writable($this->options['filePath']))
     147      {
     148        $this->_logStatus = self::STATUS_OPEN_FAILED;
     149        throw new RuntimeException(self::$_messages['writefail']);
     150        return;
     151      }
     152
     153      if (($this->_fileHandle = fopen($this->options['filePath'], 'a')) != false)
     154      {
     155        $this->_logStatus = self::STATUS_LOG_OPEN;
     156      }
     157      else
     158      {
     159        $this->_logStatus = self::STATUS_OPEN_FAILED;
     160        throw new RuntimeException(self::$_messages['openfail']);
     161      }
     162    }
     163  }
    146164
    147165  /**
     
    150168  public function __destruct()
    151169  {
    152     if ($this->_fileHandle) {
     170    if ($this->_fileHandle)
     171    {
    153172      fclose($this->_fileHandle);
    154173    }
     
    281300  public function log($severity, $message, $cat = null, $args = array())
    282301  {
    283     if ($this->severity() >= $severity) {
    284       if (is_array($cat)) {
     302    if ($this->severity() >= $severity)
     303    {
     304      if (is_array($cat))
     305      {
    285306        $args = $cat;
    286307        $cat = null;
     
    298319  public function write($line)
    299320  {
    300     if ($this->_logStatus == self::STATUS_LOG_OPEN) {
    301       if (fwrite($this->_fileHandle, $line) === false) {
     321    $this->open();
     322    if ($this->status() == self::STATUS_LOG_OPEN)
     323    {
     324      if (fwrite($this->_fileHandle, $line) === false)
     325      {
    302326        throw new RuntimeException(self::$_messages['writefail']);
    303327      }
     
    308332   * Purges files matching 'globPattern' older than 'archiveDays'.
    309333   */
    310   public function purge() {
     334  public function purge()
     335  {
    311336    $files = glob($this->options['directory'] . $this->options['globPattern']);
    312337    $limit = time() - $this->options['archiveDays'] * 86400;
    313338
    314     foreach ($files as $file) {
    315       if (@filemtime($file) < $limit) {
     339    foreach ($files as $file)
     340    {
     341      if (@filemtime($file) < $limit)
     342      {
    316343        @unlink($file);
    317344      }
     
    329356  private function formatMessage($level, $message, $cat, $context)
    330357  {
    331     if (!empty($context)) {
    332       $message .= "\n" . $this->indent($this->contextToString($context));
     358    if (!empty($context))
     359    {
     360      $message.= "\n" . $this->indent($this->contextToString($context));
    333361    }
    334362    $line = "[" . $this->getTimestamp() . "]\t[" . self::levelToCode($level) . "]\t";
    335     if ($cat != null) {
    336       $line .= "[" . $cat . "]\t";
     363    if ($cat != null)
     364    {
     365      $line.= "[" . $cat . "]\t";
    337366    }
    338367    return $line . $message . "\n";
     
    350379  {
    351380    $originalTime = microtime(true);
    352     $micro = sprintf("%06d", ($originalTime - floor($originalTime)) * 1000000);
     381    $micro = sprintf('%06d', ($originalTime - floor($originalTime)) * 1000000);
    353382    $date = new DateTime(date('Y-m-d H:i:s.'.$micro, $originalTime));
    354383    return $date->format($this->options['dateFormat']);
     
    364393  {
    365394    $export = '';
    366     foreach ($context as $key => $value) {
    367       $export .= "{$key}: ";
    368       $export .= preg_replace(array(
     395    foreach ($context as $key => $value)
     396    {
     397      $export.= $key . ': ';
     398      $export.= preg_replace(array(
    369399        '/=>\s+([a-zA-Z])/im',
    370400        '/array\(\s+\)/im',
    371401        '/^  |\G  /m'
    372       ), array(
     402        ),
     403        array(
    373404        '=> $1',
    374405        'array()',
    375406        '  '
    376       ), str_replace('array (', 'array(', var_export($value, true)));
    377       $export .= PHP_EOL;
     407        ),
     408        str_replace('array (', 'array(', var_export($value, true))
     409        );
     410      $export.= PHP_EOL;
    378411    }
    379412    return str_replace(array('\\\\', '\\\''), array('\\', '\''), rtrim($export));
     
    389422  private function indent($string, $indent = '  ')
    390423  {
    391     return $indent.str_replace("\n", "\n".$indent, $string);
     424    return $indent . str_replace("\n", "\n" . $indent, $string);
    392425  }
    393426
     
    400433  static function levelToCode($level)
    401434  {
    402     switch ($level) {
     435    switch ($level)
     436    {
    403437      case self::EMERGENCY:
    404438        return 'EMERGENCY';
     
    430464  static function codeToLevel($code)
    431465  {
    432     switch (strtoupper($code)) {
     466    switch (strtoupper($code))
     467    {
    433468      case 'EMERGENCY':
    434469        return self::EMERGENCY;
Note: See TracChangeset for help on using the changeset viewer.