@decohere/scribe 0.0.1-dev.126 → 0.0.1-dev.14

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.
Files changed (40) hide show
  1. package/commonjs/src/console.adapter.js +14 -21
  2. package/commonjs/src/env.js +3 -2
  3. package/commonjs/src/errors.js +2 -0
  4. package/commonjs/src/index.js +2 -3
  5. package/commonjs/src/logger.adapter.js +6 -58
  6. package/commonjs/src/logger.factory.js +11 -112
  7. package/commonjs/src/logger.type.js +2 -0
  8. package/commonjs/src/testing/index.js +2 -14
  9. package/commonjs/src/types.js +6 -19
  10. package/es6/src/console.adapter.js +15 -22
  11. package/es6/src/env.js +4 -3
  12. package/es6/src/errors.js +2 -0
  13. package/es6/src/index.js +2 -3
  14. package/es6/src/logger.adapter.js +3 -45
  15. package/es6/src/logger.factory.js +11 -112
  16. package/es6/src/logger.type.js +2 -0
  17. package/es6/src/testing/index.js +3 -1
  18. package/es6/src/types.js +6 -16
  19. package/package.json +1 -15
  20. package/types/src/console.adapter.d.ts +0 -1
  21. package/types/src/env.d.ts +1 -1
  22. package/types/src/index.d.ts +0 -3
  23. package/types/src/logger.adapter.d.ts +1 -32
  24. package/types/src/logger.factory.d.ts +2 -31
  25. package/types/src/logger.type.d.ts +0 -5
  26. package/types/src/testing/index.d.ts +1 -1
  27. package/types/src/types.d.ts +0 -7
  28. package/LICENSE +0 -15
  29. package/commonjs/src/log-entry.adapter.js +0 -55
  30. package/commonjs/src/log-entry.js +0 -89
  31. package/commonjs/src/redact.js +0 -67
  32. package/commonjs/src/testing/capture.adapter.js +0 -23
  33. package/es6/src/log-entry.adapter.js +0 -49
  34. package/es6/src/log-entry.js +0 -79
  35. package/es6/src/redact.js +0 -57
  36. package/es6/src/testing/capture.adapter.js +0 -17
  37. package/types/src/log-entry.adapter.d.ts +0 -20
  38. package/types/src/log-entry.d.ts +0 -38
  39. package/types/src/redact.d.ts +0 -10
  40. package/types/src/testing/capture.adapter.d.ts +0 -14
