Skip to content
Snippets Groups Projects
Commit 38b2a29f authored by imarkov's avatar imarkov Committed by Konstantin Osipov
Browse files

say: Fix logging in log_rotate

log_rotate writes informational message only in plain format,
which is inappropriate when logger is configured to log in json format.

Fix it with replacing write with say_info which is safe
because it is ev signal callback not signal handler.

Fix bug say_format in json formatting,
Log message was invalid in several cases.

Closes #2987
parent 9f99bc62
No related branches found
No related tags found
No related merge requests found
......@@ -237,7 +237,7 @@ write_to_syslog(struct log *log, int total);
* Rotate logs on SIGHUP
*/
static int
log_rotate(const struct log *log)
log_rotate(struct log *log)
{
if (log->type != SAY_LOGGER_FILE) {
return 0;
......@@ -262,10 +262,11 @@ log_rotate(const struct log *log)
say_syserror("fcntl, fd=%i", log->fd);
}
}
char logrotate_message[] = "log file has been reopened\n";
ssize_t r = write(log->fd,
logrotate_message, (sizeof logrotate_message) - 1);
(void) r;
/* We are in ev signal handler
* so we don't have to be worry about async signal safety
*/
log_say(log, S_INFO, __FILE__, __LINE__, NULL,
"log file has been reopened");
/*
* log_background applies only to log_default logger
*/
......@@ -765,10 +766,10 @@ say_format_json(struct log *log, char *buf, int len, int level, const char *file
if (cord) {
SNPRINT(total, snprintf, buf, len, ", \"cord_name\": \"");
SNPRINT(total, json_escape, buf, len, cord->name);
SNPRINT(total, snprintf, buf, len, "\", ");
SNPRINT(total, snprintf, buf, len, "\"");
if (fiber() && fiber()->fid != 1) {
SNPRINT(total, snprintf, buf, len,
"\"fiber_id\": %i, ", fiber()->fid);
", \"fiber_id\": %i, ", fiber()->fid);
SNPRINT(total, snprintf, buf, len,
"\"fiber_name\": \"");
SNPRINT(total, json_escape, buf, len,
......
#!/usr/bin/env tarantool
local test = require('tap').test('log')
test:plan(22)
test:plan(23)
--
-- Check that Tarantool creates ADMIN session for #! script
......@@ -13,7 +13,7 @@ box.cfg{
memtx_memory=107374182,
}
local log = require('log')
local io = require('io')
local fio = require('fio')
local json = require('json')
local file = io.open(filename)
while file:read() do
......@@ -102,5 +102,17 @@ test:ok(not line:match("{"), "log change format")
s, e = pcall(log.log_format, "non_format")
test:ok(not s)
file:close()
log.log_format("json")
os.rename(filename, filename .. "2")
log.rotate()
file = fio.open(filename)
line = file:read()
index = line:find('\n')
line = line:sub(1, index)
message = json.decode(line)
test:is(message.message, "log file has been reopened", "check message after log.rotate()")
file:close()
test:check()
os.exit()
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment