11import argparse
22import base64
3+ import json
34import logging
5+ import logging .config
46import os
57import pkgutil
68import platform
1416import traceback
1517from datetime import datetime
1618from io import BytesIO
17- from logging import StreamHandler
18- from logging .handlers import RotatingFileHandler
1919from pathlib import Path
2020
2121import jsonschema
3535
3636WARNET_SERVER_PORT = 9276
3737CONFIG_DIR_ALREADY_EXISTS = 32001
38+ LOGGING_CONFIG_PATH = Path ("src/logging_config/config.json" )
3839
3940
4041class Server :
@@ -60,12 +61,10 @@ def __init__(self, backend):
6061 self .jsonrpc = JSONRPC (self .app , "/api" )
6162
6263 self .log_file_path = os .path .join (self .basedir , "warnet.log" )
63- self .logger : logging .Logger
6464 self .setup_global_exception_handler ()
6565 self .setup_logging ()
6666 self .setup_rpc ()
6767 self .logger .info ("Started server" )
68- self .app .add_url_rule ("/-/healthy" , view_func = self .healthy )
6968
7069 # register a well known /-/healthy endpoint for liveness tests
7170 # we regard warnet as healthy if the http server is up
@@ -101,30 +100,23 @@ def healthy(self):
101100 return "warnet is healthy"
102101
103102 def setup_logging (self ):
104- # Ensure the directory exists
105103 os .makedirs (os .path .dirname (self .log_file_path ), exist_ok = True )
106104
107- # Configure root logger
108- logging .basicConfig (
109- level = logging .DEBUG ,
110- handlers = [
111- RotatingFileHandler (
112- self .log_file_path , maxBytes = 16_000_000 , backupCount = 3 , delay = True
113- ),
114- StreamHandler (sys .stdout ),
115- ],
116- format = "%(asctime)s - %(name)s - %(levelname)s - %(message)s" ,
117- )
118- # Disable urllib3.connectionpool logging
119- logging .getLogger ("urllib3.connectionpool" ).setLevel (logging .CRITICAL )
105+ with open (LOGGING_CONFIG_PATH ) as f :
106+ logging_config = json .load (f )
107+
108+ # Update log file path
109+ logging_config ["handlers" ]["file" ]["filename" ] = str (self .log_file_path )
110+
111+ # Apply the config
112+ logging .config .dictConfig (logging_config )
113+
120114 self .logger = logging .getLogger ("warnet" )
121115 self .logger .info ("Logging started" )
122116
123- if self .backend == "k8s" :
124- # if using k8s as a backend, tone the logging down
125- logging .getLogger ("kubernetes.client.rest" ).setLevel (logging .WARNING )
126-
127117 def log_request ():
118+ if "healthy" in request .path :
119+ return # No need to log all these
128120 if not request .path .startswith ("/api/" ):
129121 self .logger .debug (request .path )
130122 else :
@@ -439,7 +431,7 @@ def thread_start(wn):
439431 wn .apply_network_conditions ()
440432 wn .wait_for_health ()
441433 self .logger .info (
442- f"Resumed warnet named '{ network } ' from config dir { wn .config_dir } "
434+ f"Successfully resumed warnet named '{ network } ' from config dir { wn .config_dir } "
443435 )
444436 except Exception as e :
445437 trace = traceback .format_exc ()
@@ -472,6 +464,7 @@ def thread_start(wn, lock: threading.Lock):
472464 wn .warnet_up ()
473465 wn .wait_for_health ()
474466 wn .apply_network_conditions ()
467+ self .logger .info ("Warnet started successfully" )
475468 except Exception as e :
476469 trace = traceback .format_exc ()
477470 self .logger .error (f"Unhandled exception starting warnet: { e } \n { trace } " )
0 commit comments