import { createBaseLog } from './baselog'; import { getCorrelationId, getEnvironment, getSessionId, requestToContext, } from './common'; import { AXIOM_AUTH_TOKEN_ENV, AXIOM_DATASET_ENV, AXIOM_ORG_ID_ENV, type ConsoleLog, LOG_ENV, type LogBindings, type LogTags, type RequestLike, TASK_QUEUE_HEADER, WORKFLOW_ID_HEADER, type WorkerLog, } from './type'; export * from './baselog'; export * from './common'; export * from './type'; export function useLog({ environment: _environment, name, request, body, env, correlationId: cid, sessionId: sid, version, executionContext, logRequest, consoleLog: _consoleLog, dataset: _dataset, context: initialContext = {}, }: Partial & { request?: RequestLike & { cf?: any }; logRequest?: boolean | ((raw: ReturnType) => any); body?: any; dataset?: string; env?: LogBindings; context?: Record; consoleLog?: ConsoleLog; executionContext?: { waitUntil(promise: Promise): void; }; }): WorkerLog { const environment = _environment || getEnvironment(env); // const colorLogs = env?.[LOG_ENV] === '*' || ; const correlationId = cid || getCorrelationId(request); //const logger = nodeColorLog; const sessionId = sid || getSessionId(request); const axiomToken = env?.[AXIOM_AUTH_TOKEN_ENV]; const isNodeJsCompat = typeof process !== 'undefined'; const isDev = environment === 'development'; const isLogEnabled = env?.[LOG_ENV] === '*'; const consoleLog = !axiomToken || isDev || isLogEnabled; const colorLog = consoleLog && isNodeJsCompat; const axiom = createBaseLog( { token: axiomToken, orgId: env?.[AXIOM_ORG_ID_ENV], dataset: _dataset || env?.[AXIOM_DATASET_ENV] || 'default', // color log on development and if env is * consoleLog: _consoleLog || (colorLog ? 'color' : consoleLog), }, { environment, type: 'worker', executionContext, workerId: env?.MACHINE_NAME || name || 'default', app: name || 'default', version: version || 'default', correlationId, sessionId, context: { ...initialContext, rootCorrelationId: correlationId, correlationId, sessionId, taskQueue: request?.headers?.get(TASK_QUEUE_HEADER) || undefined, workflowId: request?.headers?.get(WORKFLOW_ID_HEADER) || undefined, child: undefined as any as string, }, } ) as WorkerLog; if (logRequest !== false && request) { const { message, ...raw } = typeof logRequest === 'function' ? logRequest(requestToContext(request, body)) : requestToContext(request, body); axiom.info(message || 'Request', raw); } axiom.respond = async (res) => { const temp = await (typeof res === 'function' ? res() : res); const response = (() => { if (temp instanceof Response) { axiom?.info(`Response ${temp.status}`, { response: { body: temp .clone() .json() .catch(() => undefined), response: temp, status: temp.status, statusText: temp.statusText, headers: temp.headers, }, }); return temp; } else { // We have no response status code YET axiom?.info('Response 200', { response: temp, }); return new Response( typeof temp.body === 'object' ? JSON.stringify(temp.body) : temp.body, { status: temp.status, statusText: temp.statusText, headers: temp.headers, } ); } })(); await axiom?.flush(); return response!; }; return axiom; }