2020-01-26 13:19:35 +00:00
|
|
|
<?php declare(strict_types=1);
|
2018-11-05 22:30:18 +00:00
|
|
|
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *\
|
|
|
|
* Logging convenience *
|
|
|
|
\* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
|
|
|
|
|
|
|
|
define("SCORE_LOG_CRITICAL", 50);
|
|
|
|
define("SCORE_LOG_ERROR", 40);
|
|
|
|
define("SCORE_LOG_WARNING", 30);
|
|
|
|
define("SCORE_LOG_INFO", 20);
|
|
|
|
define("SCORE_LOG_DEBUG", 10);
|
|
|
|
define("SCORE_LOG_NOTSET", 0);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A shorthand way to send a LogEvent
|
|
|
|
*
|
|
|
|
* When parsing a user request, a flash message should give info to the user
|
|
|
|
* When taking action, a log event should be stored by the server
|
|
|
|
* Quite often, both of these happen at once, hence log_*() having $flash
|
|
|
|
*/
|
2020-02-01 21:37:07 +00:00
|
|
|
function log_msg(string $section, int $priority, string $message, ?string $flash=null)
|
2019-05-28 16:59:38 +00:00
|
|
|
{
|
2019-12-15 19:47:18 +00:00
|
|
|
global $page;
|
2020-02-01 21:37:07 +00:00
|
|
|
send_event(new LogEvent($section, $priority, $message));
|
2019-05-28 16:59:38 +00:00
|
|
|
$threshold = defined("CLI_LOG_LEVEL") ? CLI_LOG_LEVEL : 0;
|
2018-11-05 22:30:18 +00:00
|
|
|
|
2019-05-28 16:59:38 +00:00
|
|
|
if ((PHP_SAPI === 'cli' || PHP_SAPI === 'phpdbg') && ($priority >= $threshold)) {
|
|
|
|
print date("c")." $section: $message\n";
|
|
|
|
}
|
|
|
|
if (!is_null($flash)) {
|
2019-12-15 19:47:18 +00:00
|
|
|
$page->flash($flash);
|
2019-05-28 16:59:38 +00:00
|
|
|
}
|
2018-11-05 22:30:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// More shorthand ways of logging
|
2020-02-01 21:37:07 +00:00
|
|
|
function log_debug(string $section, string $message, ?string $flash=null)
|
2019-05-28 16:59:38 +00:00
|
|
|
{
|
2020-02-01 21:37:07 +00:00
|
|
|
log_msg($section, SCORE_LOG_DEBUG, $message, $flash);
|
2019-05-28 16:59:38 +00:00
|
|
|
}
|
2020-02-01 21:37:07 +00:00
|
|
|
function log_info(string $section, string $message, ?string $flash=null)
|
2019-05-28 16:59:38 +00:00
|
|
|
{
|
2020-02-01 21:37:07 +00:00
|
|
|
log_msg($section, SCORE_LOG_INFO, $message, $flash);
|
2019-05-28 16:59:38 +00:00
|
|
|
}
|
2020-02-01 21:37:07 +00:00
|
|
|
function log_warning(string $section, string $message, ?string $flash=null)
|
2019-05-28 16:59:38 +00:00
|
|
|
{
|
2020-02-01 21:37:07 +00:00
|
|
|
log_msg($section, SCORE_LOG_WARNING, $message, $flash);
|
2019-05-28 16:59:38 +00:00
|
|
|
}
|
2020-02-01 21:37:07 +00:00
|
|
|
function log_error(string $section, string $message, ?string $flash=null)
|
2019-05-28 16:59:38 +00:00
|
|
|
{
|
2020-02-01 21:37:07 +00:00
|
|
|
log_msg($section, SCORE_LOG_ERROR, $message, $flash);
|
2019-05-28 16:59:38 +00:00
|
|
|
}
|
2020-02-01 21:37:07 +00:00
|
|
|
function log_critical(string $section, string $message, ?string $flash=null)
|
2019-05-28 16:59:38 +00:00
|
|
|
{
|
2020-02-01 21:37:07 +00:00
|
|
|
log_msg($section, SCORE_LOG_CRITICAL, $message, $flash);
|
2019-05-28 16:59:38 +00:00
|
|
|
}
|
2018-11-05 22:30:18 +00:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get a unique ID for this request, useful for grouping log messages.
|
|
|
|
*/
|
2019-05-28 16:59:38 +00:00
|
|
|
function get_request_id(): string
|
|
|
|
{
|
|
|
|
static $request_id = null;
|
|
|
|
if (!$request_id) {
|
|
|
|
// not completely trustworthy, as a user can spoof this
|
|
|
|
if (@$_SERVER['HTTP_X_VARNISH']) {
|
|
|
|
$request_id = $_SERVER['HTTP_X_VARNISH'];
|
|
|
|
} else {
|
|
|
|
$request_id = "P" . uniqid();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $request_id;
|
2018-11-05 22:30:18 +00:00
|
|
|
}
|