• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

RobinTail / express-zod-api / 14625934837

23 Apr 2025 06:57PM UTC coverage: 99.975%. Remained the same
14625934837

Pull #2573

github

web-flow
Merge 287b126f3 into 83dfadd5f
Pull Request #2573: Improve handling of `ZodError` by `ensureError`

1203 of 1240 branches covered (97.02%)

1 of 1 new or added line in 1 file covered. (100.0%)

4063 of 4064 relevant lines covered (99.98%)

258.26 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

100.0
/express-zod-api/src/builtin-logger.ts
1
import ansis from "ansis";
4✔
2
import { inspect } from "node:util";
4✔
3
import { performance } from "node:perf_hooks";
4✔
4
import { FlatObject, isProduction } from "./common-helpers";
4✔
5
import {
4✔
6
  AbstractLogger,
7
  formatDuration,
8
  isHidden,
9
  Severity,
10
  styles,
11
} from "./logger-helpers";
12

13
interface Context extends FlatObject {
14
  requestId?: string;
15
}
16

17
export interface BuiltinLoggerConfig {
18
  /**
19
   * @desc The minimal severity to log or "silent" to disable logging
20
   * @example "debug" also enables pretty output for inspected entities
21
   * */
22
  level: "silent" | "warn" | "info" | "debug";
23
  /** @desc Enables colors on printed severity and inspected entities */
24
  color: boolean;
25
  /**
26
   * @desc Control how deeply entities should be inspected
27
   * @example null
28
   * @example Infinity
29
   * */
30
  depth: number | null;
31
  /**
32
   * @desc Context: the metadata applicable for each logged entry, used by .child() method
33
   * @see childLoggerProvider
34
   * */
35
  ctx: Context;
36
}
37

38
interface ProfilerOptions {
39
  message: string;
40
  /** @default "debug" */
41
  severity?: Severity | ((ms: number) => Severity);
42
  /** @default formatDuration - adaptive units and limited fraction */
43
  formatter?: (ms: number) => string | number;
44
}
45

46
/** @desc Built-in console logger with optional colorful inspections */
47
export class BuiltinLogger implements AbstractLogger {
4✔
48
  protected readonly config: BuiltinLoggerConfig;
212✔
49

50
  /** @example new BuiltinLogger({ level: "debug", color: true, depth: 4 }) */
51
  public constructor({
212✔
52
    color = ansis.isSupported(),
212✔
53
    level = isProduction() ? "warn" : "debug",
212✔
54
    depth = 2,
212✔
55
    ctx = {},
212✔
56
  }: Partial<BuiltinLoggerConfig> = {}) {
212✔
57
    this.config = { color, level, depth, ctx };
212✔
58
  }
212✔
59

60
  protected format(subject: unknown) {
212✔
61
    const { depth, color: colors, level } = this.config;
144✔
62
    return inspect(subject, {
144✔
63
      depth,
144✔
64
      colors,
144✔
65
      breakLength: level === "debug" ? 80 : Infinity,
144✔
66
      compact: level === "debug" ? 3 : true,
144✔
67
    });
144✔
68
  }
144✔
69

70
  protected print(method: Severity, message: string, meta?: unknown) {
212✔
71
    const {
392✔
72
      level,
392✔
73
      ctx: { requestId, ...ctx },
392✔
74
      color: hasColor,
392✔
75
    } = this.config;
392✔
76
    if (level === "silent" || isHidden(method, level)) return;
392✔
77
    const output: string[] = [new Date().toISOString()];
140✔
78
    if (requestId) output.push(hasColor ? styles.ctx(requestId) : requestId);
148✔
79
    output.push(
140✔
80
      hasColor ? `${styles[method](method)}:` : `${method}:`,
392✔
81
      message,
392✔
82
    );
392✔
83
    if (meta !== undefined) output.push(this.format(meta));
392✔
84
    if (Object.keys(ctx).length > 0) output.push(this.format(ctx));
148✔
85
    console.log(output.join(" "));
140✔
86
  }
392✔
87

88
  public debug(message: string, meta?: unknown) {
212✔
89
    this.print("debug", message, meta);
172✔
90
  }
172✔
91

92
  public info(message: string, meta?: unknown) {
212✔
93
    this.print("info", message, meta);
64✔
94
  }
64✔
95

96
  public warn(message: string, meta?: unknown) {
212✔
97
    this.print("warn", message, meta);
16✔
98
  }
16✔
99

100
  public error(message: string, meta?: unknown) {
212✔
101
    this.print("error", message, meta);
76✔
102
  }
76✔
103

104
  public child(ctx: Context) {
212✔
105
    return new BuiltinLogger({ ...this.config, ctx });
16✔
106
  }
16✔
107

108
  /**
109
   * @desc The argument used for instance created by .child() method
110
   * @see ChildLoggerProvider
111
   * */
112
  public get ctx() {
212✔
113
    return this.config.ctx;
16✔
114
  }
16✔
115

116
  /** @desc Measures the duration until you invoke the returned callback */
117
  public profile(message: string): () => void;
118
  public profile(options: ProfilerOptions): () => void;
119
  public profile(subject: string | ProfilerOptions) {
212✔
120
    const start = performance.now();
64✔
121
    return () => {
64✔
122
      const duration = performance.now() - start;
64✔
123
      const {
64✔
124
        message,
64✔
125
        severity = "debug",
64✔
126
        formatter = formatDuration,
64✔
127
      } = typeof subject === "object" ? subject : { message: subject };
64✔
128
      this.print(
64✔
129
        typeof severity === "function" ? severity(duration) : severity,
64✔
130
        message,
64✔
131
        formatter(duration),
64✔
132
      );
64✔
133
    };
64✔
134
  }
64✔
135
}
212✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc