Skip to content
This repository has been archived by the owner on Dec 20, 2024. It is now read-only.

Commit

Permalink
fix the log configuration -- direct to stdout (#38)
Browse files Browse the repository at this point in the history
  • Loading branch information
bh2smith authored May 1, 2023
1 parent 40282bb commit d46386b
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 34 deletions.
28 changes: 28 additions & 0 deletions logging.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
[loggers]
keys=root,src

[handlers]
keys=consoleHandler

[formatters]
keys=sampleFormatter

[logger_root]
level=INFO
handlers=consoleHandler
propagate=0

[logger_src]
level=DEBUG
qualname=src
handlers=consoleHandler
propagate=0

[handler_consoleHandler]
class=StreamHandler
level=DEBUG
formatter=sampleFormatter
args=(sys.stdout,)

[formatter_sampleFormatter]
format=%(asctime)s %(levelname)s %(name)s %(message)s
18 changes: 1 addition & 17 deletions src/environment.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,6 @@
"""Project Global Constants."""
import argparse
import logging
from pathlib import Path

PROJECT_ROOT = Path(__file__).parent.parent
QUERY_PATH = PROJECT_ROOT / Path("src/sql")


def parse_log_level() -> str:
"""Parses Global Log Level from runtime arguments (default = INFO)"""
parser = argparse.ArgumentParser()
parser.add_argument(
"-l", "--log-level", type=str, default="INFO", help="set log level"
)
args, _ = parser.parse_known_args()
level: str = args.log_level.upper()
logging.info(f"Global Log Level configured as {level}")
return level


LOG_LEVEL = parse_log_level()
LOG_CONFIG_FILE = PROJECT_ROOT / Path("logging.conf")
18 changes: 5 additions & 13 deletions src/logger.py
Original file line number Diff line number Diff line change
@@ -1,23 +1,15 @@
"""Easy universal log configuration """
import logging.config
import sys
from logging import Logger

from src.environment import LOG_LEVEL
from src.environment import LOG_CONFIG_FILE


def set_log(name: str) -> Logger:
"""
Instantiates and returns a logger with the name defined by the caller.
Used by any file which wants to inherit the global log level configuration
and assigns the desired naming convention (usually `__name__`)
"""
"""Removes redundancy when setting log in each file"""

log = logging.getLogger(name)
try:
log.setLevel(level=LOG_LEVEL)
except ValueError:
logging.error(f"Invalid log level: {LOG_LEVEL}")
sys.exit(1)

logging.config.fileConfig(
fname=LOG_CONFIG_FILE.absolute(), disable_existing_loggers=True
)
return log
6 changes: 2 additions & 4 deletions src/main.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
"""Main Entry point for app_hash sync"""
import argparse
import asyncio
import logging.config
import os
from dataclasses import dataclass
from pathlib import Path
Expand All @@ -10,15 +9,14 @@

from src.fetch.dune import DuneFetcher
from src.fetch.orderbook import OrderbookFetcher
from src.logger import set_log
from src.models.tables import SyncTable
from src.post.aws import AWSClient
from src.sync import sync_app_data
from src.sync.config import SyncConfig, AppDataSyncConfig
from src.sync.order_rewards import sync_order_rewards, sync_batch_rewards

log = logging.getLogger(__name__)
logging.basicConfig(format="%(asctime)s %(levelname)s %(name)s %(message)s")
log.setLevel(logging.DEBUG)
log = set_log(__name__)


@dataclass
Expand Down

0 comments on commit d46386b

Please sign in to comment.