/** * Append a line to a logfile in the logs directory. * Date will be added automatically to the line. * * @param $name name of log file * @param line Line to append */ public static function write_log($name, $line) { if (!is_string($line)) { $line = var_export($line, true); } $date_format = self::$instance ? self::$instance->config->get('log_date_format') : null; $log_driver = self::$instance ? self::$instance->config->get('log_driver') : null; if (empty($date_format)) { $date_format = 'd-M-Y H:i:s O'; } $date = date($date_format); // trigger logging hook if (is_object(self::$instance) && is_object(self::$instance->plugins)) { $log = self::$instance->plugins->exec_hook('write_log', array('name' => $name, 'date' => $date, 'line' => $line)); $name = $log['name']; $line = $log['line']; $date = $log['date']; if ($log['abort']) { return true; } } if ($log_driver == 'syslog') { $prio = $name == 'errors' ? LOG_ERR : LOG_INFO; syslog($prio, $line); return true; } // log_driver == 'file' is assumed here $line = sprintf("[%s]: %s\n", $date, $line); $log_dir = null; // per-user logging is activated if (self::$instance && self::$instance->config->get('per_user_logging', false) && self::$instance->get_user_id()) { $log_dir = self::$instance->get_user_log_dir(); if (empty($log_dir)) { return false; } } else { if (!empty($log['dir'])) { $log_dir = $log['dir']; } else { if (self::$instance) { $log_dir = self::$instance->config->get('log_dir'); } } } if (empty($log_dir)) { $log_dir = RCUBE_INSTALL_PATH . 'logs'; } // try to open specific log file for writing $logfile = $log_dir . '/' . $name; if ($fp = @fopen($logfile, 'a')) { fwrite($fp, $line); fflush($fp); fclose($fp); return true; } trigger_error("Error writing to log file {$logfile}; Please check permissions", E_USER_WARNING); return false; }
/** * Append a line to a logfile in the logs directory. * Date will be added automatically to the line. * * @param string $name Name of the log file * @param mixed $line Line to append * * @return bool True on success, False on failure */ public static function write_log($name, $line) { if (!is_string($line)) { $line = var_export($line, true); } $date_format = $log_driver = $session_key = null; if (self::$instance) { $date_format = self::$instance->config->get('log_date_format'); $log_driver = self::$instance->config->get('log_driver'); $session_key = intval(self::$instance->config->get('log_session_id', 8)); } $date = rcube_utils::date_format($date_format); // trigger logging hook if (is_object(self::$instance) && is_object(self::$instance->plugins)) { $log = self::$instance->plugins->exec_hook('write_log', array('name' => $name, 'date' => $date, 'line' => $line)); $name = $log['name']; $line = $log['line']; $date = $log['date']; if ($log['abort']) { return true; } } // add session ID to the log if ($session_key > 0 && ($sess = session_id())) { $line = '<' . substr($sess, 0, $session_key) . '> ' . $line; } if ($log_driver == 'syslog') { $prio = $name == 'errors' ? LOG_ERR : LOG_INFO; return syslog($prio, $line); } // log_driver == 'file' is assumed here $line = sprintf("[%s]: %s\n", $date, $line); // per-user logging is activated if (self::$instance && self::$instance->config->get('per_user_logging', false) && self::$instance->get_user_id()) { $log_dir = self::$instance->get_user_log_dir(); if (empty($log_dir) && $name != 'errors') { return false; } } if (empty($log_dir)) { if (!empty($log['dir'])) { $log_dir = $log['dir']; } else { if (self::$instance) { $log_dir = self::$instance->config->get('log_dir'); } } } if (empty($log_dir)) { $log_dir = RCUBE_INSTALL_PATH . 'logs'; } return file_put_contents("{$log_dir}/{$name}", $line, FILE_APPEND) !== false; }