@@ -63,6 +63,29 @@ def build(self) -> dict[str, Any]:
6363 "()" : JsonFormatter ,
6464 "include_traces" : True ,
6565 },
66+ # Stream-bound formatters stamp the record with a stream_id so the
67+ # log server can tell the streams apart on the shared syslog channel.
68+ # APP == "stroom 2", SIEM == "stroom 3".
69+ "json_app" : {
70+ "()" : JsonFormatter ,
71+ "include_traces" : False ,
72+ "stream_id" : "app" ,
73+ },
74+ "json_siem" : {
75+ "()" : JsonFormatter ,
76+ "include_traces" : False ,
77+ "stream_id" : "siem" ,
78+ },
79+ "json_public_inspect" : {
80+ "()" : JsonFormatter ,
81+ "include_traces" : False ,
82+ "stream_id" : "public_inspect" ,
83+ },
84+ "json_debug" : {
85+ "()" : JsonFormatter ,
86+ "include_traces" : True ,
87+ "stream_id" : "debug" ,
88+ },
6689 "plain" : {
6790 "()" : PlainTextFormatter ,
6891 },
@@ -96,40 +119,43 @@ def build(self) -> dict[str, Any]:
96119 "root" : {"handlers" : ["console" ], "level" : self .loglevel },
97120 }
98121
122+ # Stamp every JSON record with the configured application id so the
123+ # log server can tell apart applications sharing the syslog channel.
124+ if self .logging_config .application_id :
125+ for formatter in conf ["formatters" ].values ():
126+ if formatter ["()" ] is JsonFormatter :
127+ formatter ["application_id" ] = self .logging_config .application_id
128+
99129 self ._add_log_handlers (conf )
100130
101131 return conf
102132
103133 def _add_log_handlers (self , conf : dict [str , Any ]) -> None :
134+ # All streams share one syslog channel; each handler stamps its records
135+ # with a stream_id via its formatter so the log server can split them.
136+ path = self .logging_config .syslog_path
137+ if not path :
138+ return
139+
104140 app_logger_handlers = conf ["loggers" ]["app" ]["handlers" ]
105141 uvicorn_handlers = conf ["loggers" ]["uvicorn" ]["handlers" ]
106142 uvicorn_error_handlers = conf ["loggers" ]["uvicorn.error" ]["handlers" ]
107143
108- if self .logging_config .app_path :
109- conf ["handlers" ]["app_syslog" ] = self ._syslog_handler (
110- self .logging_config .app_path , filters = ["app_filter" ]
111- )
112- app_logger_handlers .append ("app_syslog" )
113- uvicorn_handlers .append ("app_syslog" )
114- uvicorn_error_handlers .append ("app_syslog" )
144+ conf ["handlers" ]["syslog_app" ] = self ._syslog_handler (path , formatter = "json_app" , filters = ["app_filter" ])
145+ app_logger_handlers .append ("syslog_app" )
146+ uvicorn_handlers .append ("syslog_app" )
147+ uvicorn_error_handlers .append ("syslog_app" )
115148
116- if self .logging_config .siem_path :
117- conf ["handlers" ]["siem" ] = self ._syslog_handler (
118- self .logging_config .siem_path , filters = ["siem_filter" ]
119- )
120- app_logger_handlers .append ("siem" )
149+ conf ["handlers" ]["syslog_siem" ] = self ._syslog_handler (path , formatter = "json_siem" , filters = ["siem_filter" ])
150+ app_logger_handlers .append ("syslog_siem" )
121151
122- if self .logging_config .public_inspect_path :
123- conf ["handlers" ]["public_inspect" ] = self ._syslog_handler (
124- self .logging_config .public_inspect_path , filters = ["public_inspect_filter" ]
125- )
126- app_logger_handlers .append ("public_inspect" )
152+ conf ["handlers" ]["syslog_public_inspect" ] = self ._syslog_handler (
153+ path , formatter = "json_public_inspect" , filters = ["public_inspect_filter" ]
154+ )
155+ app_logger_handlers .append ("syslog_public_inspect" )
127156
128- if self .logging_config .debug_path :
129- conf ["handlers" ]["debug" ] = self ._syslog_handler (
130- self .logging_config .debug_path , formatter = "json_traces"
131- )
132- app_logger_handlers .append ("debug" )
133- uvicorn_handlers .append ("debug" )
134- uvicorn_error_handlers .append ("debug" )
135- conf ["root" ]["handlers" ].append ("debug" )
157+ conf ["handlers" ]["syslog_debug" ] = self ._syslog_handler (path , formatter = "json_debug" )
158+ app_logger_handlers .append ("syslog_debug" )
159+ uvicorn_handlers .append ("syslog_debug" )
160+ uvicorn_error_handlers .append ("syslog_debug" )
161+ conf ["root" ]["handlers" ].append ("syslog_debug" )
0 commit comments