Overview

Namespaces

  • gregoryv
    • logger

Classes

  • gregoryv\logger\CachedWriter
  • gregoryv\logger\ConsoleWriter
  • gregoryv\logger\FileWriter
  • gregoryv\logger\Logger
  • gregoryv\logger\State
  • gregoryv\logger\SyslogWriter

Interfaces

  • gregoryv\logger\SeverityWriterInterface
  • Overview
  • Namespace
  • Class
 1: <?php
 2: // Copyright (c) 2015 Gregory Vinčić, The MIT License (MIT)
 3: namespace gregoryv\logger;
 4: 
 5: /**
 6: * FileWriter writes log messages to one or more files
 7: */
 8: class FileWriter implements SeverityWriterInterface
 9: {
10: 
11:     private $default_fh, $info_fh, $warn_fh, $error_fh, $debug_fh;
12: 
13:     /**
14:      * @param string $default path to file where all log levels are written by default
15:      */
16:     function __construct($default)
17:     {
18:         $fh = fopen($default, 'a');
19:         $this->default_fh = $fh;
20:         $this->info_fh = $fh;
21:         $this->warn_fh = $fh;
22:         $this->error_fh = $fh;
23:         $this->debug_fh = $fh;
24:     }
25: 
26:     public function swrite($severity, $value='')
27:     {
28:         $fh = $this->selectFileHandle($severity);
29:         fwrite($fh, $value . "\n");
30:     }
31: 
32:     /**
33:      * @param int $severity which level
34:      * @param string $file path to file
35:      */
36:     public function useFile($severity, $file)
37:     {
38:         $fh = fopen($file, 'a');
39:         switch ($severity) {
40:             case LOG_INFO:
41:                 $this->info_fh = $fh;
42:                 break;
43:             case LOG_WARNING:
44:                 $this->warn_fh = $fh;
45:                 break;
46:             case LOG_ERR:
47:                 $this->error_fh = $fh;
48:                 break;
49:             case LOG_DEBUG:
50:                 $this->debug_fh = $fh;
51:                 break;
52:             default:
53:                 throw new \InvalidArgumentException(sprintf('Invalid priority %s try LOG_INFO', $severity));
54:         }
55:     }
56: 
57:     private function selectFileHandle($severity)
58:     {
59:         switch ($severity) {
60:             case LOG_INFO:
61:                 return $this->info_fh;
62:             case LOG_WARNING:
63:                 return $this->warn_fh;
64:             case LOG_ERR:
65:                 return $this->error_fh;
66:             case LOG_DEBUG:
67:                 return $this->debug_fh;
68:         }
69:         return $this->default_fh;
70:     }
71: 
72: }
73: 
API documentation generated by ApiGen