Subversion Repositories phpLibraryV2

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
5 rodolico 1
<?php
2
 
6 rodolico 3
   require_once 'File.class.php';
5 rodolico 4
 
6 rodolico 5
   class LogFile extends File {
5 rodolico 6
      // if set, will compress and save old file and start new
7
      // when this size is reached. May have suffix 'k','m', 'g'
8
      // null means unlimited
9
      public $maxSize = null;
10
      // if $maxSize, this will unlink any old compressed versions
11
      // to keep the number of files below this.
12
      // null means keep all
13
      public $maxOld = null;
14
 
15
      public function __constructor (  $filename = null, 
16
                                       $mode = null, 
17
                                       $maxSize = null,
18
                                       $maxOld = null ) {
19
         $this->maxSize = $maxSize;
20
         $this->maxOld = $maxOld;
21
         if ( ! isset( $filename ) && isset( $_SESSION['global']['logger']['filename'] ) )
22
            $this->filename = $_SESSION['global']['logger']['filename'];
23
         parent::constructor( $filename, $mode );
24
      } // __constructor
25
 
26
 
27
      /*
28
       * writeLog
29
       * writes a log entry to $fullFileName if $fullFileName is set
30
       * log entry consists of a date/time stamp, a tab, the message, 
31
       * and a line return for EACH array element passed (if an array)
32
       * 
33
       * returns true if the bytes written on all lines
6 rodolico 34
       * returns false if $file can not be opened or if byte count 
5 rodolico 35
       * does not match string output size
36
       */
37
      public function writeLog ( $message, $format = null ) {
6 rodolico 38
         if ( ! isset( $this->fh ) ) $this->openFile();
39
         if ( ! isset( $this->fh ) ) return false;
5 rodolico 40
         if ( ! $message ) return true; // if empty message
6 rodolico 41
 
5 rodolico 42
         // if $message is a scalar, convert to array
43
         if ( ! is_array( $message ) ) 
44
            $message = array( $message );
45
         if ( ! isset( $format ) )
46
            $format = date( 'Y-m-d H:i:s' ) . "\t";
47
         $line = $format . implode( "\n" . $format , $message ) . "\n";
6 rodolico 48
         return ( fwrite( $this->fh, $line ) === strlen( $line ) );
5 rodolico 49
      } // function writeLog
50
   } // class logFile
51
 
52
?>
53