Browse code

core: new option for json logging to print message in json format

- option 'j' - if the log message starts with '{' and ends with '}',
then it is expected to be a json document, printed as it is in the
message value, otherwise it is printed as field "text"

Daniel-Constantin Mierla authored on 12/11/2021 09:38:04
Showing 1 changed files
... ...
@@ -529,6 +529,7 @@ static int _ksr_slog_json_flags = 0;
529 529
 #define KSR_SLOGJSON_FL_APPPREFIX (1<<2)
530 530
 #define KSR_SLOGJSON_FL_NOAPPPREFIXMSG (1<<3)
531 531
 #define KSR_SLOGJSON_FL_CALLID (1<<4)
532
+#define KSR_SLOGJSON_FL_MSGJSON (1<<5)
532 533
 
533 534
 
534 535
 #define LOGV_CALLID_STR (((_ksr_slog_json_flags & KSR_SLOGJSON_FL_CALLID) \
... ...
@@ -614,34 +615,34 @@ static void ksr_slog_json_str_escape(str *s_in, str *s_out, int *emode)
614 615
 }
615 616
 
616 617
 #define KSR_SLOG_SYSLOG_JSON_FMT "{ \"level\": \"%s\", \"module\": \"%s\", \"file\": \"%s\"," \
617
-	" \"line\": %d, \"function\": \"%s\", %.*s\"logprefix\": \"%.*s\", \"%smessage\": \"%.*s\" }%s"
618
+	" \"line\": %d, \"function\": \"%s\", %.*s\"logprefix\": \"%.*s\", \"%smessage\": %s%.*s%s }%s"
618 619
 
619 620
 #define KSR_SLOG_SYSLOG_JSON_CFMT "{ \"level\": \"%s\", \"module\": \"%s\", \"file\": \"%s\"," \
620
-	" \"line\": %d, \"function\": \"%s\", \"callid\": \"%.*s\", \"logprefix\": \"%.*s\", \"%smessage\": \"%.*s\" }%s"
621
+	" \"line\": %d, \"function\": \"%s\", \"callid\": \"%.*s\", \"logprefix\": \"%.*s\", \"%smessage\": %s%.*s%s }%s"
621 622
 
622 623
 #define KSR_SLOG_SYSLOG_JSON_PFMT "{ \"" NAME ".level\": \"%s\", \"" NAME ".module\": \"%s\", \"" NAME ".file\": \"%s\"," \
623
-	" \"" NAME ".line\": %d, \"" NAME ".function\": \"%s\", %.*s\"" NAME ".logprefix\": \"%.*s\", \"%smessage\": \"%.*s\" }%s"
624
+	" \"" NAME ".line\": %d, \"" NAME ".function\": \"%s\", %.*s\"" NAME ".logprefix\": \"%.*s\", \"%smessage\": %s%.*s%s }%s"
624 625
 
625 626
 #define KSR_SLOG_SYSLOG_JSON_CPFMT "{ \"" NAME ".level\": \"%s\", \"" NAME ".module\": \"%s\", \"" NAME ".file\": \"%s\"," \
626 627
 	" \"" NAME ".line\": %d, \"" NAME ".function\": \"%s\", \"" NAME ".callid\": \"%.*s\", \"" NAME ".logprefix\": \"%.*s\"," \
627
-	" \"%smessage\": \"%.*s\" }%s"
628
+	" \"%smessage\": %s%.*s%s }%s"
628 629
 
629 630
 #define KSR_SLOG_STDERR_JSON_FMT "{ \"idx\": %d, \"pid\": %d, \"level\": \"%s\"," \
630 631
 	" \"module\": \"%s\", \"file\": \"%s\"," \
631
-	" \"line\": %d, \"function\": \"%s\", %.*s\"logprefix\": \"%.*s\", \"%smessage\": \"%.*s\" }%s"
632
+	" \"line\": %d, \"function\": \"%s\", %.*s\"logprefix\": \"%.*s\", \"%smessage\": %s%.*s%s }%s"
632 633
 
633 634
 #define KSR_SLOG_STDERR_JSON_CFMT "{ \"idx\": %d, \"pid\": %d, \"level\": \"%s\"," \
634 635
 	" \"module\": \"%s\", \"file\": \"%s\"," \
