working on fixing logger

This commit is contained in:
Bojan Kucera 2025-06-07 09:33:54 -04:00
parent 5b8c91f31d
commit 4709887fef
3 changed files with 16 additions and 45 deletions

View file

@ -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<void> {
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<ProxyCheckResult> {
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<void> {
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);

View file

@ -21,6 +21,7 @@ export interface RequestConfig {
headers?: Record<string, string>;
body?: any;
timeout?: number;
proxy?: ProxyConfig;
}
export interface HttpResponse<T = any> {

View file

@ -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') {