Files
spa-api/system/log.php

83 lines
1.8 KiB
PHP
Raw Normal View History

2011-06-08 23:45:08 -05:00
<?php namespace System;
class Log {
/**
* Write an info message to the log.
*
* @param string $message
* @return void
*/
public static function info($message)
{
static::write('Info', $message);
}
/**
* Write a debug message to the log.
*
* @param string $message
* @return void
*/
public static function debug($message)
{
static::write('Debug', $message);
}
/**
* Write an error message to the logs.
*
* @param string $message
* @return void
*/
public static function error($message)
{
static::write('Error', $message);
}
/**
* Write a message to the logs.
*
* @param string $type
* @param string $message
* @return void
*/
public static function write($type, $message)
{
// -----------------------------------------------------
// Create the yearly and monthly directories if needed.
2011-06-08 23:45:08 -05:00
// -----------------------------------------------------
static::make_directory($directory = APP_PATH.'storage/logs/'.date('Y'));
static::make_directory($directory .= '/'.date('m'));
2011-06-08 23:45:08 -05:00
// -----------------------------------------------------
// Each day has its own log file.
2011-06-08 23:45:08 -05:00
// -----------------------------------------------------
$file = $directory.'/'.date('d').EXT;
2011-06-08 23:45:08 -05:00
// -----------------------------------------------------
// Append to the log file and set the permissions.
2011-06-08 23:45:08 -05:00
// -----------------------------------------------------
file_put_contents($file, date('Y-m-d H:i:s').' '.$type.' - '.$message.PHP_EOL, LOCK_EX | FILE_APPEND);
2011-06-08 23:45:08 -05:00
chmod($file, 0666);
}
/**
* Create a log directory.
*
* If the directory already exists, no action will be taken.
*
2011-06-08 23:45:08 -05:00
* @param string $directory
* @return void
*/
private static function make_directory($directory)
{
if ( ! is_dir($directory))
{
mkdir($directory, 02777);
chmod($directory, 02777);
}
2011-06-08 23:45:08 -05:00
}
}