5353_is_shutting_down = False
5454
5555
56+ def set_up_logging () -> None :
57+ root = logging .getLogger ()
58+ handler = logging .StreamHandler ()
59+ formatter = logging .Formatter ('%(levelname)-8s %(name)-20s %(message)s' )
60+ handler .setFormatter (formatter )
61+ root .addHandler (handler )
62+
63+ # NOTE: Nothing useful there
64+ logging .getLogger ('tortoise' ).setLevel (logging .WARNING )
65+
66+
5667def echo (message : str ) -> None :
5768 with suppress (BrokenPipeError ):
5869 click .echo (message )
@@ -62,7 +73,6 @@ def echo(message: str) -> None:
6273class CLIContext :
6374 config_paths : List [str ]
6475 config : DipDupConfig
65- logging_config : LoggingConfig
6676
6777
6878async def _shutdown () -> None :
@@ -159,7 +169,7 @@ async def _check_version() -> None:
159169 default = [DEFAULT_CONFIG_NAME ],
160170)
161171@click .option ('--env-file' , '-e' , type = str , multiple = True , help = 'A path to .env file containing `KEY=value` strings.' , default = [])
162- @click .option ('--logging-config' , '-l' , type = str , help = 'A path to Python logging config in YAML format.' , default = 'logging.yml' )
172+ @click .option ('--logging-config' , '-l' , type = str , help = 'A path to Python logging config in YAML format.' , default = None )
163173@click .pass_context
164174@cli_wrapper
165175async def cli (ctx , config : List [str ], env_file : List [str ], logging_config : str ):
@@ -173,18 +183,19 @@ async def cli(ctx, config: List[str], env_file: List[str], logging_config: str):
173183 if '--help' in sys .argv :
174184 return
175185
176- # NOTE: Search in current workdir, fallback to builtin configs
177- try :
178- path = join (os .getcwd (), logging_config )
179- _logging_config = LoggingConfig .load (path )
180- except FileNotFoundError :
181- path = join (dirname (__file__ ), 'configs' , logging_config )
182- _logging_config = LoggingConfig .load (path )
183- _logging_config .apply ()
186+ set_up_logging ()
184187
185- # NOTE: Nothing useful there
186- if 'tortoise' not in _logging_config .config ['loggers' ]:
187- logging .getLogger ('tortoise' ).setLevel (logging .WARNING )
188+ # TODO: Deprecated, remove in 6.0
189+ if logging_config :
190+ _logger .warning ('`--logging-config` option is deprecated. Use `logging` config field.' )
191+ # NOTE: Search in the current workdir, fallback to builtin configs
192+ try :
193+ path = os .path .join (os .getcwd (), logging_config )
194+ _logging_config = LoggingConfig .load (path )
195+ except FileNotFoundError :
196+ path = os .path .join (os .path .dirname (__file__ ), 'configs' , logging_config )
197+ _logging_config = LoggingConfig .load (path )
198+ _logging_config .apply ()
188199
189200 # NOTE: Apply env files before loading config
190201 for env_path in env_file :
@@ -195,13 +206,21 @@ async def cli(ctx, config: List[str], env_file: List[str], logging_config: str):
195206 load_dotenv (env_path , override = True )
196207
197208 _config = DipDupConfig .load (config )
209+
210+ # TODO: Deprecated, remove in 6.0
211+ # NOTE: Skip if Python config is already applied
212+ if not logging_config :
213+ _config .set_up_logging ()
214+
198215 # NOTE: Imports will be loaded later if needed
199216 _config .initialize (skip_imports = True )
200217 _init_sentry (_config )
201218
219+ # NOTE: Fire and forget, do not block instant commands
202220 if not _config .advanced .skip_version_check :
203221 asyncio .ensure_future (_check_version ())
204222
223+ # NOTE: Avoid import errors if project package is incomplete
205224 try :
206225 await DipDupCodeGenerator (_config , {}).create_package ()
207226 except Exception as e :
@@ -217,7 +236,6 @@ async def cli(ctx, config: List[str], env_file: List[str], logging_config: str):
217236 ctx .obj = CLIContext (
218237 config_paths = config ,
219238 config = _config ,
220- logging_config = _logging_config ,
221239 )
222240
223241
0 commit comments