logging.ts 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. import { createEventLogger, createLogger } from "@shared/common/logging";
  2. import { isHttpError } from "@shared/common/async_utils";
  3. import { HTTPError } from "got/dist/source";
  4. import { MongoDB } from "winston-mongodb";
  5. import { QueryOptions } from "winston";
  6. // Workaround for https://github.com/winstonjs/winston-mongodb/issues/168
  7. MongoDB.prototype.normalizeQuery = (opts?: Record<string, unknown>) : QueryOptions => {
  8. const result = opts ?? {};
  9. result.rows = result.rows ?? result.limit ?? 10;
  10. result.start = result.start ?? 0;
  11. result.until = result.until ?? new Date();
  12. if (typeof result.until !== "object") {
  13. result.until = new Date(result.until as unknown as Date);
  14. }
  15. result.from = result.from ?? ((result.until as unknown as number) - (24 * 60 * 60 * 1000));
  16. if (typeof result.from !== "object") {
  17. result.from = new Date(result.from as unknown as Date);
  18. }
  19. result.order = result.order ?? "desc";
  20. return result as unknown as QueryOptions;
  21. };
  22. export const logger = createLogger({
  23. errorHandler: (reason) => {
  24. if (isHttpError<HTTPError>(reason)) {
  25. return new Error(`HTTPError: ${reason.request.requestUrl} failed because ${reason}\nStack trace: ${reason.stack}`);
  26. }
  27. return undefined;
  28. },
  29. });
  30. export const LOGGER_NAME = "WEB";
  31. export const eventLogger = createEventLogger({
  32. host: process.env.MONGO_DB_HOST ?? "",
  33. password: process.env.MONGO_DB_USERNAME ?? "",
  34. username: process.env.MONGO_DB_PASSWORD ?? "",
  35. name: LOGGER_NAME,
  36. });