[patches] [say 2/2] say: Fix logging in log_rotate
imarkov
imarkov at tarantool.org
Thu Feb 15 18:10:02 MSK 2018
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
---
src/say.c | 15 ++++++++-------
test/app-tap/logger.test.lua | 12 +++++++++++-
2 files changed, 19 insertions(+), 8 deletions(-)
diff --git a/src/say.c b/src/say.c
index 9d2a174..65b76b3 100644
--- a/src/say.c
+++ b/src/say.c
@@ -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
*/
@@ -769,10 +770,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,
diff --git a/test/app-tap/logger.test.lua b/test/app-tap/logger.test.lua
index 2d0f333..068f8b2 100755
--- a/test/app-tap/logger.test.lua
+++ b/test/app-tap/logger.test.lua
@@ -1,7 +1,7 @@
#!/usr/bin/env tarantool
local test = require('tap').test('log')
-test:plan(22)
+test:plan(23)
--
-- Check that Tarantool creates ADMIN session for #! script
@@ -102,5 +102,15 @@ 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.execute(string.format("mv %s %s2", filename, filename))
+log.rotate()
+log.info("some info")
+file = io.open(filename)
+line = file:read()
+message = json.decode(line)
+test:is(message.message, "log file has been reopened", "check message after log.rotate()")
+file:close()
test:check()
os.exit()
--
2.7.4
More information about the Tarantool-patches
mailing list