Class libraries


Class library for managing and writing logs

File: tokernel.framework/lib/log.lib.php

This log class brings also you many other advantages. For example, if the log file maximum size is reached, the class will automatically take the actions to gzip it and create and open a new empty log file. It includes other functions like compress, uncompress, files_list, files_clean and more to make the management of log files fast and easy.

Library methods

object instance(string $log_file [, int $file_size = 524288 ][, string $log_path = NULL])

Returns a new instance of log object.

// Return a new instance of log object, with default file size and log files path. // Default log files path: /application/log/ $l = $this->lib->log->instance('my_file'); // Return a new instance of log object, with user defined file size and log files path. $l = $this->lib->log->instance('my_file', 1048576, '/var/log/');

bool file_compress(string $log_file [, bool $rem_file = false])

Compress log file to *.gz format. Delete source file if $rem_file defined as true.

$removed = $l->file_compress('my_file', true);

string file_name()

Return current log file name.

// Return 'my_file' $file = $l->file_name();

mixed file_read(string $file [, bool $as_array = false])

Return log file content as string or as an array. It is possible to read and return *.gz archived log file.

// Return file content as string. $content = $l->file_read('my_file'); // Return file content as an array. $content = $l->file_read('my_file', true); // Return compressed file content. $content = $l->file_read('other_file.gz');

bool file_uncompress(string $arch_file [, bool $rem_gz = false])

Uncompress log file (*.gz). Delete source file if $rem_gz defined as true.


bool files_clean([string $ext = NULL])

Delete log files.

// Delete all log files. $l->files_clean(); // Delete only compressed log files. $l->files_clean('gz');

array files_list([string $ext = NULL])

Returns list of log files. If extension not defined, returns all files such as *.log, *.gz

// Return all files as array. $files = $l->files_list(); // Return only compressed log files. $files = $l->files_list('gz');

Note: You can define more than one extensions as argument for methods: files_clean(), files_list() as 'log|gz'.

string log_path()

Return log files path.

// Return application default log files path. // .../application/log/ $path = $this->lib->log->log_path(); // Return user defined log files path // /var/log/ $path = $l->log_path();

bool write(string $log_message)

Write/append data to current log file.

$l->write('some message');

Example of 'log' class usage.

// Create an instance of log object. $l = $this->lib->log->instance('my_file', 1048576, '/var/log/'); // Write message to log file. $l->write('message 1'); $l->write('message 3'); // Compress log file to my_file.gz and delete source file my_file. $l->file_compress('my_file', true); // Get compressed file content as string. $l->file_read('my_file.gz'); // Delete all log files. $l->files_clean();