[core, mod_console, mod_graylog2] Add sequence to logs to preserve order when timestamp is not precise enough.

This commit is contained in:
Chris Rienzo 2020-07-30 22:18:45 +00:00 committed by Andrey Volk
parent cd041b768c
commit 2c3dcbde71
4 changed files with 14 additions and 1 deletions

View File

@ -66,6 +66,8 @@ SWITCH_BEGIN_EXTERN_C
switch_text_channel_t channel;
switch_log_level_t slevel;
switch_event_t *tags;
/* Log sequence */
int64_t sequence;
} switch_log_node_t;
///\{
@ -94,6 +96,7 @@ typedef struct {
switch_log_json_format_item_t short_message;
const char *custom_field_prefix;
double timestamp_divisor;
switch_log_json_format_item_t sequence;
} switch_log_json_format_t;
typedef switch_status_t (*switch_log_function_t) (const switch_log_node_t *node, switch_log_level_t level);

View File

@ -77,7 +77,8 @@ static switch_log_json_format_t json_format = {
{ "message", NULL }, // full_message
{ NULL, NULL }, // short_message
"", // custom_field_prefix
0.0 // timestamp_divisor
0.0, // timestamp_divisor
{ "sequence", NULL } // sequence
};
static char *to_json_string(const switch_log_node_t *node)
@ -177,6 +178,8 @@ static switch_status_t config_logger(void)
json_format.full_message.name = zstr(val) ? NULL : switch_core_strdup(module_pool, val);
} else if (!strcasecmp(var, "short-message")) {
json_format.short_message.name = zstr(val) ? NULL : switch_core_strdup(module_pool, val);
} else if (!strcasecmp(var, "sequence")) {
json_format.sequence.name = zstr(val) ? NULL : switch_core_strdup(module_pool, val);
}
}
for (param = switch_xml_child(settings, "config"); param; param = param->next) {

View File

@ -333,6 +333,7 @@ SWITCH_MODULE_LOAD_FUNCTION(mod_graylog2_load)
globals.gelf_format.full_message.name = "full_message";
globals.gelf_format.short_message.name = "short_message";
globals.gelf_format.custom_field_prefix = "_";
globals.gelf_format.sequence.name = "_sequence";
switch_event_create_plain(&globals.session_fields, SWITCH_EVENT_CHANNEL_DATA);

View File

@ -68,6 +68,8 @@ static int mods_loaded = 0;
static int console_mods_loaded = 0;
static switch_bool_t COLORIZE = SWITCH_FALSE;
static int64_t log_sequence = 0;
#ifdef WIN32
static HANDLE hStdout;
static WORD wOldColorAttrs;
@ -144,6 +146,9 @@ SWITCH_DECLARE(cJSON *) switch_log_node_to_json(const switch_log_node_t *node, i
if (json_format->function.name && !zstr_buf(node->func)) {
cJSON_AddItemToObject(json, json_format->function.name, cJSON_CreateString(node->func));
}
if (json_format->sequence.name) {
cJSON_AddItemToObject(json, json_format->sequence.name, cJSON_CreateNumber(node->sequence));
}
/* skip initial space and new line */
if (*full_message == ' ') {
@ -478,6 +483,7 @@ static void *SWITCH_THREAD_FUNC log_thread(switch_thread_t *t, void *obj)
node = (switch_log_node_t *) pop;
switch_mutex_lock(BINDLOCK);
node->sequence = ++log_sequence;
for (binding = BINDINGS; binding; binding = binding->next) {
if (binding->level >= node->level) {
binding->function(node, node->level);