[patches] [say 3/3] say: Fix logging in log_rotate

Ilya Markov imarkov at tarantool.org
Wed Mar 7 16:23:20 MSK 2018


From: imarkov <imarkov at tarantool.org>

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 | 16 ++++++++++++++--
 2 files changed, 22 insertions(+), 9 deletions(-)

diff --git a/src/say.c b/src/say.c
index d42d5f2..b92514d 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
 	 */
@@ -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,
diff --git a/test/app-tap/logger.test.lua b/test/app-tap/logger.test.lua
index 2d0f333..72d13d3 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
@@ -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()
-- 
2.7.4




More information about the Tarantool-patches mailing list