Skip to content

Commit

Permalink
Updated log module to use full paths when writing log files instead o…
Browse files Browse the repository at this point in the history
…f local sites file dir.
  • Loading branch information
geekforbrains committed May 25, 2012
1 parent 60d3f54 commit 8cd33cf
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 40 deletions.
38 changes: 8 additions & 30 deletions core/log/log.php
Expand Up @@ -77,9 +77,11 @@ private static function _log($type, $module, $message)
{
// Always write to CLI
if(IS_CLI)
{
fwrite(STDOUT, sprintf("[%s] $module: $message\n", strtoupper($type)));
return;
}


if(Config::get(sprintf('log.%s_enabled', $type)))
{
if(!Config::get(sprintf('log.%s_to_file', $type)))
Expand All @@ -97,42 +99,18 @@ private static function _log($type, $module, $message)

private static function _writeLog($type, $module, $message)
{
$logFile = ROOT . Media::getFilesPath() . Config::get(sprintf('log.%s_file', $type));
$logDir = implode('/', explode('/', $logFile, -1));

if(!file_exists($logDir))
{
$filesPath = ROOT . Media::getFilesPath();

if(is_writable($filesPath))
mkdir($logDir);
else
die('Unable to create log directory because your files directory isn\'t writable.');
}
$logFile = Config::get(sprintf('log.%s_file', $type));

if(!is_writable($logDir))
die('Unable to write log file because your log directory isn\'t writable.');

if(!file_exists($logFile))
{
$header = "<?php if(!defined('ROOT')) exit; ?>\n";

if(!$handle = fopen($logFile, 'a'))
die('Cant create log file: ' . $logFile);

if(fwrite($handle, $header) === false)
die('Cant write to log file: ' . $logFile);

fclose($handle);
}
if(!$logFile)
return;

$log = sprintf("[%s] %s: %s\n", date('Y-m-d H:i:s'), $module, $message);

if(!$handle = fopen($logFile, 'a'))
die('Cant open log file for writing: ' . $logFile);
error_log('Cant open log file for writing: ' . $logFile);

if(fwrite($handle, $log) === false)
die('Cant write to log file: ' . $logFile);
error_log('Cant write to log file: ' . $logFile);

fclose($handle);
}
Expand Down
27 changes: 20 additions & 7 deletions core/log/setup.php
Expand Up @@ -15,11 +15,17 @@
'log.debug_to_file' => false,

/**
* The debug log file, relative to the current sites "files/" directory, that debug
* messages will be written to. Messages will only be written to file if the
* log.debug_to_file config is set to "true".
* The full file path to write debug messages to. This is "null" by default and will
* not attempt to write to file unless a path is set.
*
* If you would like to log files relative to your applications path, use the "ROOT"
* constant.
*
* Example: 'log.debug_file' => '/tmp/caffeine_debug.log'
*
* Note that you must ensure the path given exists and is writable.
*/
'log.debug_file' => sprintf('logs/debug_%s.php', date('Y_m_d')),
'log.debug_file' => null,

/**
* When enabled, all messages logged with the Log::error() method will either be
Expand All @@ -35,10 +41,17 @@
'log.error_to_file' => false,

/**
* The error log file, relative to the current sites "files/" directory, that error
* messages will be written to.
* The full file path to write error messages to. This is "null" by default and will
* not attempt to write to file unless a path is set.
*
* If you would like to log files relative to your applications path, use the "ROOT"
* constant.
*
* Example: 'log.error_file' => '/tmp/caffeine_error.log'
*
* Note that you must ensure the path given exists and is writable.
*/
'log.error_file' => sprintf('logs/error_%s.php', date('Y_m_d'))
'log.error_file' => null,
),

'events' => array(
Expand Down
3 changes: 0 additions & 3 deletions setup.php
Expand Up @@ -46,9 +46,6 @@
* http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html
*/
'system.maintenance_mode' => false,

'log.debug_enabled' => true,
'log.error_enabled' => true,
)

);

0 comments on commit 8cd33cf

Please sign in to comment.