{"version":3,"file":"winston.mjs","names":[],"sources":["../../src/logger/winston.ts"],"sourcesContent":["import { Attributes } from '@opentelemetry/api';\nimport isPlainObject from 'lodash.isplainobject';\nimport isString from 'lodash.isstring';\nimport Transport from 'winston-transport';\nimport { jsonToString } from '../utils/json.ts';\nimport { OTelLogger } from './otelLogger.ts';\nimport type { LoggerOptions } from './types.ts';\n\nexport function parseWinstonLog(\n  log: {\n    level: string;\n    message: string | Attributes;\n  } & Attributes,\n) {\n  const { level, message, ...attributes } = log;\n  let meta = attributes;\n  const body = isString(message) ? message : jsonToString(message);\n  if (isPlainObject(message)) {\n    meta = { ...attributes, ...(message as Attributes) };\n  }\n  return { level, message: body, meta };\n}\n\nexport type WinstonTransportOptions = LoggerOptions & {\n  maxLevel?: string;\n  getCustomMeta?: () => Attributes;\n};\n\nexport class OTelWinstonTransport extends Transport {\n  private readonly logger: OTelLogger;\n  private readonly getCustomMeta?: () => Attributes;\n\n  constructor(options: WinstonTransportOptions) {\n    super({ level: options.maxLevel ?? 'info' });\n    this.getCustomMeta = options.getCustomMeta;\n    this.logger = new OTelLogger({\n      config: options.config,\n      resourceAttributes: options.resourceAttributes,\n    });\n  }\n\n  log(\n    info: {\n      level: string;\n      message: string | Attributes;\n    } & Attributes,\n    callback: () => void,\n  ) {\n    setImmediate(() => {\n      this.emit('logged', info);\n    });\n    const parsed = parseWinstonLog(info);\n    this.logger.postMessage(parsed.level, parsed.message, {\n      ...(this.getCustomMeta?.() ?? {}),\n      ...parsed.meta,\n    });\n    callback();\n  }\n\n  close() {\n    void this.logger.shutdown().finally(() => {\n      this.emit('finish');\n      this.emit('close');\n    });\n  }\n}\n"],"mappings":";;;;;;AAQA,SAAgB,gBACd,KAIA;CACA,MAAM,EAAE,OAAO,SAAS,GAAG,eAAe;CAC1C,IAAI,OAAO;CACX,MAAM,OAAO,SAAS,OAAO,IAAI,UAAU,aAAa,OAAO;CAC/D,IAAI,cAAc,OAAO,GACvB,OAAO;EAAE,GAAG;EAAY,GAAI;CAAuB;CAErD,OAAO;EAAE;EAAO,SAAS;EAAM;CAAK;AACtC;AAOA,IAAa,uBAAb,cAA0C,UAAU;CAClD;CACA;CAEA,YAAY,SAAkC;EAC5C,MAAM,EAAE,OAAO,QAAQ,YAAY,OAAO,CAAC;EAC3C,KAAK,gBAAgB,QAAQ;EAC7B,KAAK,SAAS,IAAI,WAAW;GAC3B,QAAQ,QAAQ;GAChB,oBAAoB,QAAQ;EAC9B,CAAC;CACH;CAEA,IACE,MAIA,UACA;EACA,mBAAmB;GACjB,KAAK,KAAK,UAAU,IAAI;EAC1B,CAAC;EACD,MAAM,SAAS,gBAAgB,IAAI;EACnC,KAAK,OAAO,YAAY,OAAO,OAAO,OAAO,SAAS;GACpD,GAAI,KAAK,gBAAgB,KAAK,CAAC;GAC/B,GAAG,OAAO;EACZ,CAAC;EACD,SAAS;CACX;CAEA,QAAQ;EACN,KAAU,OAAO,SAAS,CAAC,CAAC,cAAc;GACxC,KAAK,KAAK,QAAQ;GAClB,KAAK,KAAK,OAAO;EACnB,CAAC;CACH;AACF"}