Skip to content

Commit

Permalink
Merge pull request #61 from freifunkMUC/log_level
Browse files Browse the repository at this point in the history
 Add configurable log_level
  • Loading branch information
awlx authored Sep 8, 2021
2 parents 3b1662a + 4143ad6 commit a690f23
Show file tree
Hide file tree
Showing 7 changed files with 22 additions and 9 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/bazel.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,4 @@ jobs:
key: bazel
- uses: actions/checkout@v2
- name: Run Bazel tests
run: bazel test ...:all --test_output=all
run: bazel test ...:all --test_output=all --action_env=WGKEX_CONFIG_FILE=`pwd`/wgkex.yaml.example
3 changes: 2 additions & 1 deletion wgkex.yaml.example
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,5 @@ mqtt:
password: SECRET
keepalive: 5
tls: False
domain_prefix: myprefix-
domain_prefix: myprefix-
log_level: DEBUG
14 changes: 11 additions & 3 deletions wgkex/broker/app.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#!/usr/bin/env python3
import re
import dataclasses
import logging
from typing import Tuple, Any

from flask import Flask
Expand All @@ -14,6 +15,11 @@

from wgkex.config import config

logging.basicConfig(
format="%(asctime)s,%(msecs)d %(levelname)-8s [%(filename)s:%(lineno)d] %(message)s",
datefmt="%Y-%m-%d:%H:%M:%S",
level=config.load_config().get("log_level"),
)

WG_PUBKEY_PATTERN = re.compile(r"^[A-Za-z0-9+/]{42}[AEIMQUYcgkosw480]=$")

Expand Down Expand Up @@ -88,7 +94,7 @@ def wg_key_exchange() -> Tuple[str, int]:
domain = data.domain
# in case we want to decide here later we want to publish it only to dedicated gateways
gateway = "all"
print(f"wg_key_exchange: Domain: {domain}, Key:{key}")
logging.info(f"wg_key_exchange: Domain: {domain}, Key:{key}")

mqtt.publish(f"wireguard/{domain}/{gateway}", key)
return jsonify({"Message": "OK"}), 200
Expand All @@ -100,7 +106,7 @@ def handle_mqtt_connect(
) -> None:
"""Prints status of connect message."""
# TODO(ruairi): Clarify current usage of this function.
print(
logging.debug(
"MQTT connected to {}:{}".format(
app.config["MQTT_BROKER_URL"], app.config["MQTT_BROKER_PORT"]
)
Expand All @@ -114,7 +120,9 @@ def handle_mqtt_message(
) -> None:
"""Prints message contents."""
# TODO(ruairi): Clarify current usage of this function.
print(f"MQTT message received on {message.topic}: {message.payload.decode()}")
logging.debug(
f"MQTT message received on {message.topic}: {message.payload.decode()}"
)


def is_valid_wg_pubkey(pubkey: str) -> str:
Expand Down
6 changes: 5 additions & 1 deletion wgkex/config/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ class Config:
domains: List[str]
mqtt: MQTT
domain_prefix: str
log_level: str

@classmethod
def from_dict(cls, cfg: Dict[str, str]) -> "Config":
Expand All @@ -83,7 +84,10 @@ def from_dict(cls, cfg: Dict[str, str]) -> "Config":
"""
mqtt_cfg = MQTT.from_dict(cfg["mqtt"])
return cls(
domains=cfg["domains"], mqtt=mqtt_cfg, domain_prefix=cfg["domain_prefix"]
domains=cfg["domains"],
mqtt=mqtt_cfg,
domain_prefix=cfg["domain_prefix"],
log_level=cfg["log_level"],
)


Expand Down
2 changes: 1 addition & 1 deletion wgkex/config/config_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import config
import yaml

_VALID_CFG = "domain_prefix: ffmuc_\ndomains:\n- a\n- b\nmqtt:\n broker_port: 1883\n broker_url: mqtt://broker\n keepalive: 5\n password: pass\n tls: true\n username: user\n"
_VALID_CFG = "domain_prefix: ffmuc_\nlog_level: DEBUG\ndomains:\n- a\n- b\nmqtt:\n broker_port: 1883\n broker_url: mqtt://broker\n keepalive: 5\n password: pass\n tls: true\n username: user\n"
_INVALID_LINT = "domain_prefix: ffmuc_\nBAD_KEY_FOR_DOMAIN:\n- a\n- b\nmqtt:\n broker_port: 1883\n broker_url: mqtt://broker\n keepalive: 5\n password: pass\n tls: true\n username: user\n"
_INVALID_CFG = "asdasdasdasd"

Expand Down
2 changes: 1 addition & 1 deletion wgkex/worker/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
logging.basicConfig(
format="%(asctime)s,%(msecs)d %(levelname)-8s [%(filename)s:%(lineno)d] %(message)s",
datefmt="%Y-%m-%d:%H:%M:%S",
level=logging.DEBUG,
level=config.load_config().get("log_level"),
)

_CLEANUP_TIME = 300
Expand Down
2 changes: 1 addition & 1 deletion wgkex/worker/mqtt.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
logging.basicConfig(
format="%(asctime)s,%(msecs)d %(levelname)-8s [%(filename)s:%(lineno)d] %(message)s",
datefmt="%Y-%m-%d:%H:%M:%S",
level=logging.DEBUG,
level=load_config().get("log_level"),
)


Expand Down

0 comments on commit a690f23

Please sign in to comment.