@@ -1,67 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", {
3
- value: true
4
- });
5
- Object.defineProperty(exports, "redact", {
6
- enumerable: true,
7
- get: function() {
8
- return redact;
9
- }
10
- });
11
- const _types = require("./types.js");
12
- function keysFromPaths(paths) {
13
- const keys = new Set();
14
- for (const path of paths){
15
- const segments = path.split(".").filter((segment)=>segment.length > 0 && segment !== "*");
16
- const key = segments.at(-1);
17
- if (key != null) {
18
- keys.add(key);
19
- }
20
- }
21
- return keys;
22
- }
23
- function isPlainObject(value) {
24
- if (value == null || typeof value !== "object" || Array.isArray(value) || value instanceof Error) {
25
- return false;
26
- }
27
- const proto = Object.getPrototypeOf(value);
28
- return proto === Object.prototype || proto === null;
29
- }
30
- function object(value, keys, censor = _types.DefaultCensor, copies = new WeakMap()) {
31
- if (value == null || typeof value !== "object") {
32
- return value;
33
- }
34
- if (value instanceof Error) {
35
- return value;
36
- }
37
- // A cycle in the input stays a cycle in the copy.
38
- const copied = copies.get(value);
39
- if (copied !== undefined) {
40
- return copied;
41
- }
42
- if (Array.isArray(value)) {
43
- const out = [];
44
- copies.set(value, out);
45
- for (const item of value){
46
- out.push(object(item, keys, censor, copies));
47
- }
48
- return out;
49
- }
50
- if (!isPlainObject(value)) {
51
- return value;
52
- }
53
- const out = {};
54
- copies.set(value, out);
55
- for (const [key, nested] of Object.entries(value)){
56
- out[key] = keys.has(key) ? censor : object(nested, keys, censor, copies);
57
- }
58
- return out;
59
- }
60
- function fromOptions(options) {
61
- return keysFromPaths(options?.paths ?? _types.DefaultRedactions.paths);
62
- }
63
- const redact = {
64
- object,
65
- keysFromPaths,
66
- fromOptions
67
- };
@@ -1,23 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", {
3
- value: true
4
- });
5
- Object.defineProperty(exports, "CaptureAdapter", {
6
- enumerable: true,
7
- get: function() {
8
- return CaptureAdapter;
9
- }
10
- });
11
- const _logentryadapter = require("../log-entry.adapter.js");
12
- class CaptureAdapter extends _logentryadapter.LogEntryAdapter {
13
- entries = [];
14
- /** Entries at `level`. */ at(level) {
15
- return this.entries.filter((entry)=>entry.level === level);
16
- }
17
- clear() {
18
- this.entries.length = 0;
19
- }
20
- write(entry) {
21
- this.entries.push(entry);
22
- }
23
- }
@@ -1,49 +0,0 @@
1
- import { logEntry } from "./log-entry.js";
2
- import { LogAdapter } from "./logger.adapter.js";
3
- import { redact } from "./redact.js";
4
- import { DefaultLogLevel, LogLevels } from "./types.js";
5
- /**
6
- * Base for adapters that write structured entries. It owns levels, the level
7
- * threshold, `child`, and censoring the `redactions` keys; a subclass only
8
- * writes each `LogEntry`.
9
- */ export class LogEntryAdapter extends LogAdapter {
10
- levels = LogLevels;
11
- level = DefaultLogLevel;
12
- threshold = LogLevels.info;
13
- redactionKeys = redact.fromOptions();
14
- configure(scribeOptions) {
15
- this.levels = this.getLogLevels(scribeOptions);
16
- this.level = scribeOptions.logLevel ?? DefaultLogLevel;
17
- this.threshold = this.levels[this.level] ?? LogLevels.info;
18
- this.redactionKeys = redact.fromOptions(scribeOptions.redactions);
19
- }
20
- getLogger(name) {
21
- return this.createLogger(name || "default", {});
22
- }
23
- createLogger(name, bindings) {
24
- const { levels, level: currentLevel, threshold, redactionKeys } = this;
25
- const baseLogger = {
26
- get currentLevel () {
27
- return currentLevel;
28
- },
29
- get logLevels () {
30
- return levels;
31
- }
32
- };
33
- const noop = ()=>{};
34
- const methods = {};
35
- for (const [level, value] of Object.entries(levels)){
36
- methods[level] = value >= threshold ? (...args)=>this.write(logEntry.from(level, name, args, {
37
- bindings,
38
- redactionKeys
39
- })) : noop;
40
- }
41
- return Object.assign(baseLogger, methods, {
42
- isLevelEnabled: (level)=>(levels[level] ?? 0) >= threshold,
43
- child: (fields)=>this.createLogger(name, {
44
- ...bindings,
45
- ...fields
46
- })
47
- });
48
- }
49
- }
@@ -1,79 +0,0 @@
1
- import { serializeError } from "./logger.adapter.js";
2
- import { redact } from "./redact.js";
3
- function isFields(value) {
4
- return value != null && typeof value === "object" && !Array.isArray(value);
5
- }
6
- /** Serialize errors passed as fields (`{ error }`), so they keep their message and stack. */ function serializeErrors(fields) {
7
- const out = {};
8
- for (const [key, value] of Object.entries(fields)){
9
- out[key] = value instanceof Error ? serializeError(value) : value;
10
- }
11
- return out;
12
- }
13
- /**
14
- * Builds an entry from a log call's arguments:
15
- *
16
- * - `log.info("text")`, `log.info(42)` → `msg`
17
- * - `log.error(err)` → `msg` is `err.message`, `err` the serialized error
18
- * - `log.info({ fields })`, `log.info({ fields }, "text")` → `fields` (a string
19
- * `msg` field becomes the message)
20
- * - `log.info("text", { fields })` → `msg` + `fields`
21
- * - `log.error("text", err)` → `msg` + `err`
22
- * - `log.info("text", value)` → `msg` + `fields.data`; any further arguments go
23
- * in `fields.args`
24
- */ function from(level, name, [first, ...rest], { bindings, redactionKeys }) {
25
- let msg;
26
- let err;
27
- let fields = {};
28
- if (first instanceof Error) {
29
- err = first;
30
- msg = first.message;
31
- } else if (isFields(first)) {
32
- const { msg: fieldMsg, ...others } = first;
33
- fields = others;
34
- if (typeof fieldMsg === "string") {
35
- msg = fieldMsg;
36
- } else if (fieldMsg !== undefined) {
37
- fields.msg = fieldMsg;
38
- }
39
- if (typeof rest[0] === "string") {
40
- msg = rest.shift();
41
- }
42
- } else if (first !== undefined) {
43
- msg = String(first);
44
- }
45
- const [second, ...extra] = rest;
46
- if (second instanceof Error && err == null) {
47
- err = second;
48
- } else if (isFields(second)) {
49
- fields = {
50
- ...fields,
51
- ...second
52
- };
53
- } else if (second !== undefined) {
54
- fields.data = second;
55
- }
56
- if (extra.length > 0) {
57
- fields.args = extra;
58
- }
59
- const allFields = serializeErrors({
60
- ...bindings,
61
- ...fields
62
- });
63
- const entry = {
64
- level,
65
- name,
66
- fields: redactionKeys == null ? allFields : redact.object(allFields, redactionKeys)
67
- };
68
- if (msg !== undefined) {
69
- entry.msg = msg;
70
- }
71
- if (err != null) {
72
- const serialized = serializeError(err);
73
- entry.err = redactionKeys == null ? serialized : redact.object(serialized, redactionKeys);
74
- }
75
- return entry;
76
- }
77
- export const logEntry = {
78
- from
79
- };
package/es6/src/redact.js DELETED
@@ -1,57 +0,0 @@
1
- import { DefaultCensor, DefaultRedactions } from "./types.js";
2
- function keysFromPaths(paths) {
3
- const keys = new Set();
4
- for (const path of paths){
5
- const segments = path.split(".").filter((segment)=>segment.length > 0 && segment !== "*");
6
- const key = segments.at(-1);
7
- if (key != null) {
8
- keys.add(key);
9
- }
10
- }
11
- return keys;
12
- }
13
- function isPlainObject(value) {
14
- if (value == null || typeof value !== "object" || Array.isArray(value) || value instanceof Error) {
15
- return false;
16
- }
17
- const proto = Object.getPrototypeOf(value);
18
- return proto === Object.prototype || proto === null;
19
- }
20
- function object(value, keys, censor = DefaultCensor, copies = new WeakMap()) {
21
- if (value == null || typeof value !== "object") {
22
- return value;
23
- }
24
- if (value instanceof Error) {
25
- return value;
26
- }
27
- // A cycle in the input stays a cycle in the copy.
28
- const copied = copies.get(value);
29
- if (copied !== undefined) {
30
- return copied;
31
- }
32
- if (Array.isArray(value)) {
33
- const out = [];
34
- copies.set(value, out);
35
- for (const item of value){
36
- out.push(object(item, keys, censor, copies));
37
- }
38
- return out;
39
- }
40
- if (!isPlainObject(value)) {
41
- return value;
42
- }
43
- const out = {};
44
- copies.set(value, out);
45
- for (const [key, nested] of Object.entries(value)){
46
- out[key] = keys.has(key) ? censor : object(nested, keys, censor, copies);
47
- }
48
- return out;
49
- }
50
- function fromOptions(options) {
51
- return keysFromPaths(options?.paths ?? DefaultRedactions.paths);
52
- }
53
- export const redact = {
54
- object,
55
- keysFromPaths,
56
- fromOptions
57
- };
@@ -1,17 +0,0 @@
1
- import { LogEntryAdapter } from "../log-entry.adapter.js";
2
- /**
3
- * Test double: keeps every entry that passes the level threshold, prints
4
- * nothing. `scribe.configure({ adapter: capture })`, run the code, then assert
5
- * on `capture.entries`.
6
- */ export class CaptureAdapter extends LogEntryAdapter {
7
- entries = [];
8
- /** Entries at `level`. */ at(level) {
9
- return this.entries.filter((entry)=>entry.level === level);
10
- }
11
- clear() {
12
- this.entries.length = 0;
13
- }
14
- write(entry) {
15
- this.entries.push(entry);
16
- }
17
- }
@@ -1,20 +0,0 @@
1
- import { type LogEntry } from "./log-entry.js";
2
- import { LogAdapter } from "./logger.adapter.js";
3
- import type { Logger } from "./logger.type.js";
4
- import { type ScribeOptions } from "./types.js";
5
- /**
6
- * Base for adapters that write structured entries. It owns levels, the level
7
- * threshold, `child`, and censoring the `redactions` keys; a subclass only
8
- * writes each `LogEntry`.
9
- */
10
- export declare abstract class LogEntryAdapter extends LogAdapter {
11
- protected levels: Record<string, number>;
12
- private level;
13
- private threshold;
14
- private redactionKeys;
15
- configure<T extends string = never, CustomOnly extends true | false = false>(scribeOptions: ScribeOptions<T, CustomOnly>): void;
16
- getLogger<T extends string = never, CustomOnly extends true | false = false>(name?: string): Logger<T, CustomOnly>;
17
- /** Write one entry that passed the level threshold. */
18
- protected abstract write(entry: LogEntry): void;
19
- private createLogger;
20
- }
@@ -1,38 +0,0 @@
1
- import { type SerializedError } from "./logger.adapter.js";
2
- /**
3
- * One logged call. Adapters that write structured lines build it with
4
- * `logEntry.from`, and taps receive it.
5
- */
6
- export type LogEntry = {
7
- level: string;
8
- name: string;
9
- msg?: string;
10
- err?: SerializedError;
11
- /** Bound fields (`child`), then the call's own fields. */
12
- fields: Record<string, unknown>;
13
- };
14
- export type LogEntryOptions = {
15
- bindings?: Record<string, unknown>;
16
- /**
17
- * Keys the adapter censors (`redact.fromOptions`). Redaction is the
18
- * adapter's job, so the facade's tap passes none.
19
- */
20
- redactionKeys?: ReadonlySet<string>;
21
- };
22
- /**
23
- * Builds an entry from a log call's arguments:
24
- *
25
- * - `log.info("text")`, `log.info(42)` → `msg`
26
- * - `log.error(err)` → `msg` is `err.message`, `err` the serialized error
27
- * - `log.info({ fields })`, `log.info({ fields }, "text")` → `fields` (a string
28
- * `msg` field becomes the message)
29
- * - `log.info("text", { fields })` → `msg` + `fields`
30
- * - `log.error("text", err)` → `msg` + `err`
31
- * - `log.info("text", value)` → `msg` + `fields.data`; any further arguments go
32
- * in `fields.args`
33
- */
34
- declare function from(level: string, name: string, [first, ...rest]: readonly unknown[], { bindings, redactionKeys }: LogEntryOptions): LogEntry;
35
- export declare const logEntry: {
36
- from: typeof from;
37
- };
38
- export {};
@@ -1,10 +0,0 @@
1
- import { type RedactionOptions } from "./types.js";
2
- declare function keysFromPaths(paths: string[]): Set<string>;
3
- declare function object(value: unknown, keys: ReadonlySet<string>, censor?: string, copies?: WeakMap<object, unknown>): unknown;
4
- declare function fromOptions(options?: RedactionOptions): Set<string>;
5
- export declare const redact: {
6
- object: typeof object;
7
- keysFromPaths: typeof keysFromPaths;
8
- fromOptions: typeof fromOptions;
9
- };
10
- export {};
@@ -1,14 +0,0 @@
1
- import { LogEntryAdapter } from "../log-entry.adapter.js";
2
- import type { LogEntry } from "../log-entry.js";
3
- /**
4
- * Test double: keeps every entry that passes the level threshold, prints
5
- * nothing. `scribe.configure({ adapter: capture })`, run the code, then assert
6
- * on `capture.entries`.
7
- */
8
- export declare class CaptureAdapter extends LogEntryAdapter {
9
- readonly entries: LogEntry[];
10
- /** Entries at `level`. */
11
- at(level: string): LogEntry[];
12
- clear(): void;
13
- protected write(entry: LogEntry): void;
14
- }