1+ import logging
12import platform
23from pathlib import Path
34
78from granian .server import Server
89from itsdangerous import URLSafeTimedSerializer
910from project_W_lib .config import load_config
10- from project_W_lib .logger import get_logger
11+ from project_W_lib .logger import configure_logging
1112
1213import project_W .dependencies as dp
14+ from project_W_lib .models .shared_setting_models import LoggingEnum
1315
1416from ._version import __commit_id__ , __version__
1517from .models .setting_models import Settings , SecretKeyValidated
2022)
2123
2224program_name = "project-W"
23- logger = get_logger (program_name )
2425
2526
2627@click .command ()
@@ -80,16 +81,6 @@ def main(
8081 development : bool ,
8182 root_static_files : Path | None ,
8283):
83- # post application version for debug purposes and bug reports
84- logger .info (f"Running application version { __version__ } " )
85- if __commit_id__ is None :
86- raise Exception (
87- "Couldn't read git hash from _version.py file. Make sure to install this package from a working git repository!"
88- )
89- dp .git_hash = __commit_id__ .removeprefix ("g" )
90- logger .info (f"Application was built from git hash { dp .git_hash } " )
91- logger .info (f"Python version: { platform .python_version ()} " )
92-
9384 dp .client_path = root_static_files
9485
9586 # parse config file
@@ -98,10 +89,37 @@ def main(
9889 if custom_config_path
9990 else load_config (program_name , Settings )
10091 )
92+
93+ # now we can setup the logger and everything else
94+ logging_dict = configure_logging (dp .config .logging )
95+ dp .logger = logging .getLogger (program_name )
96+ debug_enabled_anywhere = False
97+ access_log_handlers = [] # for granian
98+ if dp .config .logging .console .level == LoggingEnum .DEBUG :
99+ debug_enabled_anywhere = True
100+ access_log_handlers .append ("console" )
101+ if dp .config .logging .file .level == LoggingEnum .DEBUG :
102+ debug_enabled_anywhere = True
103+ access_log_handlers .append ("file" )
104+ if debug_enabled_anywhere :
105+ dp .logger .warning (
106+ "The level of at least one of the loggers has been set to 'DEBUG'. This will produce A LOT of logs which WILL INCLUDE SENSITIVE INFORMATION like tokens or user data, as well as result in slightly lower performance. If this is a production system, then please change the log level as soon as possible after you are done triaging a bug."
107+ )
108+
101109 dp .auth_s = URLSafeTimedSerializer (
102110 dp .config .security .secret_key .root .get_secret_value (), "Project-W"
103111 )
104112
113+ # post application version for debug purposes and bug reports
114+ dp .logger .info (f"Running application version { __version__ } " )
115+ if __commit_id__ is None :
116+ raise Exception (
117+ "Couldn't read git hash from _version.py file. Make sure to install this package from a working git repository!"
118+ )
119+ dp .git_hash = __commit_id__ .removeprefix ("g" )
120+ dp .logger .info (f"Application was built from git hash { dp .git_hash } " )
121+ dp .logger .info (f"Python version: { platform .python_version ()} " )
122+
105123 if run_periodic_tasks :
106124 execute_background_tasks ()
107125 return
@@ -130,6 +148,25 @@ def main(
130148 "address" : str (dp .config .web_server .address .ip ),
131149 "port" : dp .config .web_server .port ,
132150 "workers" : dp .config .web_server .worker_count ,
151+ "log_enabled" : True ,
152+ "log_level" : LogLevels .debug if debug_enabled_anywhere else LogLevels .debug ,
153+ "log_access_format" : '%(addr)s - "%(method)s %(path)s %(protocol)s" %(status)d %(dt_ms).3f' ,
154+ "log_access" : debug_enabled_anywhere ,
155+ "log_dictconfig" : {
156+ ** logging_dict ,
157+ "loggers" : {
158+ "_granian" : {
159+ "handlers" : list (logging_dict ["handlers" ].keys ()),
160+ "level" : "DEBUG" if debug_enabled_anywhere else "INFO" ,
161+ "propagate" : False ,
162+ },
163+ "granian.access" : {
164+ "handlers" : access_log_handlers ,
165+ "level" : "DEBUG" if debug_enabled_anywhere else "INFO" ,
166+ "propagate" : False ,
167+ },
168+ },
169+ },
133170 }
134171
135172 if dp .config .web_server .ssl :
@@ -145,8 +182,6 @@ def main(
145182 )
146183
147184 if development :
148- granian_options ["log_level" ] = LogLevels .debug
149- granian_options ["log_access" ] = True
150185 granian_options ["reload" ] = True
151186 granian_options ["reload_paths" ] = [Path (__file__ ).parent .absolute ()]
152187
0 commit comments