use logging

This commit is contained in:
ars
2026-06-15 03:29:42 +03:00
parent 25ca35c097
commit a321d64169
7 changed files with 91 additions and 11 deletions
+2
View File
@@ -1,6 +1,8 @@
import sys
from nexus_sync.client.runtime import main
from nexus_sync.utils import configure_logging
if __name__ == "__main__":
configure_logging()
raise SystemExit(main(sys.argv[1:]))
+7 -5
View File
@@ -1,8 +1,8 @@
import json
import logging
import os
import platform
import socket
import sys
import urllib.error
import urllib.request
from dataclasses import dataclass
@@ -27,6 +27,7 @@ CLIENT_ID_ENV = "NEXUS_SYNC_CLIENT_ID"
CLIENT_TOKEN_ENV = "NEXUS_SYNC_CLIENT_TOKEN"
CLIENT_VERSION = "0.1.0"
HEARTBEAT_PATH = "/api/v1/client/heartbeat"
logger = logging.getLogger(__name__)
class ClientConfigError(ValueError):
@@ -142,19 +143,20 @@ def main(argv: list[str] | None = None) -> int:
config = load_client_config()
result = run_once(config)
except (ClientConfigError, HeartbeatError, ValueError) as error:
print(f"nexus-sync client error: {error}", file=sys.stderr)
logger.error("nexus-sync client error: %s", error)
return 1
if result is None:
print("heartbeat accepted; no command")
logger.info("heartbeat accepted; no command")
return 0
print(
logger.info(
"command result: %s",
json.dumps(
result.model_dump(mode="json"),
ensure_ascii=False,
separators=(",", ":"),
)
),
)
return 0
+2
View File
@@ -1,9 +1,11 @@
import uvicorn
from nexus_sync.server.app import app
from nexus_sync.utils import configure_logging
def main() -> None:
configure_logging()
uvicorn.run(app, host="0.0.0.0", port=8000)
+5
View File
@@ -0,0 +1,5 @@
from nexus_sync.utils.log_config import configure_logging
__all__ = [
"configure_logging",
]
+19
View File
@@ -0,0 +1,19 @@
import logging
import os
LOG_LEVEL_ENV = "NEXUS_SYNC_LOG_LEVEL"
DEFAULT_LOG_LEVEL = "INFO"
DEFAULT_LOG_FORMAT = "%(levelname)s %(name)s %(message)s"
def configure_logging(level: str | None = None) -> None:
raw_level = level or os.environ.get(LOG_LEVEL_ENV, DEFAULT_LOG_LEVEL)
log_level = getattr(logging, raw_level.upper(), None)
if not isinstance(log_level, int):
raise ValueError(f"invalid log level: {raw_level}")
logging.basicConfig(
level=log_level,
format=DEFAULT_LOG_FORMAT,
)
logging.getLogger().setLevel(log_level)