Public Member Functions | Data Fields | Protected Member Functions
Log Class Reference

Logs data to specified files. More...

Public Member Functions

 Log ($file= '')
 
 setDateFormat ($date_format="H-m-d G:i:s")
 
 setSeparator ($separator= '--> ')
 
 setFile ($file)
 
 line ($line)
 
 lines ($lines)
 
 __destruct ()
 

Data Fields

 $file_handle = null
 
 $date_format = "Y-m-d G:i:s"
 
 $separator = ' --> '
 

Protected Member Functions

 lineForm ($line)
 

Detailed Description

Logs data to specified files.

Version
1.2
Author
DinuSV

The constructor takes the file location as its argument. If you want to autoload this library, you can set the file using the setFile() method. Both the method and the constructor throw a FileNotFoundException in case the file does not exist, or a NoPermissionsException in case the file does not have permissions for writing.

// Creating an object
$logger = new Log("myfile.txt");
// Setting the file in the constructor in case the library was autoloaded
$this->log->setFile("myfile.txt");

To output a line, you can use the line() or lines() methods. Both methods throw a NoPermissionsException in case the file is not writable.

$this->log->line('first statement');
$this->log->lines( array( 'second statement', 'third statement' ) );

By default, an example of line that was printed will look like this:
2012-01-31 22:30:30 --> first statement

So, the line is composed of a date and separator. The two are customizable :

$this->log->setDateFormat( 'Y M d H:i:s' )->setSeparator( ' ====== ' )->line( 'forth statement' );
// 2012 Jan 20 22:30:30 ====== forth statement

Constructor & Destructor Documentation

__destruct ( )

Destructor, closes the file

Member Function Documentation

Log (   $file = '')

Constructor

Parameters
string$file: the location of the file
Exceptions
NoPermissionsException
FileNotFoundException
setDateFormat (   $date_format = "H-m-d G:i:s")

Set the format of the date

Parameters
string$date_format
Returns
current object
setSeparator (   $separator = ' --> ')

Set the separator between the date and data in each line

Parameters
string$separator
Returns
current object
setFile (   $file)

Set the file to log to

Parameters
string$file: location of the file to log to
Exceptions
NoPermissionsException
FileNotFoundException
Returns
current object
lineForm (   $line)
protected

Formats the line, adding the date

Parameters
string$line: the line to return
Returns
string : the formated line
line (   $line)

Outputs the line using the lineForm function

Parameters
string$line
Exceptions
NoPermissionException
Returns
current object
lines (   $lines)

Same as line, only this can output multiple lines

Parameters
array$lines: the lines to output
string$lines: the function also accepts one parameter
Exceptions
NoPermissionException
Returns
current object

Field Documentation

$file_handle = null

resource : Log file handler

$date_format = "Y-m-d G:i:s"

string : Format of the date to output to the file

$separator = ' --> '

string : Separator between date and data for each line