Modules

Gleez_Log_File
extends Log_Writer
extends Kohana_Log_Writer

File log writer. Writes out messages and stores them in a YYYY/MM directory.

package
Gleez
category
Logging
author
Sandeep Sangamreddi - Gleez
copyright
© 2011 Gleez Technologies
license
http://gleezcms.org/license

Class declared in MODPATH/gleez/classes/gleez/log/file.php on line 11.

Constants

  • None

Properties

protected string $_directory

Directory to place log files in

protected array $_log_levels

Methods

public __construct( string $directory ) (defined in Gleez_Log_File)

Creates a new file logger. Checks that the directory exists and is writable.

$writer = new Log_File($directory);

Parameters

  • string $directory required - Log directory

Return Values

  • void

Source Code

public function __construct($directory)
{
  if ( ! is_dir($directory) OR ! is_writable($directory))
  {
    throw new Kohana_Exception('Directory :dir must be writable',
      array(':dir' => Debug::path($directory)));
  }

  // Determine the directory path
  $this->_directory = realpath($directory).DIRECTORY_SEPARATOR;
}

public write( array $messages ) (defined in Gleez_Log_File)

Writes each of the messages into the log file. The log file will be appended to the YYYY/MM/DD.log.php file, where YYYY is the current year, MM is the current month, and DD is the current day.

$writer->write($messages);

Parameters

  • array $messages required - Messages

Return Values

  • void

Source Code

public function write(array $messages)
{
  // Set the yearly directory name
  $directory = $this->_directory.date('Y');

  if ( ! is_dir($directory))
  {
    // Create the yearly directory
    mkdir($directory, 02777);

    // Set permissions (must be manually set to fix umask issues)
    chmod($directory, 02777);
  }

  // Add the month to the directory
  $directory .= DIRECTORY_SEPARATOR.date('m');

  if ( ! is_dir($directory))
  {
    // Create the yearly directory
    mkdir($directory, 02777);

    // Set permissions (must be manually set to fix umask issues)
    chmod($directory, 02777);
  }

  // Set the name of the log file
  $filename = $directory.DIRECTORY_SEPARATOR.date('d').EXT;

  if ( ! file_exists($filename))
  {
    // Create the log file
    file_put_contents($filename, Kohana::FILE_SECURITY.' ?>'.PHP_EOL);

    // Allow anyone to write to log files
    chmod($filename, 0666);
  }

  $info = array(
    'hostname'   => Request::$client_ip,
    'user_agent' => Request::$user_agent,
    'url'        => Request::$initial->uri(),
    'referer'    => isset(Request::$referrer) ? Request::$referrer : '',
  );

  foreach ($messages as $message)
  {
    // Write each message into the log file
    // Format: time --- level: body hostname
    file_put_contents($filename, PHP_EOL.$message['time'].' - - '.$this->_log_levels[$message['level']].': '.$message['body'].' - - '.$info['hostname'].' - - '.$info['url'].' - - '.$info['user_agent'].' - - '.$info['referer'], FILE_APPEND);
  }

}

final public __toString( ) (defined in Kohana_Log_Writer)

Allows the writer to have a unique key when stored.

echo $writer;

Return Values

  • string

Source Code

final public function __toString()
{
	return spl_object_hash($this);
}
Documentation comments powered by Disqus