Skip to content

Commit 433b07d

Browse files
committed
use loguru instead of logging
Signed-off-by: Lu Ken <bluewish.ken.lu@live.cn>
1 parent bb3035e commit 433b07d

6 files changed

Lines changed: 29 additions & 43 deletions

File tree

requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ backtrader
88
ag2
99
mplfinance
1010
ntplib
11+
loguru
1112

1213
langchain_openai
1314
langchain_core

src/gentrade/news/factory.py

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@
77
"""
88

99
import os
10-
import logging
1110
import time
1211
import threading
1312
from typing import List, Optional
13+
from loguru import logger
1414

1515
from gentrade.scraper.extractor import ArticleContentExtractor
1616

@@ -19,8 +19,6 @@
1919
from gentrade.news.rss import RssProvider
2020
from gentrade.news.finnhub import FinnhubNewsProvider
2121

22-
LOG = logging.getLogger(__name__)
23-
2422

2523
class NewsFactory:
2624
"""Factory class for creating news provider instances based on provider type.
@@ -99,15 +97,15 @@ def _fetch_thread(self, provider, aggregator, ticker, category,
9997
news = provider.fetch_stock_news(
10098
ticker, category, max_hour_interval, max_count
10199
)
102-
LOG.info(
100+
logger.info(
103101
f"Fetched {len(news)} stock news articles for {ticker} from "
104102
f"{provider.__class__.__name__}"
105103
)
106104
else:
107105
news = provider.fetch_latest_market_news(
108106
category, max_hour_interval, max_count
109107
)
110-
LOG.info(
108+
logger.info(
111109
f"Fetched {len(news)} market news articles from "
112110
f"{provider.__class__.__name__}"
113111
)
@@ -117,7 +115,7 @@ def _fetch_thread(self, provider, aggregator, ticker, category,
117115
item.summary = ace.clean_html(item.summary)
118116
if is_process:
119117
item.content = ace.extract_content(item.url)
120-
LOG.info(item.content)
118+
logger.info(item.content)
121119

122120
with aggregator.db_lock:
123121
aggregator.db.add_news(news)
@@ -142,10 +140,10 @@ def sync_news(
142140
"""
143141
current_time = time.time()
144142
if current_time < self.db.last_sync + 3600:
145-
LOG.info("Skipping sync: Last sync was less than 1 hour ago.")
143+
logger.info("Skipping sync: Last sync was less than 1 hour ago.")
146144
return
147145

148-
LOG.info("Starting news sync...")
146+
logger.info("Starting news sync...")
149147

150148
threads = []
151149
for provider in self.providers:
@@ -160,10 +158,9 @@ def sync_news(
160158
thread.join()
161159

162160
self.db.last_sync = current_time
163-
LOG.info("News sync completed.")
161+
logger.info("News sync completed.")
164162

165163
if __name__ == "__main__":
166-
logging.basicConfig(level=logging.INFO)
167164
db = NewsDatabase()
168165

169166
try:
@@ -186,18 +183,18 @@ def sync_news(
186183

187184
# Log results
188185
all_news = db.get_all_news()
189-
LOG.info(f"Total articles in database: {len(all_news)}")
186+
logger.info(f"Total articles in database: {len(all_news)}")
190187

191188
if all_news:
192-
LOG.info("Example article:")
193-
LOG.info(all_news[0].to_dict())
189+
logger.info("Example article:")
190+
logger.info(all_news[0].to_dict())
194191

195192
for news_item in all_news:
196-
LOG.info("--------------------------------")
193+
logger.info("--------------------------------")
197194
print(news_item.headline)
198195
print(news_item.url)
199196
print(news_item.content)
200-
LOG.info("--------------------------------")
197+
logger.info("--------------------------------")
201198

202199
except ValueError as e:
203-
LOG.error(f"Error during news aggregation: {e}")
200+
logger.error(f"Error during news aggregation: {e}")

src/gentrade/news/finnhub.py

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,14 @@
55
and news specific to individual stock tickers, with filtering by time interval and article count.
66
"""
77

8-
import logging
98
import time
109
from typing import List
1110
from datetime import datetime, timedelta
1211
import requests
12+
from loguru import logger
1313

1414
from gentrade.news.meta import NewsInfo, NewsProviderBase
1515

16-
LOG = logging.getLogger(__name__)
17-
18-
1916
class FinnhubNewsProvider(NewsProviderBase):
2017
"""News provider implementation for fetching news via the Finnhub.io API.
2118
@@ -87,7 +84,7 @@ def fetch_latest_market_news(
8784
return self._filter_news(news_list, max_hour_interval, max_count)
8885

8986
except requests.RequestException as e:
90-
LOG.debug(f"Error fetching market news from Finnhub: {e}")
87+
logger.debug(f"Error fetching market news from Finnhub: {e}")
9188
return []
9289

9390
def fetch_stock_news(
@@ -146,5 +143,5 @@ def fetch_stock_news(
146143
return self._filter_news(news_list, max_hour_interval, max_count)
147144

148145
except requests.RequestException as e:
149-
LOG.debug(f"Error fetching stock news from Finnhub: {e}")
146+
logger.debug(f"Error fetching stock news from Finnhub: {e}")
150147
return []

src/gentrade/news/meta.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,14 @@
99
"""
1010

1111
import abc
12-
import logging
1312
import time
1413
import hashlib
1514
from typing import Dict, List, Any, Optional
1615
from datetime import datetime
1716
from dataclasses import dataclass
18-
17+
from loguru import logger
1918
import requests
2019

21-
LOG = logging.getLogger(__name__)
22-
2320
NEWS_MARKET = [
2421
'us', 'zh', 'hk', 'cypto', 'common'
2522
]
@@ -79,7 +76,7 @@ def fetch_article_html(self) -> Optional[str]:
7976
response.raise_for_status()
8077
return response.text
8178
except requests.RequestException as e:
82-
LOG.debug(f"Failed to fetch HTML for {self.url}: {e}")
79+
logger.debug(f"Failed to fetch HTML for {self.url}: {e}")
8380
return None
8481

8582

src/gentrade/news/newsapi.py

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,13 @@
55
article count, and language, while formatting results into standardized NewsInfo objects.
66
"""
77

8-
import logging
98
from typing import List
109
from datetime import datetime, timedelta
11-
1210
import requests
11+
from loguru import logger
1312

1413
from gentrade.news.meta import NewsInfo, NewsProviderBase
1514

16-
LOG = logging.getLogger(__name__)
17-
18-
1915
class NewsApiProvider(NewsProviderBase):
2016
"""News provider that uses NewsAPI.org to fetch financial and stock-specific news.
2117
@@ -92,10 +88,10 @@ def fetch_latest_market_news(
9288
return self._filter_news(news_list, max_hour_interval, max_count)
9389

9490
except requests.RequestException as e:
95-
LOG.debug(f"Failed to fetch market news from NewsAPI.org: {e}")
91+
logger.debug(f"Failed to fetch market news from NewsAPI.org: {e}")
9692
return []
9793
except Exception as e:
98-
LOG.debug(f"Unexpected error: {e}")
94+
logger.debug(f"Unexpected error: {e}")
9995
return []
10096

10197
def fetch_stock_news(
@@ -159,5 +155,5 @@ def fetch_stock_news(
159155
return self._filter_news(news_list, max_hour_interval, max_count)
160156

161157
except requests.RequestException as e:
162-
LOG.debug(f"Failed to fetch {ticker} stock news from NewsAPI.org: {e}")
158+
logger.debug(f"Failed to fetch {ticker} stock news from NewsAPI.org: {e}")
163159
return []

src/gentrade/news/rss.py

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,14 @@
77
"""
88

99
import os
10-
import logging
1110
from typing import List
1211

1312
import requests
1413
import feedparser
14+
from loguru import logger
1515

1616
from gentrade.news.meta import NewsInfo, NewsProviderBase
1717

18-
LOG = logging.getLogger(__name__)
19-
2018

2119
class RssProvider(NewsProviderBase):
2220
"""News provider that fetches news from RSS/ATOM feeds.
@@ -63,7 +61,7 @@ def fetch_latest_market_news(
6361
parsing fails, or no valid articles exist.
6462
"""
6563
if not self.feed_url:
66-
LOG.error("RSS feed URL is missing (no explicit URL, env var, or default).")
64+
logger.error("RSS feed URL is missing (no explicit URL, env var, or default).")
6765
return []
6866

6967
# Headers to mimic browser (avoid feed server blocking) and accept RSS/XML
@@ -81,7 +79,7 @@ def fetch_latest_market_news(
8179
# Parse feed with feedparser
8280
feed = feedparser.parse(response.text)
8381
if not feed.entries:
84-
LOG.warning(f"No articles found in RSS feed: {self.feed_url}")
82+
logger.warning(f"No articles found in RSS feed: {self.feed_url}")
8583
return []
8684

8785
# Convert feed entries to standardized NewsInfo objects
@@ -111,16 +109,16 @@ def fetch_latest_market_news(
111109
return self._filter_news(news_list, max_hour_interval, max_count)
112110

113111
except requests.HTTPError as e:
114-
LOG.error(
112+
logger.error(
115113
f"HTTP error fetching RSS feed {self.feed_url}: "
116114
f"Status {e.response.status_code} - {str(e)}"
117115
)
118116
return []
119117
except requests.RequestException as e:
120-
LOG.error(f"Network error fetching RSS feed {self.feed_url}: {str(e)}")
118+
logger.error(f"Network error fetching RSS feed {self.feed_url}: {str(e)}")
121119
return []
122120
except Exception as e:
123-
LOG.error(f"Unexpected error parsing RSS feed {self.feed_url}: {str(e)}")
121+
logger.error(f"Unexpected error parsing RSS feed {self.feed_url}: {str(e)}")
124122
return []
125123

126124
def fetch_stock_news(

0 commit comments

Comments
 (0)