635
-	" \"line\": %d, \"function\": \"%s\", \"callid\": \"%.*s\", \"logprefix\": \"%.*s\", \"%smessage\": \"%.*s\" }%s"
636
+	" \"line\": %d, \"function\": \"%s\", \"callid\": \"%.*s\", \"logprefix\": \"%.*s\", \"%smessage\": %s%.*s%s }%s"
636 637
 
637 638
 #define KSR_SLOG_STDERR_JSON_PFMT "{ \"" NAME ".idx\": %d, \"" NAME ".pid\": %d, \"" NAME ".level\": \"%s\"," \
638 639
 	" \"" NAME ".module\": \"%s\", \"" NAME ".file\": \"%s\"," \
639
-	" \"" NAME ".line\": %d, \"" NAME ".function\": \"%s\", %.*s\"" NAME ".logprefix\": \"%.*s\", \"%smessage\": \"%.*s\" }%s"
640
+	" \"" NAME ".line\": %d, \"" NAME ".function\": \"%s\", %.*s\"" NAME ".logprefix\": \"%.*s\", \"%smessage\": %s%.*s%s }%s"
640 641
 
641 642
 #define KSR_SLOG_STDERR_JSON_CPFMT "{ \"" NAME ".idx\": %d, \"" NAME ".pid\": %d, \"" NAME ".level\": \"%s\"," \
642 643
 	" \"" NAME ".module\": \"%s\", \"" NAME ".file\": \"%s\"," \
643 644
 	" \"" NAME ".line\": %d, \"" NAME ".function\": \"%s\", \"" NAME ".callid\": \"%.*s\", \"" NAME ".logprefix\": \"%.*s\"," \
644
-	" \"%smessage\": \"%.*s\" }%s"
645
+	" \"%smessage\": %s%.*s%s }%s"
645 646
 
646 647
 #ifdef HAVE_PTHREAD
647 648
 #define KSR_SLOG_JSON_CEEFMT_TID ",\"tid\":%ju"
... ...
@@ -649,7 +650,7 @@ static void ksr_slog_json_str_escape(str *s_in, str *s_out, int *emode)
649 650
 #define KSR_SLOG_JSON_CEEFMT_TID ""
650 651
 #endif
651 652
 #define KSR_SLOG_JSON_CEEFMT "{\"time\":\"%s.%09luZ\",\"proc\":{\"id\":\"%d\"" KSR_SLOG_JSON_CEEFMT_TID "},\"pri\":\"%s\",\"subsys\":\"%s\"," \
652
-        "\"file\":{\"name\":\"%s\",\"line\":%d},\"native\":{\"function\":\"%s\"},\"msg\":\"%s\"," \
653
+        "\"file\":{\"name\":\"%s\",\"line\":%d},\"native\":{\"function\":\"%s\"},\"msg\":%s%.*s%s," \
653 654
         "\"pname\":\"%s\",\"appname\":\"%s\",\"hostname\":\"%s\"}%s"
654 655
 
655 656
 #define KSR_SLOG_SYSLOG_JSON_CEEFMT "@cee: " KSR_SLOG_JSON_CEEFMT
... ...
@@ -672,6 +673,8 @@ void ksr_slog_json(ksr_logdata_t *kld, const char *format, ...)
672 673
 	char iso8601buf[ISO8601_BUF_SIZE + 1];
673 674
 	struct timespec _tp;
674 675
 	struct tm _tm;
676
+	char *smb = "\"";
677
+	char *sme = "\"";
675 678
 
676 679
 	va_start(arglist, format);
677 680
 	n = vsnprintf(obuf + s_in.len, KSR_SLOG_MAX_SIZE - s_in.len, format, arglist);
... ...
@@ -689,9 +692,22 @@ void ksr_slog_json(ksr_logdata_t *kld, const char *format, ...)
689 692
 		}
690 693
 	}
691 694
 
692
-	ksr_slog_json_str_escape(&s_in, &s_out, &emode);
695
+	if ((!log_cee) && (_ksr_slog_json_flags & KSR_SLOGJSON_FL_MSGJSON)) {
696
+		if ((s_in.len>1) && (s_in.s[0] == '{') && (s_in.s[s_in.len - 1] == '}')) {
697
+			s_out = s_in;
698
+			smb = "";
699
+			sme = "";
700
+		} else {
701
+			smb = "{ \"text\": \"";
702
+			sme = "\" }";
703
+		}
704
+	}
705
+
693 706
 	if(s_out.s == NULL) {
694
-		goto error;
707
+		ksr_slog_json_str_escape(&s_in, &s_out, &emode);
708
+		if(s_out.s == NULL) {
709
+			goto error;
710
+		}
695 711
 	}
