11# Copyright 2014-2016 OpenMarket Ltd
2+ # Copyright 2021 The Matrix.org Foundation C.I.C.
23#
34# Licensed under the Apache License, Version 2.0 (the "License");
45# you may not use this file except in compliance with the License.
1819import sys
1920import threading
2021from string import Template
21- from typing import TYPE_CHECKING , Any , Dict
22+ from typing import TYPE_CHECKING , Any , Dict , Optional
2223
2324import yaml
2425from zope .interface import implementer
4041from ._base import Config , ConfigError
4142
4243if TYPE_CHECKING :
44+ from synapse .config .homeserver import HomeServerConfig
4345 from synapse .server import HomeServer
4446
4547DEFAULT_LOG_CONFIG = Template (
141143class LoggingConfig (Config ):
142144 section = "logging"
143145
144- def read_config (self , config , ** kwargs ):
146+ def read_config (self , config , ** kwargs ) -> None :
145147 if config .get ("log_file" ):
146148 raise ConfigError (LOG_FILE_ERROR )
147149 self .log_config = self .abspath (config .get ("log_config" ))
148150 self .no_redirect_stdio = config .get ("no_redirect_stdio" , False )
149151
150- def generate_config_section (self , config_dir_path , server_name , ** kwargs ):
152+ def generate_config_section (self , config_dir_path , server_name , ** kwargs ) -> str :
151153 log_config = os .path .join (config_dir_path , server_name + ".log.config" )
152154 return (
153155 """\
@@ -161,14 +163,14 @@ def generate_config_section(self, config_dir_path, server_name, **kwargs):
161163 % locals ()
162164 )
163165
164- def read_arguments (self , args ) :
166+ def read_arguments (self , args : argparse . Namespace ) -> None :
165167 if args .no_redirect_stdio is not None :
166168 self .no_redirect_stdio = args .no_redirect_stdio
167169 if args .log_file is not None :
168170 raise ConfigError (LOG_FILE_ERROR )
169171
170172 @staticmethod
171- def add_arguments (parser ) :
173+ def add_arguments (parser : argparse . ArgumentParser ) -> None :
172174 logging_group = parser .add_argument_group ("logging" )
173175 logging_group .add_argument (
174176 "-n" ,
@@ -197,7 +199,9 @@ def generate_files(self, config: Dict[str, Any], config_dir_path: str) -> None:
197199 log_config_file .write (DEFAULT_LOG_CONFIG .substitute (log_file = log_file ))
198200
199201
200- def _setup_stdlib_logging (config , log_config_path , logBeginner : LogBeginner ) -> None :
202+ def _setup_stdlib_logging (
203+ config : "HomeServerConfig" , log_config_path : Optional [str ], logBeginner : LogBeginner
204+ ) -> None :
201205 """
202206 Set up Python standard library logging.
203207 """
@@ -230,7 +234,7 @@ def _setup_stdlib_logging(config, log_config_path, logBeginner: LogBeginner) ->
230234 log_metadata_filter = MetadataFilter ({"server_name" : config .server .server_name })
231235 old_factory = logging .getLogRecordFactory ()
232236
233- def factory (* args , ** kwargs ) :
237+ def factory (* args : Any , ** kwargs : Any ) -> logging . LogRecord :
234238 record = old_factory (* args , ** kwargs )
235239 log_context_filter .filter (record )
236240 log_metadata_filter .filter (record )
@@ -297,7 +301,7 @@ def _load_logging_config(log_config_path: str) -> None:
297301 logging .config .dictConfig (log_config )
298302
299303
300- def _reload_logging_config (log_config_path ) :
304+ def _reload_logging_config (log_config_path : Optional [ str ]) -> None :
301305 """
302306 Reload the log configuration from the file and apply it.
303307 """
@@ -311,8 +315,8 @@ def _reload_logging_config(log_config_path):
311315
312316def setup_logging (
313317 hs : "HomeServer" ,
314- config ,
315- use_worker_options = False ,
318+ config : "HomeServerConfig" ,
319+ use_worker_options : bool = False ,
316320 logBeginner : LogBeginner = globalLogBeginner ,
317321) -> None :
318322 """
0 commit comments