Skip to content

Commit 70e99c1

Browse files
lightningd/log: Improve logging to handle multi-line messages
Changelog-Added: Improved logging format in `lightningd/log` to handle multi-line messages by unescaping '\n' and logging each line separately. Signed-off-by: Nishant Bansal <[email protected]>
1 parent 7be96ae commit 70e99c1

File tree

1 file changed

+21
-9
lines changed

1 file changed

+21
-9
lines changed

lightningd/log.c

+21-9
Original file line numberDiff line numberDiff line change
@@ -564,27 +564,39 @@ void logv(struct logger *log, enum log_level level,
564564
{
565565
int save_errno = errno;
566566
struct log_entry *l = new_log_entry(log, level, node_id);
567+
char *log_msg = NULL;
567568

568569
/* This is WARN_UNUSED_RESULT, because everyone should somehow deal
569570
* with OOM, even though nobody does. */
570-
if (vasprintf(&l->log, fmt, ap) == -1)
571+
if (vasprintf(&log_msg, fmt, ap) == -1)
571572
abort();
572573

573-
size_t log_len = strlen(l->log);
574+
char **lines = tal_strsplit(
575+
log, json_escape_unescape(log, (struct json_escape *)log_msg), "\n",
576+
STR_NO_EMPTY);
574577

575-
/* Sanitize any non-printable characters, and replace with '?' */
576-
for (size_t i=0; i<log_len; i++)
577-
if (l->log[i] < ' ' || l->log[i] >= 0x7f)
578-
l->log[i] = '?';
578+
/* Split to lines and log them separately. */
579+
for (size_t j = 0; lines[j]; j++) {
580+
l->log = tal_strdup(log, lines[j]);
579581

580-
maybe_print(log, l);
581-
maybe_notify_log(log, l);
582+
/* Sanitize any non-printable characters, and replace with '?'
583+
*/
584+
size_t line_len = strlen(l->log);
585+
for (size_t i = 0; i < line_len; i++)
586+
if (l->log[i] < ' ' || l->log[i] >= 0x7f)
587+
l->log[i] = '?';
582588

583-
add_entry(log, &l);
589+
maybe_print(log, l);
590+
maybe_notify_log(log, l);
591+
592+
add_entry(log, &l);
593+
}
584594

585595
if (call_notifier)
586596
notify_warning(log->log_book->ld, l);
587597

598+
tal_free(lines);
599+
free(log_msg);
588600
errno = save_errno;
589601
}
590602

0 commit comments

Comments
 (0)