696 712
 
697 713
 	if(_ksr_slog_json_flags & KSR_SLOGJSON_FL_APPPREFIX) {
... ...
@@ -728,18 +744,18 @@ void ksr_slog_json(ksr_logdata_t *kld, const char *format, ...)
728 744
                         (uintmax_t)pthread_self(),
729 745
 #endif
730 746
                         kld->v_lname,
731
-			kld->v_mname, kld->v_fname, kld->v_fline, kld->v_func, s_out.s,
747
+			kld->v_mname, kld->v_fname, kld->v_fline, kld->v_func, smb, s_out.len, s_out.s, sme,
732 748
 			"kamailio", log_name!=0?log_name:"kamailio", log_fqdn,
733 749
 			(_ksr_slog_json_flags & KSR_SLOGJSON_FL_NOLOGNL)?"":"\n");
734 750
 		} else {
735
-		if (unlikely(log_color)) dprint_color(kld->v_level);
736
-		fprintf(stderr,
751
+			if (unlikely(log_color)) dprint_color(kld->v_level);
752
+			fprintf(stderr,
737 753
 				efmt, process_no, my_pid(),
738 754
 				kld->v_lname, kld->v_mname, kld->v_fname, kld->v_fline,
739 755
 				kld->v_func, LOGV_CALLID_LEN, LOGV_CALLID_STR,
740
-				LOGV_PREFIX_LEN, LOGV_PREFIX_STR, prefmsg, s_out.len, s_out.s,
756
+				LOGV_PREFIX_LEN, LOGV_PREFIX_STR, prefmsg, smb, s_out.len, s_out.s, sme,
741 757
 				(_ksr_slog_json_flags & KSR_SLOGJSON_FL_NOLOGNL)?"":"\n");
742
-		if (unlikely(log_color)) dprint_color_reset();
758
+			if (unlikely(log_color)) dprint_color_reset();
743 759
 		}
744 760
 	} else {
745 761
 		if (unlikely(log_cee)) {
... ...
@@ -749,15 +765,15 @@ void ksr_slog_json(ksr_logdata_t *kld, const char *format, ...)
749 765
                         pthread_self(),
750 766
 #endif
751 767
                         kld->v_lname,
752
-			kld->v_mname, kld->v_fname, kld->v_fline, kld->v_func, s_out.s,
768
+			kld->v_mname, kld->v_fname, kld->v_fline, kld->v_func, smb, s_out.len, s_out.s, sme,
753 769
 			"kamailio", log_name!=0?log_name:"kamailio", log_fqdn,
754 770
 			(_ksr_slog_json_flags & KSR_SLOGJSON_FL_NOLOGNL)?"":"\n");
755 771
 		} else {
756
-		_km_log_func(kld->v_facility,
772
+			_km_log_func(kld->v_facility,
757 773
 				sfmt,
758 774
 				kld->v_lname, kld->v_mname, kld->v_fname, kld->v_fline,
759 775
 				kld->v_func, LOGV_CALLID_LEN, LOGV_CALLID_STR,
760
-				LOGV_PREFIX_LEN, LOGV_PREFIX_STR, prefmsg, s_out.len, s_out.s,
776
+				LOGV_PREFIX_LEN, LOGV_PREFIX_STR, prefmsg, smb, s_out.len, s_out.s, sme,
761 777
 				(_ksr_slog_json_flags & KSR_SLOGJSON_FL_NOLOGNL)?"":"\n");
762 778
 		}
763 779
 	}
... ...
@@ -807,6 +823,9 @@ void ksr_slog_init(char *ename)
807 823
 					case 'c':
808 824
 						_ksr_slog_json_flags |= KSR_SLOGJSON_FL_CALLID;
809 825
 					break;
826
+					case 'j':
827
+						_ksr_slog_json_flags |= KSR_SLOGJSON_FL_MSGJSON;
828
+					break;
810 829
 					case 'U':
811 830
 						log_cee = 1;
812 831
 					break;