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

RobinTail / express-zod-api / 15980543093

30 Jun 2025 06:17PM UTC coverage: 100.0%. Remained the same
15980543093

Pull #2778

github

web-flow
Merge fc817f046 into 22f3987ec
Pull Request #2778: chore(deps): update typescript-eslint and its rule tester (used by migration) to v8.35.1

1230 of 1273 branches covered (96.62%)

3994 of 3994 relevant lines covered (100.0%)

361.86 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";
6✔
2
import { inspect } from "node:util";
6✔
3
import { performance } from "node:perf_hooks";
6✔
4
import { FlatObject, isProduction } from "./common-helpers";
6✔
5
import {
6✔
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 {
6✔
48
  protected readonly config: BuiltinLoggerConfig;
218✔
49

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

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

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

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

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

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

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

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

108
  /**
109
   * @desc The argument used for instance created by .child() method
110
   * @see ChildLoggerProvider
111
   * */
112
  public get ctx() {
218✔
113
    return this.config.ctx;
24✔
114
  }
24✔
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) {
218✔
120
    const start = performance.now();
96✔
121
    return () => {
96✔
122
      const duration = performance.now() - start;
96✔
123
      const {
96✔
124
        message,
96✔
125
        severity = "debug",
96✔
126
        formatter = formatDuration,
96✔
127
      } = typeof subject === "object" ? subject : { message: subject };
96✔
128
      this.print(
96✔
129
        typeof severity === "function" ? severity(duration) : severity,
96✔
130
        message,
96✔
131
        formatter(duration),
96✔
132
      );
96✔
133
    };
96✔
134
  }
96✔
135
}
218✔
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