1616from app .utils .utils import remove_str_prefix , sanitize_for_logging
1717
1818DEBUG_LOG_FORMAT = "%(asctime)s.%(msecs)03d | %(short_logger_name)-10.15s | %(levelname)-8s | %(correlation_id)s | %(filename)s:%(lineno)d | %(message)s"
19- LOG_FORMAT = "%(asctime)s.%(msecs)03d | %(short_logger_name)-10.15s | %(levelname)-8s | %(correlation_id)s | %(message)s"
19+ LOG_FORMAT = (
20+ "%(asctime)s.%(msecs)03d | %(short_logger_name)-10.15s | %(levelname)-8s | %(correlation_id)s | %(message)s"
21+ )
22+
2023
2124class PackageOnlyFilter (logging .Filter ):
2225 """
2326 Logging filter that allows only log records originating from this application package.
2427 """
28+
2529 def filter (self , record ):
2630 return record .name .startswith (__name__ )
2731
@@ -36,17 +40,15 @@ class LogFormatFilter(logging.Filter):
3640
3741 def filter (self , record ):
3842 record .correlation_id = (
39- getattr (g , "correlation_id" , CORRELATION_ID_FALLBACK )
40- if has_app_context ()
41- else CORRELATION_ID_FALLBACK
43+ getattr (g , "correlation_id" , CORRELATION_ID_FALLBACK ) if has_app_context () else CORRELATION_ID_FALLBACK
4244 )
43- record .short_logger_name = record .name .split ("." )[0 ] # extract main module name
45+ record .short_logger_name = record .name .split ("." )[0 ] # extract main module name
4446 return True
4547
4648
4749def _loader_include (loader , node ):
4850 nome_file = os .path .join (os .path .dirname (loader .name ), loader .construct_scalar (node ))
49- with open (nome_file , 'r' , encoding = ' utf-8' ) as f :
51+ with open (nome_file , "r" , encoding = " utf-8" ) as f :
5052 return yaml .load (f , Loader = type (loader ))
5153
5254
@@ -60,13 +62,12 @@ def _loader_env_var(loader, node):
6062 raw_value = loader .construct_scalar (node )
6163 new_value = os .environ .get (raw_value )
6264 if new_value is None :
63- msg = "Cannot construct value from {node}: {value}" .format (
64- node = node , value = new_value
65- )
65+ msg = "Cannot construct value from {node}: {value}" .format (node = node , value = new_value )
6666 raise yaml .YAMLError (msg )
6767 return new_value
6868
69- yaml .SafeLoader .add_constructor ('!INCLUDE' , _loader_include )
69+
70+ yaml .SafeLoader .add_constructor ("!INCLUDE" , _loader_include )
7071yaml .SafeLoader .add_constructor ("!ENV" , _loader_env_var )
7172
7273
@@ -82,28 +83,33 @@ def setup_logging(app):
8283 fmt = logging .Formatter (LOG_FORMAT , datefmt = "%Y-%m-%d %H:%M:%S" )
8384
8485 if settings .filename and settings .filepath :
85- _handler = RotatingFileHandler (os .path .join (settings .filepath , settings .filename ), maxBytes = 10 * 1024 * 1024 , backupCount = 5 , encoding = "utf-8" )
86+ _handler = RotatingFileHandler (
87+ os .path .join (settings .filepath , settings .filename ),
88+ maxBytes = 10 * 1024 * 1024 ,
89+ backupCount = 5 ,
90+ encoding = "utf-8" ,
91+ )
8692 else :
8793 _handler = logging .StreamHandler (sys .stdout )
8894
8995 _handler .setLevel (_level )
9096 _handler .setFormatter (fmt )
9197 _handler .addFilter (LogFormatFilter ())
9298
93- #configure ROOT logger
99+ # configure ROOT logger
94100 root_logger = logging .getLogger ()
95101 root_logger .setLevel (_level )
96102 root_logger .handlers .clear ()
97103 root_logger .addHandler (_handler )
98104
99- app .logger .setLevel (_level ) # flask
105+ app .logger .setLevel (_level ) # flask
100106
101- if not settings .libs_enabled : # disable lib logger
107+ if not settings .libs_enabled : # disable lib logger
102108 _handler .addFilter (PackageOnlyFilter ())
103109 else :
104- logging .getLogger ("werkzeug" ) # init a lazy logger otherwise not in loggerDict
110+ logging .getLogger ("werkzeug" ) # init a lazy logger otherwise not in loggerDict
105111 for logger_name in logging .Logger .manager .loggerDict :
106- if logger_name .startswith (__name__ ): # skip app module
112+ if logger_name .startswith (__name__ ): # skip app module
107113 continue
108114 logging .getLogger (logger_name ).setLevel (settings .libs_level )
109115
@@ -115,23 +121,21 @@ def load_config(app):
115121 raise ValueError (f"Failed to load configuration: The folder { config_path } does not exist." )
116122
117123 try :
118- with open (os .path .join (config_path , "app_config.yaml" ), 'r' , encoding = ' utf-8' ) as f :
124+ with open (os .path .join (config_path , "app_config.yaml" ), "r" , encoding = " utf-8" ) as f :
119125 _config = yaml .safe_load (f )
120126 config = AppConfig .model_validate (_config )
121127 app .config .update (dict (SETTINGS = config ))
122128
123129 config_data = {
124- nome : getattr (config , nome )
125- for nome , field in config .model_fields .items ()
126- if field .annotation is dict
130+ nome : getattr (config , nome ) for nome , field in config .model_fields .items () if field .annotation is dict
127131 }
128132 app .config .update (config_data )
129133 except Exception as e :
130134 raise ValueError ("Failed to load configuration" ) from e
131135
132136
133137def create_app ():
134- #setup app
138+ # setup app
135139 app = Flask (__name__ )
136140 load_config (app )
137141 app .secret_key = app .config [APP_SETTINGS_KEY ].app .secret_key
0 commit comments