@restura/core 1.1.0 → 1.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.d.ts CHANGED
@@ -1,3 +1,4 @@
1
+ import { TransportTargetOptions, SerializerFn, DestinationStream } from 'pino';
1
2
  import { z } from 'zod';
2
3
  import { UUID } from 'crypto';
3
4
  import * as express from 'express';
@@ -20,6 +21,7 @@ declare const logger: {
20
21
  trace: LogFunction;
21
22
  };
22
23
 
24
+ type ErrorSerializerFactory = (baseSerializer: SerializerFn) => SerializerFn;
23
25
  declare const loggerConfigSchema: z.ZodObject<{
24
26
  level: z.ZodDefault<z.ZodEnum<{
25
27
  fatal: "fatal";
@@ -30,11 +32,11 @@ declare const loggerConfigSchema: z.ZodObject<{
30
32
  silly: "silly";
31
33
  trace: "trace";
32
34
  }>>;
33
- transports: z.ZodOptional<z.ZodArray<z.ZodObject<{
34
- target: z.ZodString;
35
- level: z.ZodOptional<z.ZodString>;
36
- options: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodUnknown>>;
37
- }, z.core.$strip>>>;
35
+ transports: z.ZodOptional<z.ZodArray<z.ZodCustom<TransportTargetOptions<Record<string, any>>, TransportTargetOptions<Record<string, any>>>>>;
36
+ serializers: z.ZodOptional<z.ZodObject<{
37
+ err: z.ZodOptional<z.ZodCustom<ErrorSerializerFactory, ErrorSerializerFactory>>;
38
+ }, z.core.$strip>>;
39
+ stream: z.ZodOptional<z.ZodCustom<DestinationStream, DestinationStream>>;
38
40
  }, z.core.$strip>;
39
41
  type LoggerConfigSchema = z.infer<typeof loggerConfigSchema>;
40
42
 
package/dist/index.js CHANGED
@@ -12,18 +12,20 @@ var __decorateClass = (decorators, target, key, kind) => {
12
12
  // src/logger/logger.ts
13
13
  import { config } from "@restura/internal";
14
14
  import pino from "pino";
15
+ import pinoPretty from "pino-pretty";
15
16
 
16
17
  // src/logger/loggerConfigSchema.ts
17
18
  import { z } from "zod";
18
19
  var loggerConfigSchema = z.object({
19
20
  level: z.enum(["fatal", "error", "warn", "info", "debug", "silly", "trace"]).default("info"),
20
- transports: z.array(
21
- z.object({
22
- target: z.string(),
23
- level: z.string().optional(),
24
- options: z.record(z.string(), z.unknown()).optional()
25
- })
26
- ).optional()
21
+ transports: z.array(z.custom()).optional(),
22
+ serializers: z.object({
23
+ err: z.custom().optional()
24
+ }).optional(),
25
+ stream: z.custom().optional()
26
+ }).refine((data) => !(data.transports && data.stream), {
27
+ message: "You must provide either a transports array or a stream object, but not both",
28
+ path: ["transports"]
27
29
  });
28
30
 
29
31
  // src/logger/logger.ts
@@ -38,29 +40,54 @@ var logLevelMap = {
38
40
  trace: "trace"
39
41
  };
40
42
  var currentLogLevel = logLevelMap[loggerConfig.level];
41
- var defaultTransports = [
43
+ var defaultStream = pinoPretty({
44
+ colorize: true,
45
+ translateTime: "yyyy-mm-dd HH:MM:ss.l",
46
+ ignore: "pid,hostname,_meta",
47
+ // _meta allows a user to pass in metadata for JSON but not print it to the console
48
+ messageFormat: "{msg}",
49
+ levelFirst: true,
50
+ customColors: "error:red,warn:yellow,info:green,debug:blue,trace:magenta",
51
+ destination: process.stdout
52
+ });
53
+ function isAxiosError(error) {
54
+ const isObject = (error2) => error2 !== null && typeof error2 === "object";
55
+ return isObject(error) && "isAxiosError" in error && error.isAxiosError === true;
56
+ }
57
+ var baseSerializer = pino.stdSerializers.err;
58
+ var defaultSerializer = (error) => {
59
+ if (isAxiosError(error)) {
60
+ const err = error;
61
+ return {
62
+ type: "AxiosError",
63
+ message: err.message,
64
+ stack: err.stack,
65
+ url: err.config?.url,
66
+ method: err.config?.method?.toUpperCase(),
67
+ status: err.response?.status,
68
+ responseData: err.response?.data
69
+ };
70
+ }
71
+ return baseSerializer(error);
72
+ };
73
+ var errorSerializer = (() => {
74
+ try {
75
+ return loggerConfig.serializers?.err ? loggerConfig.serializers.err(baseSerializer) : defaultSerializer;
76
+ } catch (error) {
77
+ console.error("Failed to initialize custom error serializer, falling back to default", error);
78
+ return defaultSerializer;
79
+ }
80
+ })();
81
+ var pinoLogger = pino(
42
82
  {
43
- target: "pino-pretty",
44
- options: {
45
- colorize: true,
46
- translateTime: "yyyy-mm-dd HH:MM:ss.l",
47
- ignore: "pid,hostname,_meta",
48
- // _meta allows a user to pass in metadata for JSON but not print it to the console
49
- messageFormat: "{msg}",
50
- levelFirst: true,
51
- customColors: "error:red,warn:yellow,info:green,debug:blue,trace:magenta"
52
- }
53
- }
54
- ];
55
- var pinoLogger = pino({
56
- level: currentLogLevel,
57
- transport: {
58
- targets: loggerConfig.transports ?? defaultTransports
83
+ level: currentLogLevel,
84
+ ...loggerConfig.transports ? { transport: { targets: loggerConfig.transports } } : {},
85
+ serializers: {
86
+ err: errorSerializer
87
+ }
59
88
  },
60
- serializers: {
61
- err: pino.stdSerializers.err
62
- }
63
- });
89
+ loggerConfig.stream ? loggerConfig.stream : defaultStream
90
+ );
64
91
  function buildContext(args) {
65
92
  const ctx = {};
66
93
  const prims = [];