From e4908b51aa80ac7c4060ce8c75aedfcc3a4d297b Mon Sep 17 00:00:00 2001 From: Thomas Kaul <4159106+dtslvr@users.noreply.github.com> Date: Mon, 7 Mar 2022 17:20:07 +0100 Subject: [PATCH] Feature/add context to logger (#745) * Add contexts * Update changelog --- CHANGELOG.md | 6 ++ apps/api/src/app/auth/google.strategy.ts | 2 +- apps/api/src/app/auth/web-auth.service.ts | 4 +- apps/api/src/app/import/import.controller.ts | 2 +- apps/api/src/app/info/info.service.ts | 4 +- .../app/portfolio/portfolio-calculator-new.ts | 6 +- .../src/app/portfolio/portfolio-calculator.ts | 14 ++-- .../subscription/subscription.controller.ts | 10 ++- .../app/subscription/subscription.service.ts | 2 +- apps/api/src/app/symbol/symbol.service.ts | 2 +- .../src/services/data-gathering.service.ts | 64 +++++++++++++------ .../alpha-vantage/alpha-vantage.service.ts | 2 +- .../data-provider/data-provider.service.ts | 2 +- .../ghostfolio-scraper-api.service.ts | 4 +- .../google-sheets/google-sheets.service.ts | 4 +- .../rakuten-rapid-api.service.ts | 4 +- .../yahoo-finance/yahoo-finance.service.ts | 7 +- .../services/exchange-rate-data.service.ts | 3 +- .../twitter-bot/twitter-bot.service.ts | 5 +- 19 files changed, 98 insertions(+), 49 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index d2aea9101..d6391dab6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,12 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). +## Unreleased + +### Added + +- Added the contexts to the logger commands + ## 1.124.0 - 06.03.2022 ### Added diff --git a/apps/api/src/app/auth/google.strategy.ts b/apps/api/src/app/auth/google.strategy.ts index 43def1baf..c8fb260b7 100644 --- a/apps/api/src/app/auth/google.strategy.ts +++ b/apps/api/src/app/auth/google.strategy.ts @@ -42,7 +42,7 @@ export class GoogleStrategy extends PassportStrategy(Strategy, 'google') { done(null, user); } catch (error) { - Logger.error(error); + Logger.error(error, 'GoogleStrategy'); done(error, false); } } diff --git a/apps/api/src/app/auth/web-auth.service.ts b/apps/api/src/app/auth/web-auth.service.ts index ba60b028b..9212a2e07 100644 --- a/apps/api/src/app/auth/web-auth.service.ts +++ b/apps/api/src/app/auth/web-auth.service.ts @@ -95,7 +95,7 @@ export class WebAuthService { }; verification = await verifyRegistrationResponse(opts); } catch (error) { - Logger.error(error); + Logger.error(error, 'WebAuthService'); throw new InternalServerErrorException(error.message); } @@ -193,7 +193,7 @@ export class WebAuthService { }; verification = verifyAuthenticationResponse(opts); } catch (error) { - Logger.error(error); + Logger.error(error, 'WebAuthService'); throw new InternalServerErrorException({ error: error.message }); } diff --git a/apps/api/src/app/import/import.controller.ts b/apps/api/src/app/import/import.controller.ts index 9ae66247d..d14bd69af 100644 --- a/apps/api/src/app/import/import.controller.ts +++ b/apps/api/src/app/import/import.controller.ts @@ -40,7 +40,7 @@ export class ImportController { userId: this.request.user.id }); } catch (error) { - Logger.error(error); + Logger.error(error, ImportController); throw new HttpException( { diff --git a/apps/api/src/app/info/info.service.ts b/apps/api/src/app/info/info.service.ts index f13679efc..67bd62a62 100644 --- a/apps/api/src/app/info/info.service.ts +++ b/apps/api/src/app/info/info.service.ts @@ -144,7 +144,7 @@ export class InfoService { const contributors = await get(); return contributors?.length; } catch (error) { - Logger.error(error); + Logger.error(error, 'InfoService'); return undefined; } @@ -165,7 +165,7 @@ export class InfoService { const { stargazers_count } = await get(); return stargazers_count; } catch (error) { - Logger.error(error); + Logger.error(error, 'InfoService'); return undefined; } diff --git a/apps/api/src/app/portfolio/portfolio-calculator-new.ts b/apps/api/src/app/portfolio/portfolio-calculator-new.ts index 972d4db3c..d1ed8fa91 100644 --- a/apps/api/src/app/portfolio/portfolio-calculator-new.ts +++ b/apps/api/src/app/portfolio/portfolio-calculator-new.ts @@ -458,7 +458,8 @@ export class PortfolioCalculatorNew { ); } else if (!currentPosition.quantity.eq(0)) { Logger.warn( - `Missing initial value for symbol ${currentPosition.symbol} at ${currentPosition.firstBuyDate}` + `Missing initial value for symbol ${currentPosition.symbol} at ${currentPosition.firstBuyDate}`, + 'PortfolioCalculatorNew' ); hasErrors = true; } @@ -523,7 +524,8 @@ export class PortfolioCalculatorNew { } catch (error) { Logger.error( `Failed to fetch info for date ${startDate} with exception`, - error + error, + 'PortfolioCalculatorNew' ); return null; } diff --git a/apps/api/src/app/portfolio/portfolio-calculator.ts b/apps/api/src/app/portfolio/portfolio-calculator.ts index 1bdc03deb..2dd11e0eb 100644 --- a/apps/api/src/app/portfolio/portfolio-calculator.ts +++ b/apps/api/src/app/portfolio/portfolio-calculator.ts @@ -238,7 +238,10 @@ export class PortfolioCalculator { if (!marketSymbolMap[nextDate]?.[item.symbol]) { invalidSymbols.push(item.symbol); hasErrors = true; - Logger.warn(`Missing value for symbol ${item.symbol} at ${nextDate}`); + Logger.warn( + `Missing value for symbol ${item.symbol} at ${nextDate}`, + 'PortfolioCalculator' + ); continue; } let lastInvestment: Big = new Big(0); @@ -270,7 +273,8 @@ export class PortfolioCalculator { invalidSymbols.push(item.symbol); hasErrors = true; Logger.warn( - `Missing value for symbol ${item.symbol} at ${currentDate}` + `Missing value for symbol ${item.symbol} at ${currentDate}`, + 'PortfolioCalculator' ); continue; } @@ -514,7 +518,8 @@ export class PortfolioCalculator { ); } else if (!currentPosition.quantity.eq(0)) { Logger.warn( - `Missing initial value for symbol ${currentPosition.symbol} at ${currentPosition.firstBuyDate}` + `Missing initial value for symbol ${currentPosition.symbol} at ${currentPosition.firstBuyDate}`, + 'PortfolioCalculator' ); hasErrors = true; } @@ -581,7 +586,8 @@ export class PortfolioCalculator { } catch (error) { Logger.error( `Failed to fetch info for date ${startDate} with exception`, - error + error, + 'PortfolioCalculator' ); return null; } diff --git a/apps/api/src/app/subscription/subscription.controller.ts b/apps/api/src/app/subscription/subscription.controller.ts index e064b01a5..aabc46d24 100644 --- a/apps/api/src/app/subscription/subscription.controller.ts +++ b/apps/api/src/app/subscription/subscription.controller.ts @@ -72,7 +72,8 @@ export class SubscriptionController { }); Logger.log( - `Subscription for user '${this.request.user.id}' has been created with a coupon for ${coupon.duration}` + `Subscription for user '${this.request.user.id}' has been created with a coupon for ${coupon.duration}`, + 'SubscriptionController' ); return { @@ -87,7 +88,10 @@ export class SubscriptionController { req.query.checkoutSessionId ); - Logger.log(`Subscription for user '${userId}' has been created via Stripe`); + Logger.log( + `Subscription for user '${userId}' has been created via Stripe`, + 'SubscriptionController' + ); res.redirect(`${this.configurationService.get('ROOT_URL')}/account`); } @@ -104,7 +108,7 @@ export class SubscriptionController { userId: this.request.user.id }); } catch (error) { - Logger.error(error); + Logger.error(error, 'SubscriptionController'); throw new HttpException( getReasonPhrase(StatusCodes.BAD_REQUEST), diff --git a/apps/api/src/app/subscription/subscription.service.ts b/apps/api/src/app/subscription/subscription.service.ts index 1111ac0e0..f7db04728 100644 --- a/apps/api/src/app/subscription/subscription.service.ts +++ b/apps/api/src/app/subscription/subscription.service.ts @@ -98,7 +98,7 @@ export class SubscriptionService { return session.client_reference_id; } catch (error) { - Logger.error(error); + Logger.error(error, 'SubscriptionService'); } } diff --git a/apps/api/src/app/symbol/symbol.service.ts b/apps/api/src/app/symbol/symbol.service.ts index 8d73617c6..c45f45cd1 100644 --- a/apps/api/src/app/symbol/symbol.service.ts +++ b/apps/api/src/app/symbol/symbol.service.ts @@ -95,7 +95,7 @@ export class SymbolService { results.items = items; return results; } catch (error) { - Logger.error(error); + Logger.error(error, 'SymbolService'); throw error; } diff --git a/apps/api/src/services/data-gathering.service.ts b/apps/api/src/services/data-gathering.service.ts index ea70d7be8..c3a7f64c7 100644 --- a/apps/api/src/services/data-gathering.service.ts +++ b/apps/api/src/services/data-gathering.service.ts @@ -40,7 +40,7 @@ export class DataGatheringService { const isDataGatheringNeeded = await this.isDataGatheringNeeded(); if (isDataGatheringNeeded) { - Logger.log('7d data gathering has been started.'); + Logger.log('7d data gathering has been started.', 'DataGatheringService'); console.time('data-gathering-7d'); await this.prismaService.property.create({ @@ -64,7 +64,7 @@ export class DataGatheringService { where: { key: PROPERTY_LAST_DATA_GATHERING } }); } catch (error) { - Logger.error(error); + Logger.error(error, 'DataGatheringService'); } await this.prismaService.property.delete({ @@ -73,7 +73,10 @@ export class DataGatheringService { } }); - Logger.log('7d data gathering has been completed.'); + Logger.log( + '7d data gathering has been completed.', + 'DataGatheringService' + ); console.timeEnd('data-gathering-7d'); } } @@ -84,7 +87,10 @@ export class DataGatheringService { }); if (!isDataGatheringLocked) { - Logger.log('Max data gathering has been started.'); + Logger.log( + 'Max data gathering has been started.', + 'DataGatheringService' + ); console.time('data-gathering-max'); await this.prismaService.property.create({ @@ -108,7 +114,7 @@ export class DataGatheringService { where: { key: PROPERTY_LAST_DATA_GATHERING } }); } catch (error) { - Logger.error(error); + Logger.error(error, 'DataGatheringService'); } await this.prismaService.property.delete({ @@ -117,7 +123,10 @@ export class DataGatheringService { } }); - Logger.log('Max data gathering has been completed.'); + Logger.log( + 'Max data gathering has been completed.', + 'DataGatheringService' + ); console.timeEnd('data-gathering-max'); } } @@ -128,7 +137,10 @@ export class DataGatheringService { }); if (!isDataGatheringLocked) { - Logger.log(`Symbol data gathering for ${symbol} has been started.`); + Logger.log( + `Symbol data gathering for ${symbol} has been started.`, + 'DataGatheringService' + ); console.time('data-gathering-symbol'); await this.prismaService.property.create({ @@ -159,7 +171,7 @@ export class DataGatheringService { where: { key: PROPERTY_LAST_DATA_GATHERING } }); } catch (error) { - Logger.error(error); + Logger.error(error, 'DataGatheringService'); } await this.prismaService.property.delete({ @@ -168,7 +180,10 @@ export class DataGatheringService { } }); - Logger.log(`Symbol data gathering for ${symbol} has been completed.`); + Logger.log( + `Symbol data gathering for ${symbol} has been completed.`, + 'DataGatheringService' + ); console.timeEnd('data-gathering-symbol'); } } @@ -205,14 +220,17 @@ export class DataGatheringService { }); } } catch (error) { - Logger.error(error); + Logger.error(error, 'DataGatheringService'); } finally { return undefined; } } public async gatherProfileData(aDataGatheringItems?: IDataGatheringItem[]) { - Logger.log('Profile data gathering has been started.'); + Logger.log( + 'Profile data gathering has been started.', + 'DataGatheringService' + ); console.time('data-gathering-profile'); let dataGatheringItems = aDataGatheringItems?.filter( @@ -248,7 +266,8 @@ export class DataGatheringService { } catch (error) { Logger.error( `Failed to enhance data for symbol ${symbol} by ${dataEnhancer.getName()}`, - error + error, + 'DataGatheringService' ); } } @@ -294,11 +313,18 @@ export class DataGatheringService { } }); } catch (error) { - Logger.error(`${symbol}: ${error?.meta?.cause}`); + Logger.error( + `${symbol}: ${error?.meta?.cause}`, + error, + 'DataGatheringService' + ); } } - Logger.log('Profile data gathering has been completed.'); + Logger.log( + 'Profile data gathering has been completed.', + 'DataGatheringService' + ); console.timeEnd('data-gathering-profile'); } @@ -361,7 +387,8 @@ export class DataGatheringService { `Failed to gather data for symbol ${symbol} from ${dataSource} at ${format( currentDate, DATE_FORMAT - )}.` + )}.`, + 'DataGatheringService' ); } @@ -377,14 +404,15 @@ export class DataGatheringService { } } catch (error) { hasError = true; - Logger.error(error); + Logger.error(error, 'DataGatheringService'); } if (symbolCounter > 0 && symbolCounter % 100 === 0) { Logger.log( `Data gathering progress: ${( this.dataGatheringProgress * 100 - ).toFixed(2)}%` + ).toFixed(2)}%`, + 'DataGatheringService' ); } @@ -474,7 +502,7 @@ export class DataGatheringService { } public async reset() { - Logger.log('Data gathering has been reset.'); + Logger.log('Data gathering has been reset.', 'DataGatheringService'); await this.prismaService.property.deleteMany({ where: { diff --git a/apps/api/src/services/data-provider/alpha-vantage/alpha-vantage.service.ts b/apps/api/src/services/data-provider/alpha-vantage/alpha-vantage.service.ts index e85bc9ba8..bff966fe3 100644 --- a/apps/api/src/services/data-provider/alpha-vantage/alpha-vantage.service.ts +++ b/apps/api/src/services/data-provider/alpha-vantage/alpha-vantage.service.ts @@ -76,7 +76,7 @@ export class AlphaVantageService implements DataProviderInterface { return response; } catch (error) { - Logger.error(error, symbol); + Logger.error(error, 'AlphaVantageService'); return {}; } diff --git a/apps/api/src/services/data-provider/data-provider.service.ts b/apps/api/src/services/data-provider/data-provider.service.ts index 71cc293d4..fd44f2426 100644 --- a/apps/api/src/services/data-provider/data-provider.service.ts +++ b/apps/api/src/services/data-provider/data-provider.service.ts @@ -82,7 +82,7 @@ export class DataProviderService { return r; }, {}); } catch (error) { - Logger.error(error); + Logger.error(error, 'DataProviderService'); } finally { return response; } diff --git a/apps/api/src/services/data-provider/ghostfolio-scraper-api/ghostfolio-scraper-api.service.ts b/apps/api/src/services/data-provider/ghostfolio-scraper-api/ghostfolio-scraper-api.service.ts index 837dc2af3..35c53bc7a 100644 --- a/apps/api/src/services/data-provider/ghostfolio-scraper-api/ghostfolio-scraper-api.service.ts +++ b/apps/api/src/services/data-provider/ghostfolio-scraper-api/ghostfolio-scraper-api.service.ts @@ -69,7 +69,7 @@ export class GhostfolioScraperApiService implements DataProviderInterface { } }; } catch (error) { - Logger.error(error); + Logger.error(error, 'GhostfolioScraperApiService'); } return {}; @@ -110,7 +110,7 @@ export class GhostfolioScraperApiService implements DataProviderInterface { } }; } catch (error) { - Logger.error(error); + Logger.error(error, 'GhostfolioScraperApiService'); } return {}; diff --git a/apps/api/src/services/data-provider/google-sheets/google-sheets.service.ts b/apps/api/src/services/data-provider/google-sheets/google-sheets.service.ts index 3bc427fd3..16e18f529 100644 --- a/apps/api/src/services/data-provider/google-sheets/google-sheets.service.ts +++ b/apps/api/src/services/data-provider/google-sheets/google-sheets.service.ts @@ -72,7 +72,7 @@ export class GoogleSheetsService implements DataProviderInterface { [symbol]: historicalData }; } catch (error) { - Logger.error(error); + Logger.error(error, 'GoogleSheetsService'); } return {}; @@ -121,7 +121,7 @@ export class GoogleSheetsService implements DataProviderInterface { return response; } catch (error) { - Logger.error(error); + Logger.error(error, 'GoogleSheetsService'); } return {}; diff --git a/apps/api/src/services/data-provider/rakuten-rapid-api/rakuten-rapid-api.service.ts b/apps/api/src/services/data-provider/rakuten-rapid-api/rakuten-rapid-api.service.ts index 8d5a8d5e7..a15636956 100644 --- a/apps/api/src/services/data-provider/rakuten-rapid-api/rakuten-rapid-api.service.ts +++ b/apps/api/src/services/data-provider/rakuten-rapid-api/rakuten-rapid-api.service.ts @@ -125,7 +125,7 @@ export class RakutenRapidApiService implements DataProviderInterface { }; } } catch (error) { - Logger.error(error); + Logger.error(error, 'RakutenRapidApiService'); } return {}; @@ -160,7 +160,7 @@ export class RakutenRapidApiService implements DataProviderInterface { const { fgi } = await get(); return fgi; } catch (error) { - Logger.error(error); + Logger.error(error, 'RakutenRapidApiService'); return undefined; } diff --git a/apps/api/src/services/data-provider/yahoo-finance/yahoo-finance.service.ts b/apps/api/src/services/data-provider/yahoo-finance/yahoo-finance.service.ts index 706119c29..95a14c1ab 100644 --- a/apps/api/src/services/data-provider/yahoo-finance/yahoo-finance.service.ts +++ b/apps/api/src/services/data-provider/yahoo-finance/yahoo-finance.service.ts @@ -177,7 +177,8 @@ export class YahooFinanceService implements DataProviderInterface { return response; } catch (error) { Logger.warn( - `Skipping yahooFinance2.getHistorical("${aSymbol}"): [${error.name}] ${error.message}` + `Skipping yahooFinance2.getHistorical("${aSymbol}"): [${error.name}] ${error.message}`, + 'YahooFinanceService' ); return {}; @@ -232,7 +233,7 @@ export class YahooFinanceService implements DataProviderInterface { return response; } catch (error) { - Logger.error(error); + Logger.error(error, 'YahooFinanceService'); return {}; } @@ -296,7 +297,7 @@ export class YahooFinanceService implements DataProviderInterface { }); } } catch (error) { - Logger.error(error); + Logger.error(error, 'YahooFinanceService'); } return { items }; diff --git a/apps/api/src/services/exchange-rate-data.service.ts b/apps/api/src/services/exchange-rate-data.service.ts index 53a6f0f6e..8092f1804 100644 --- a/apps/api/src/services/exchange-rate-data.service.ts +++ b/apps/api/src/services/exchange-rate-data.service.ts @@ -149,7 +149,8 @@ export class ExchangeRateDataService { // Fallback with error, if currencies are not available Logger.error( - `No exchange rate has been found for ${aFromCurrency}${aToCurrency}` + `No exchange rate has been found for ${aFromCurrency}${aToCurrency}`, + 'ExchangeRateDataService' ); return aValue; } diff --git a/apps/api/src/services/twitter-bot/twitter-bot.service.ts b/apps/api/src/services/twitter-bot/twitter-bot.service.ts index b18312279..58052872b 100644 --- a/apps/api/src/services/twitter-bot/twitter-bot.service.ts +++ b/apps/api/src/services/twitter-bot/twitter-bot.service.ts @@ -54,11 +54,12 @@ export class TwitterBotService { ); Logger.log( - `Fear & Greed Index has been tweeted: https://twitter.com/ghostfolio_/status/${createdTweet.id}` + `Fear & Greed Index has been tweeted: https://twitter.com/ghostfolio_/status/${createdTweet.id}`, + 'TwitterBotService' ); } } catch (error) { - Logger.error(error); + Logger.error(error, 'TwitterBotService'); } } }