Commit 173503ab authored by Nigel McNie's avatar Nigel McNie
Browse files

Add LOG_TARGET_FILE and $CFG->log_file so errors can be logged to a file.



This isn't enabled by default, but might help some people. Also, the
importer can use it to log debugging output.
Signed-off-by: default avatarNigel McNie <nigel@catalyst.net.nz>
parent 5ae5207b
......@@ -63,6 +63,7 @@ $cfg = new StdClass;
// Logging configuration
// For each log level, you can specify where the messages are displayed.
//
// LOG_TARGET_SCREEN makes the error messages go to the screen - useful
// when debugging but not on a live site!
// LOG_TARGET_ADMIN sends error messages to the screen but only when
......@@ -70,7 +71,14 @@ $cfg = new StdClass;
// LOG_TARGET_ERRORLOG makes the error messages go to the log as specified
// by the apache ErrorLog directive. It's probably useful to have this on
// for all log levels.
// You can combine them with bitwise operations,
// LOG_TARGET_FILE allows you to specify a file that messages will be logged
// to. It's best to pick a path in dataroot, but note that logfiles tend to get
// very large over time - so it's advisable to implement some kind of logrotate
// if you want to leave this on all the time. The other option is to just turn
// this on when you are getting some kind of error or want to see the logging,
// and know that you're not going to let the logfile get large.
//
// You can combine the targets with bitwise operations,
// e.g. LOG_TARGET_SCREEN | LOG_TARGET_ERRORLOG
//
// This configuration is suitable for people running Mahara for the first
......@@ -90,6 +98,10 @@ $cfg->log_environ_targets = LOG_TARGET_SCREEN | LOG_TARGET_ERRORLOG;
//$cfg->log_warn_targets = LOG_TARGET_SCREEN | LOG_TARGET_ERRORLOG;
//$cfg->log_environ_targets = LOG_TARGET_SCREEN | LOG_TARGET_ERRORLOG;
// If you use LOG_TARGET_FILE, this is the file that errors will be logged to.
// It's best to pick a path under dataroot, as we know we can write there.
$cfg->log_file = $CFG->dataroot . '/error.log';
// The log levels that will generate backtraces. Useful for development,
// but probably only warnings are useful on a live site.
$cfg->log_backtrace_levels = LOG_LEVEL_WARN | LOG_LEVEL_ENVIRON;
......
......@@ -40,6 +40,8 @@ define('LOG_TARGET_STDOUT', 4);
/** Display the errors on the screen in the admin area only (short term hack
until we create an admin notifications page) */
define('LOG_TARGET_ADMIN', 8);
/** Log to a specific file */
define('LOG_TARGET_FILE', 16);
// Logging levels
/** Environment type errors, such as register_globals being on */
......@@ -253,6 +255,40 @@ function log_message ($message, $loglevel, $escape, $backtrace, $file=null, $lin
echo $textbacktrace;
}
}
if (function_exists('get_config')) {
$logfilename = get_config('log_file');
if (($targets & LOG_TARGET_FILE) && $logfilename) {
global $LOGFILE_FH;
static $logfile_open_attempted = null;
if (!$logfile_open_attempted) {
$logfile_open_attempted = true;
$LOGFILE_FH = fopen($logfilename, 'wb');
if ($LOGFILE_FH !== false) {
function _close_logfile() {
global $LOGFILE_FH;
fclose($LOGFILE_FH);
}
register_shutdown_function('_close_logfile');
}
else {
error_log("Could not open your custom log file ($logfilename)");
}
}
if (is_resource($LOGFILE_FH)) {
foreach ($loglines as $line) {
fwrite($LOGFILE_FH, $prefix . $line . "\n");
}
if ($backtrace && $textbacktrace) {
$lines = explode("\n", $textbacktrace);
foreach ($lines as $line) {
fwrite($LOGFILE_FH, $line . "\n");
}
}
}
}
}
}
/**
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment