1
1
import argparse
2
2
import base64
3
+ import json
3
4
import logging
5
+ import logging .config
4
6
import os
5
7
import pkgutil
6
8
import platform
14
16
import traceback
15
17
from datetime import datetime
16
18
from io import BytesIO
17
- from logging import StreamHandler
18
- from logging .handlers import RotatingFileHandler
19
19
from pathlib import Path
20
20
21
21
import jsonschema
35
35
36
36
WARNET_SERVER_PORT = 9276
37
37
CONFIG_DIR_ALREADY_EXISTS = 32001
38
+ LOGGING_CONFIG_PATH = Path ("src/logging_config/config.json" )
38
39
39
40
40
41
class Server :
@@ -60,7 +61,6 @@ def __init__(self, backend):
60
61
self .jsonrpc = JSONRPC (self .app , "/api" )
61
62
62
63
self .log_file_path = os .path .join (self .basedir , "warnet.log" )
63
- self .logger : logging .Logger
64
64
self .setup_global_exception_handler ()
65
65
self .setup_logging ()
66
66
self .setup_rpc ()
@@ -101,29 +101,20 @@ def healthy(self):
101
101
return "warnet is healthy"
102
102
103
103
def setup_logging (self ):
104
- # Ensure the directory exists
105
104
os .makedirs (os .path .dirname (self .log_file_path ), exist_ok = True )
106
105
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 )
106
+ with open (LOGGING_CONFIG_PATH ) as f :
107
+ logging_config = json .load (f )
108
+
109
+ # Update log file path
110
+ logging_config ["handlers" ]["file" ]["filename" ] = str (self .log_file_path )
111
+
112
+ # Apply the config
113
+ logging .config .dictConfig (logging_config )
114
+
120
115
self .logger = logging .getLogger ("warnet" )
121
116
self .logger .info ("Logging started" )
122
117
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
-
127
118
def log_request ():
128
119
if not request .path .startswith ("/api/" ):
129
120
self .logger .debug (request .path )
@@ -439,7 +430,7 @@ def thread_start(wn):
439
430
wn .apply_network_conditions ()
440
431
wn .wait_for_health ()
441
432
self .logger .info (
442
- f"Resumed warnet named '{ network } ' from config dir { wn .config_dir } "
433
+ f"Successfully resumed warnet named '{ network } ' from config dir { wn .config_dir } "
443
434
)
444
435
except Exception as e :
445
436
trace = traceback .format_exc ()
@@ -472,6 +463,7 @@ def thread_start(wn, lock: threading.Lock):
472
463
wn .warnet_up ()
473
464
wn .wait_for_health ()
474
465
wn .apply_network_conditions ()
466
+ self .logger .info ("Warnet started successfully" )
475
467
except Exception as e :
476
468
trace = traceback .format_exc ()
477
469
self .logger .error (f"Unhandled exception starting warnet: { e } \n { trace } " )
0 commit comments