logosaur 0.2.0 → 0.2.1

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/elysia.d.ts CHANGED
@@ -1,4 +1,24 @@
1
+ /**
2
+ * Request logging for Elysia: one line per request, with its method, path,
3
+ * status and time taken; info, warn for a 4xx, error for a 5xx.
4
+ *
5
+ * @example
6
+ * ```ts
7
+ * import { Elysia } from "elysia";
8
+ * import { logosaur } from "@0c71/logosaur/elysia";
9
+ *
10
+ * const app = new Elysia().use(logosaur());
11
+ * // 14:02:03 INF request method=GET path=/eggs status=200 ms=1.2
12
+ * ```
13
+ *
14
+ * @module
15
+ */
1
16
  import { Elysia } from "elysia";
2
17
  import { type Logger } from "./index.ts";
18
+ /**
19
+ * Elysia plugin: one line per request, with its method, path, status and
20
+ * time taken (`app.use(logosaur())`); pass a logger to set its level, format
21
+ * or fields, else it uses the ready-made `log`
22
+ */
3
23
  declare const logosaur: (log?: Logger) => Elysia;
4
24
  export { logosaur };
package/dist/hono.d.ts CHANGED
@@ -1,4 +1,24 @@
1
+ /**
2
+ * Request logging for Hono: one line per request, with its method, path,
3
+ * status and time taken; info, warn for a 4xx, error for a 5xx.
4
+ *
5
+ * @example
6
+ * ```ts
7
+ * import { Hono } from "hono";
8
+ * import { logosaur } from "@0c71/logosaur/hono";
9
+ *
10
+ * const app = new Hono().use(logosaur());
11
+ * // 14:02:03 INF request method=GET path=/eggs status=200 ms=1.2
12
+ * ```
13
+ *
14
+ * @module
15
+ */
1
16
  import type { MiddlewareHandler } from "hono";
2
17
  import { type Logger } from "./index.ts";
18
+ /**
19
+ * Hono middleware: one line per request, with its method, path, status and
20
+ * time taken (`app.use(logosaur())`); pass a logger to set its level, format
21
+ * or fields, else it uses the ready-made `log`
22
+ */
3
23
  declare const logosaur: (log?: Logger) => MiddlewareHandler;
4
24
  export { logosaur };
package/dist/index.d.ts CHANGED
@@ -1,64 +1,83 @@
1
+ /** how each line is written: `"text"`, `"json"` or `"logfmt"` */
1
2
  type Format = "text" | "json" | "logfmt";
3
+ /** anything with a write method, like `process.stderr` */
2
4
  interface Writer {
5
+ /** takes one line, with its newline */
3
6
  write(chunk: string): unknown;
7
+ /** a terminal: colors turn on by themselves */
4
8
  isTTY?: boolean;
5
9
  }
10
+ /** reshapes a field before it is logged, at any depth */
6
11
  type Replacer = (key: string, value: unknown) => unknown;
12
+ /** a level, quietest first */
13
+ type Level = "debug" | "info" | "warn" | "error" | "fatal";
14
+ /** how a logger from {@link createLogger} writes its lines */
7
15
  interface Options {
16
+ /** the quietest level that gets through (default `"info"`) */
8
17
  level?: Level;
18
+ /** shown before every message, as `prefix:` */
9
19
  prefix?: string;
20
+ /** added to every line */
10
21
  fields?: object;
22
+ /** show the time on text lines (default `true`) */
11
23
  time?: boolean;
24
+ /** an IANA time zone for the clock, like `"America/New_York"` (default: local) */
12
25
  timeZone?: string;
26
+ /** `"text"` (default), `"json"` or `"logfmt"` */
13
27
  format?: Format;
28
+ /** force colors on or off (default: on for a terminal, off with `NO_COLOR`) */
14
29
  color?: boolean;
30
+ /** where lines go (default `process.stderr`) */
15
31
  output?: Writer;
32
+ /** also append every line, without colors, to this file or to each day's file */
16
33
  file?: string | ((day: string) => string);
34
+ /** reshapes fields before they are logged */
17
35
  replacer?: Replacer;
36
+ /** the clock, handy for tests */
18
37
  now?: () => Date;
19
38
  }
39
+ /** logs a message at one level, with optional fields */
20
40
  type Log = (msg: string, fields?: object) => void;
41
+ /** a logger: a method per level, each taking a message and optional fields */
21
42
  interface Logger {
43
+ /** `DBG`: hidden unless the level is `"debug"` */
22
44
  debug: Log;
45
+ /** `INF`: the default level */
23
46
  info: Log;
47
+ /** `WRN` */
24
48
  warn: Log;
49
+ /** `ERR` */
25
50
  error: Log;
51
+ /** `FTL`: logs, then exits with code 1 */
26
52
  fatal: (msg: string, fields?: object) => never;
53
+ /** a new logger that adds these fields to every line */
27
54
  with: (fields: object) => Logger;
55
+ /** a new logger with this prefix */
28
56
  withPrefix: (prefix: string) => Logger;
57
+ /** changes the quietest level that gets through */
29
58
  setLevel: (level: Level) => void;
59
+ /** the quietest level that gets through */
30
60
  readonly level: Level;
31
61
  }
32
- declare const levels: {
33
- readonly debug: {
34
- readonly rank: 0;
35
- readonly label: "DBG";
36
- readonly color: 63;
37
- };
38
- readonly info: {
39
- readonly rank: 1;
40
- readonly label: "INF";
41
- readonly color: 86;
42
- };
43
- readonly warn: {
44
- readonly rank: 2;
45
- readonly label: "WRN";
46
- readonly color: 192;
47
- };
48
- readonly error: {
49
- readonly rank: 3;
50
- readonly label: "ERR";
51
- readonly color: 204;
52
- };
53
- readonly fatal: {
54
- readonly rank: 4;
55
- readonly label: "FTL";
56
- readonly color: 134;
57
- };
58
- };
59
- type Level = keyof typeof levels;
62
+ /** a value as text: quoted when it is empty or has spaces, `=` or quotes */
60
63
  declare const formatValue: (value: unknown) => string;
64
+ /**
65
+ * the fields as `[key, value]` pairs: nested objects flattened (`a.b=1`) and
66
+ * empty values left out
67
+ */
61
68
  declare const pairs: (fields: object, replacer?: Replacer) => [string, unknown][];
69
+ /**
70
+ * a new logger
71
+ *
72
+ * @example
73
+ * ```ts
74
+ * const log = createLogger({ level: "debug", timeZone: "America/New_York" });
75
+ *
76
+ * log.info("🦖 hatched", { name: "rex" });
77
+ * // 14:02:03 INF 🦖 hatched name=rex
78
+ * ```
79
+ */
62
80
  declare const createLogger: (options?: Options) => Logger;
81
+ /** a ready-made logger: info and up, to stderr */
63
82
  declare const log: Logger;
64
- export { createLogger, log, formatValue, pairs, type Format, type Level, type Logger, type Options, type Replacer, type Writer, };
83
+ export { createLogger, log, formatValue, pairs, type Format, type Level, type Log, type Logger, type Options, type Replacer, type Writer, };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "logosaur",
3
- "version": "0.2.0",
3
+ "version": "0.2.1",
4
4
  "description": "A tiny, colorful, zero-dependency structured logger for Bun and TypeScript. Rawr.",
5
5
  "keywords": [
6
6
  "log",