Skip to content

Commit

Permalink
server: Set default log level to INFO
Browse files Browse the repository at this point in the history
To enable debug logs you can:

1. Run with --debug
2. Run with --dev which will enable hot reloads and debug logs
  • Loading branch information
willcl-ark committed Feb 29, 2024
1 parent 420ce7a commit 846d301
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 11 deletions.
31 changes: 21 additions & 10 deletions src/warnet/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@


class Server:
def __init__(self, backend):
def __init__(self, backend, debug):
self.backend = backend
system = os.name
if system == "nt" or platform.system() == "Windows":
Expand All @@ -63,7 +63,7 @@ def __init__(self, backend):
self.log_file_path = os.path.join(self.basedir, "warnet.log")
self.logger: logging.Logger
self.setup_global_exception_handler()
self.setup_logging()
self.setup_logging(debug)
self.setup_rpc()
self.logger.info(f"Started server version {SERVER_VERSION}")
self.app.add_url_rule("/-/healthy", view_func=self.healthy)
Expand Down Expand Up @@ -101,13 +101,14 @@ def handle_exception(e):
def healthy(self):
return "warnet is healthy"

def setup_logging(self):
def setup_logging(self, debug):
# Ensure the directory exists
os.makedirs(os.path.dirname(self.log_file_path), exist_ok=True)
log_level = logging.DEBUG if debug else logging.INFO

# Configure root logger
logging.basicConfig(
level=logging.DEBUG,
level=log_level,
handlers=[
RotatingFileHandler(
self.log_file_path, maxBytes=16_000_000, backupCount=3, delay=True
Expand All @@ -118,8 +119,10 @@ def setup_logging(self):
)
# Disable urllib3.connectionpool logging
logging.getLogger("urllib3.connectionpool").setLevel(logging.CRITICAL)
logging.getLogger("watchdog.observers.inotify_buffer").setLevel(logging.CRITICAL)
self.logger = logging.getLogger("warnet")
self.logger.info("Logging started")
self.logger.setLevel(log_level)
self.logger.info(f"Logging started at level {logging.getLevelName(self.logger.level)}")

if self.backend == "k8s":
# if using k8s as a backend, tone the logging down
Expand Down Expand Up @@ -473,6 +476,9 @@ def thread_start(wn, lock: threading.Lock):
wn.wait_for_health()
wn.apply_network_conditions()
wn.connect_edges()
self.logger.info(
f"Started warnet named '{network}' using config dir {wn.config_dir}"
)
except Exception as e:
trace = traceback.format_exc()
self.logger.error(f"Unhandled exception starting warnet: {e}\n{trace}")
Expand Down Expand Up @@ -623,7 +629,14 @@ def run_server():
help="Specify the backend to use",
)
parser.add_argument(
"--dev", action="store_true", help="Run in development mode with debug enabled"
"--dev",
action="store_true",
help="Run in development mode with hot-reloading of server code and debug level logs",
)
parser.add_argument(
"--debug",
action="store_true",
help="Enable debug level logs."
)

args = parser.parse_args()
Expand All @@ -632,10 +645,8 @@ def run_server():
print(f"Invalid backend {args.backend}")
sys.exit(1)

debug_mode = args.dev
server = Server(args.backend)

server.app.run(host="0.0.0.0", port=WARNET_SERVER_PORT, debug=debug_mode)
server = Server(args.backend, args.debug or args.dev)
server.app.run(host="0.0.0.0", port=WARNET_SERVER_PORT, debug=args.dev)


if __name__ == "__main__":
Expand Down
2 changes: 1 addition & 1 deletion test/test_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ def start_server(self):
print(f"\nStarting Warnet server, logging to: {self.logfilepath}")

self.server = Popen(
["warnet", "--backend", self.backend],
["warnet", "--backend", self.backend, "--debug"],
stdout=PIPE,
stderr=STDOUT,
bufsize=1,
Expand Down

0 comments on commit 846d301

Please sign in to comment.