|
| 1 | +import fs from 'fs' |
| 2 | +import path from 'path' |
| 3 | +import { format } from 'util' |
| 4 | + |
| 5 | +import { splitData } from '@codebuff/common/util/split-data' |
| 6 | +import { env } from '@codebuff/internal' |
1 | 7 | import pino from 'pino' |
2 | 8 |
|
3 | | -export const logger = pino({ |
4 | | - level: process.env.LOG_LEVEL || 'debug', |
5 | | -}) |
| 9 | +// --- Constants --- |
| 10 | +const MAX_LENGTH = 65535 // Max total log size is sometimes 100k (sometimes 65535?) |
| 11 | +const BUFFER = 1000 // Buffer for context, etc. |
| 12 | + |
| 13 | +// Ensure debug directory exists for local environment |
| 14 | +const debugDir = path.join(__dirname, '../../../debug') |
| 15 | +if ( |
| 16 | + env.NEXT_PUBLIC_CB_ENVIRONMENT === 'dev' && |
| 17 | + process.env.CODEBUFF_GITHUB_ACTIONS !== 'true' |
| 18 | +) { |
| 19 | + try { |
| 20 | + fs.mkdirSync(debugDir, { recursive: true }) |
| 21 | + } catch (err) { |
| 22 | + console.error('Failed to create debug directory:', err) |
| 23 | + } |
| 24 | +} |
| 25 | + |
| 26 | +const pinoLogger = pino( |
| 27 | + { |
| 28 | + level: 'debug', |
| 29 | + formatters: { |
| 30 | + level: (label) => { |
| 31 | + return { level: label.toUpperCase() } |
| 32 | + }, |
| 33 | + }, |
| 34 | + timestamp: () => `,"timestamp":"${new Date(Date.now()).toISOString()}"`, |
| 35 | + }, |
| 36 | + env.NEXT_PUBLIC_CB_ENVIRONMENT === 'dev' && |
| 37 | + process.env.CODEBUFF_GITHUB_ACTIONS !== 'true' |
| 38 | + ? pino.transport({ |
| 39 | + target: 'pino/file', |
| 40 | + options: { |
| 41 | + destination: path.join(debugDir, 'web.log'), |
| 42 | + }, |
| 43 | + level: 'debug', |
| 44 | + }) |
| 45 | + : undefined, |
| 46 | +) |
| 47 | + |
| 48 | +const loggingLevels = ['info', 'debug', 'warn', 'error', 'fatal'] as const |
| 49 | +type LogLevel = (typeof loggingLevels)[number] |
| 50 | + |
| 51 | +function splitAndLog( |
| 52 | + level: LogLevel, |
| 53 | + data: any, |
| 54 | + msg?: string, |
| 55 | + ...args: any[] |
| 56 | +): void { |
| 57 | + const formattedMsg = format(msg ?? '', ...args) |
| 58 | + const availableDataLimit = MAX_LENGTH - BUFFER - formattedMsg.length |
| 59 | + |
| 60 | + // split data recursively into chunks small enough to log |
| 61 | + const processedData: any[] = splitData({ |
| 62 | + data, |
| 63 | + maxChunkSize: availableDataLimit, |
| 64 | + }) |
| 65 | + |
| 66 | + if (processedData.length === 1) { |
| 67 | + pinoLogger[level](processedData[0], msg, ...args) |
| 68 | + return |
| 69 | + } |
| 70 | + |
| 71 | + processedData.forEach((chunk, index) => { |
| 72 | + pinoLogger[level]( |
| 73 | + chunk, |
| 74 | + `${formattedMsg} (chunk ${index + 1}/${processedData.length})`, |
| 75 | + ) |
| 76 | + }) |
| 77 | +} |
| 78 | + |
| 79 | +export const logger: Record<LogLevel, pino.LogFn> = |
| 80 | + process.env.NEXT_PUBLIC_CB_ENVIRONMENT === 'dev' |
| 81 | + ? pinoLogger |
| 82 | + : (Object.fromEntries( |
| 83 | + loggingLevels.map((level) => { |
| 84 | + return [ |
| 85 | + level, |
| 86 | + (data: any, msg?: string, ...args: any[]) => |
| 87 | + splitAndLog(level, data, msg, ...args), |
| 88 | + ] |
| 89 | + }), |
| 90 | + ) as Record<LogLevel, pino.LogFn>) |
0 commit comments