Browse Source

add config and logger

chenhaiyang 4 years ago
parent
commit
586b131b80
3 changed files with 56 additions and 1 deletions
  1. 27 0
      app/core/config.py
  2. 29 0
      app/core/logger.py
  3. 0 1
      nohup.out

+ 27 - 0
app/core/config.py

@@ -0,0 +1,27 @@
+# -*- coding: utf-8 -*-
+
+from pathlib import Path
+
+from pydantic import BaseSettings
+
+
+class PlatformSettings(BaseSettings):
+    platform_host: str
+    platform_secret: str
+
+    class Config:
+        env_file = '.env'
+
+
+class TransferSettings(BaseSettings):
+    transfer_host: str
+
+    class Config:
+        env_file = '.env'
+
+
+class LoggerSettings(BaseSettings):
+    logs_dir: Path
+
+    class Config:
+        env_file = '.env'

+ 29 - 0
app/core/logger.py

@@ -0,0 +1,29 @@
+# -*- coding: utf-8 -*-
+
+import logging
+
+from loguru import logger
+
+
+class InterceptHandler(logging.Handler):
+    """
+    Default handler from examples in loguru documentaion.
+    See https://loguru.readthedocs.io/en/stable/overview.html#entirely-compatible-with-standard-logging
+    """
+
+    def emit(self, record: logging.LogRecord):
+        # Get corresponding Loguru level if it exists
+        try:
+            level = logger.level(record.levelname).name
+        except ValueError:
+            level = record.levelno
+
+        # Find caller from where originated the logged message
+        frame, depth = logging.currentframe(), 2
+        while frame.f_code.co_filename == logging.__file__:
+            frame = frame.f_back
+            depth += 1
+
+        logger.opt(depth=depth, exception=record.exc_info).log(
+            level, record.getMessage()
+        )

+ 0 - 1
nohup.out

@@ -1 +0,0 @@
-nohup: jupyter: No such file or directory