From 4709887fef27756fd15fc6b7bf1152e18e414a08 Mon Sep 17 00:00:00 2001 From: Bojan Kucera Date: Sat, 7 Jun 2025 09:33:54 -0400 Subject: [PATCH] working on fixing logger --- .../src/services/proxy.service.ts | 51 ++++--------------- libs/http-client/src/types.ts | 1 + libs/logger/src/logger.ts | 9 ++-- 3 files changed, 16 insertions(+), 45 deletions(-) diff --git a/apps/data-service/src/services/proxy.service.ts b/apps/data-service/src/services/proxy.service.ts index a9655cf..9ae1aa8 100644 --- a/apps/data-service/src/services/proxy.service.ts +++ b/apps/data-service/src/services/proxy.service.ts @@ -1,7 +1,6 @@ -import { createLogger } from '@stock-bot/logger'; +import { Logger } from '@stock-bot/logger'; import createCache, { type CacheProvider } from '@stock-bot/cache'; import { HttpClient, HttpClientConfig, ProxyConfig , RequestConfig } from '@stock-bot/http-client'; -import type { Logger as PinoLogger } from 'pino'; export interface ProxySource { url: string; @@ -33,7 +32,7 @@ export interface ProxyData extends ProxyConfig { } export class ProxyService { - private logger: PinoLogger; + private logger; private cache: CacheProvider; private httpClient: HttpClient; private readonly CACHE_PREFIX = 'proxy:'; @@ -73,7 +72,7 @@ export class ProxyService { ]; constructor() { - this.logger = createLogger('proxy-service'); + this.logger = new Logger('proxy-service'); this.cache = createCache('hybrid'); @@ -142,7 +141,6 @@ export class ProxyService { // Start validation of new proxies this.validateProxiesInBackground(uniqueProxies); - return uniqueProxies.length; } /** @@ -231,33 +229,6 @@ export class ProxyService { return true; }); } - /** - * Store proxies in cache - */ - private async storeProxies(proxies: ProxyConfig[]): Promise { - try { - for (const proxy of proxies) { - const key = this.getProxyKey(proxy); - const data: ProxyData = { - protocol: proxy.protocol, - host: proxy.host, - port: proxy.port, - username: proxy.username, - password: proxy.password, - addedAt: new Date(), - lastChecked: null, - isWorking: null, - responseTime: null - }; - - await this.cache.set(key, data, 86400); // 24 hours TTL - } - - this.logger.info('Stored proxies in cache', { count: proxies.length }); - } catch (error) { - this.logger.error('Error storing proxies', { error }); - } - } /** * Check if a proxy is working @@ -265,16 +236,14 @@ export class ProxyService { async checkProxy(proxy: ProxyConfig, checkUrl: string = this.DEFAULT_CHECK_URL): Promise { const startTime = Date.now(); try { - - // Create a new HttpClient instance with the proxy - const proxyClient = new HttpClient({ - timeout: this.CHECK_TIMEOUT, - proxy: proxy + this.logger.debug('Proxy check initiate request', { + proxy: proxy.host + ':' + proxy.port, }); - - const response = await proxyClient.get(checkUrl); + const response = await this.httpClient.get(checkUrl, {proxy: proxy, timeout: this.CHECK_TIMEOUT}); const responseTime = Date.now() - startTime; - + this.logger.debug('Proxy check response', { + proxy: proxy.host + ':' + proxy.port, + }); if (response.status >= 200 && response.status < 300 && !response.data.contains(this.DEFAULT_IP_ADDRESS)) { const result: ProxyCheckResult = { proxy, @@ -471,7 +440,7 @@ export class ProxyService { */ private async validateProxiesInBackground(proxies: ProxyConfig[]): Promise { this.logger.info('Starting background proxy validation', { count: proxies.length }); - + this.logger.error('Background validation is not implemented yet, this is a placeholder function', { error: { message: 'Background validation not implemented' } }); const concurrency = 50; // Process 50 proxies concurrently const chunks = this.chunkArray(proxies, concurrency); diff --git a/libs/http-client/src/types.ts b/libs/http-client/src/types.ts index c7624c6..5f4c020 100644 --- a/libs/http-client/src/types.ts +++ b/libs/http-client/src/types.ts @@ -21,6 +21,7 @@ export interface RequestConfig { headers?: Record; body?: any; timeout?: number; + proxy?: ProxyConfig; } export interface HttpResponse { diff --git a/libs/logger/src/logger.ts b/libs/logger/src/logger.ts index 51120dc..cbf981a 100644 --- a/libs/logger/src/logger.ts +++ b/libs/logger/src/logger.ts @@ -41,11 +41,13 @@ function createTransports(serviceName: string, options?: { target: 'pino-pretty', level: loggingConfig.LOG_LEVEL, options: { - minimumLevel: 'info', colorize: true, translateTime: 'yyyy-mm-dd HH:MM:ss.l', - ignore: 'pid,hostname,service,environment,version', - messageFormat: '[{service}] {msg}', + ignore: 'pid,hostname', + // messageFormat: '[{service}] {msg}', + // errorLikeObjectKeys: ['err', 'error'], // Tell pino-pretty these are error objects + // errorProps: 'message,stack,name,code', // Which error properties to show + singleLine: true, } }); } @@ -192,7 +194,6 @@ export class Logger { const logData = { ...this.context, ...metadata, - timestamp: new Date().toISOString() }; if (typeof message === 'string') {