@vlandoss/loggy 0.0.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/README.md ADDED
@@ -0,0 +1,9 @@
1
+ # Loggy
2
+
3
+ Console wrapper to make logging fun again
4
+
5
+ ## Installation
6
+
7
+ ```sh
8
+ pnpm add @vlandoss/loggy
9
+ ```
package/dist/index.cjs ADDED
@@ -0,0 +1,153 @@
1
+ "use strict";
2
+ var __create = Object.create;
3
+ var __defProp = Object.defineProperty;
4
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
+ var __getOwnPropNames = Object.getOwnPropertyNames;
6
+ var __getProtoOf = Object.getPrototypeOf;
7
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
8
+ var __export = (target, all) => {
9
+ for (var name in all)
10
+ __defProp(target, name, { get: all[name], enumerable: true });
11
+ };
12
+ var __copyProps = (to, from, except, desc) => {
13
+ if (from && typeof from === "object" || typeof from === "function") {
14
+ for (let key of __getOwnPropNames(from))
15
+ if (!__hasOwnProp.call(to, key) && key !== except)
16
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
17
+ }
18
+ return to;
19
+ };
20
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
21
+ // If the importer is in node compatibility mode or this is not an ESM
22
+ // file that has been converted to a CommonJS file using a Babel-
23
+ // compatible transform (i.e. "__esModule" has not been set), then set
24
+ // "default" to the CommonJS "module.exports" for node compatibility.
25
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
26
+ mod
27
+ ));
28
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
29
+
30
+ // src/index.ts
31
+ var index_exports = {};
32
+ __export(index_exports, {
33
+ Loggy: () => Loggy,
34
+ createLoggy: () => createLoggy
35
+ });
36
+ module.exports = __toCommonJS(index_exports);
37
+
38
+ // src/loggy.ts
39
+ var import_node_util2 = __toESM(require("util"), 1);
40
+ var import_consola = require("consola");
41
+ var import_debug = __toESM(require("debug"), 1);
42
+
43
+ // src/const.ts
44
+ var import_node_util = __toESM(require("util"), 1);
45
+ var DEFAULT_FORMAT_OPTIONS = {
46
+ colors: true,
47
+ depth: 3,
48
+ maxArrayLength: 50,
49
+ breakLength: 80
50
+ };
51
+ var DEFAULT_FORMATTERS = {
52
+ o: (arg) => {
53
+ return import_node_util.default.inspect(arg, DEFAULT_FORMAT_OPTIONS).split("\n").map((str) => str.trim()).join(" ");
54
+ },
55
+ O: (arg) => {
56
+ return import_node_util.default.inspect(arg, DEFAULT_FORMAT_OPTIONS);
57
+ }
58
+ };
59
+
60
+ // src/loggy.ts
61
+ var Loggy = class _Loggy {
62
+ #options;
63
+ #debug;
64
+ #consola;
65
+ constructor(options) {
66
+ this.#options = options;
67
+ this.#debug = (0, import_debug.default)(`${options.namespace}:root`);
68
+ this.#consola = (0, import_consola.createConsola)({
69
+ formatOptions: options.formatOptions
70
+ });
71
+ }
72
+ get namespace() {
73
+ return this.#options.namespace;
74
+ }
75
+ debug(...args) {
76
+ this.#debug(...args);
77
+ }
78
+ error(messageOrError, ...args) {
79
+ this.#consola.error(messageOrError, ...args);
80
+ }
81
+ info(...args) {
82
+ this.#consola.info(this.#format(...args));
83
+ }
84
+ trace(...args) {
85
+ this.#consola.trace(this.#format(...args));
86
+ }
87
+ warn(...args) {
88
+ this.#consola.warn(this.#format(...args));
89
+ }
90
+ child(namespace) {
91
+ return new _Loggy({
92
+ ...this.#options,
93
+ namespace: `${this.#options.namespace}:${namespace}`
94
+ });
95
+ }
96
+ start(...args) {
97
+ this.#consola.start(this.#format(...args));
98
+ }
99
+ success(...args) {
100
+ this.#consola.success(this.#format(...args));
101
+ }
102
+ subdebug(namespace) {
103
+ return (0, import_debug.default)(`${this.#options.namespace}:${namespace}`);
104
+ }
105
+ #format(...args) {
106
+ const formattedArgs = [...args];
107
+ if (typeof formattedArgs[0] !== "string") {
108
+ formattedArgs.unshift("%O");
109
+ }
110
+ const [message, ...replacements] = formattedArgs;
111
+ let replacementIndex = -1;
112
+ const formattedMessage = message.replace(
113
+ /%([a-zA-Z%])/g,
114
+ // matches %o, %O, %%, etc.
115
+ (match, formatKey) => {
116
+ if (formatKey === "%") {
117
+ return "%";
118
+ }
119
+ replacementIndex++;
120
+ const formatter = this.#options.formatters[formatKey];
121
+ if (typeof formatter !== "function") {
122
+ return match;
123
+ }
124
+ const replacement = replacements[replacementIndex];
125
+ const formattedMatch = formatter(replacement);
126
+ replacements.splice(replacementIndex, 1);
127
+ replacementIndex--;
128
+ return formattedMatch;
129
+ }
130
+ ).split("\n").join(`
131
+ ${" ".repeat(2)}`);
132
+ return import_node_util2.default.formatWithOptions(this.#options.formatOptions, formattedMessage, ...replacements);
133
+ }
134
+ };
135
+ function createLoggy(options) {
136
+ return new Loggy({
137
+ namespace: options.namespace,
138
+ formatOptions: {
139
+ ...DEFAULT_FORMAT_OPTIONS,
140
+ ...options.formatOptions
141
+ },
142
+ formatters: {
143
+ ...DEFAULT_FORMATTERS,
144
+ ...options.formatters
145
+ }
146
+ });
147
+ }
148
+ // Annotate the CommonJS export names for ESM import in node:
149
+ 0 && (module.exports = {
150
+ Loggy,
151
+ createLoggy
152
+ });
153
+ //# sourceMappingURL=index.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/index.ts","../src/loggy.ts","../src/const.ts"],"sourcesContent":["export * from \"./loggy\";\nexport * from \"./types\";\n","import util from \"node:util\";\nimport { type ConsolaInstance, createConsola } from \"consola\";\nimport createDebug from \"debug\";\nimport { DEFAULT_FORMATTERS, DEFAULT_FORMAT_OPTIONS } from \"./const\";\nimport type { AnyLogger, CreateOptions, LoggerOptions } from \"./types\";\n\nexport class Loggy implements AnyLogger {\n #options: LoggerOptions;\n #debug: ReturnType<typeof createDebug>;\n #consola: ConsolaInstance;\n\n constructor(options: LoggerOptions) {\n this.#options = options;\n this.#debug = createDebug(`${options.namespace}:root`);\n this.#consola = createConsola({\n formatOptions: options.formatOptions,\n });\n }\n\n get namespace() {\n return this.#options.namespace;\n }\n\n debug(...args: unknown[]) {\n // @ts-expect-error - it really accepts this signature\n this.#debug(...args);\n }\n\n error(messageOrError: string | unknown, ...args: unknown[]) {\n this.#consola.error(messageOrError, ...args);\n }\n\n info(...args: unknown[]) {\n this.#consola.info(this.#format(...args));\n }\n\n trace(...args: unknown[]) {\n this.#consola.trace(this.#format(...args));\n }\n\n warn(...args: unknown[]) {\n this.#consola.warn(this.#format(...args));\n }\n\n child(namespace: string) {\n return new Loggy({\n ...this.#options,\n namespace: `${this.#options.namespace}:${namespace}`,\n });\n }\n\n start(...args: unknown[]) {\n this.#consola.start(this.#format(...args));\n }\n\n success(...args: unknown[]) {\n this.#consola.success(this.#format(...args));\n }\n\n subdebug(namespace: string) {\n return createDebug(`${this.#options.namespace}:${namespace}`);\n }\n\n #format(...args: unknown[]) {\n const formattedArgs = [...args];\n\n if (typeof formattedArgs[0] !== \"string\") {\n formattedArgs.unshift(\"%O\");\n }\n\n const [message, ...replacements] = formattedArgs;\n let replacementIndex = -1;\n\n // @ts-expect-error - we're sure that message is a string due to the above check\n const formattedMessage = message\n .replace(\n /%([a-zA-Z%])/g, // matches %o, %O, %%, etc.\n (match: string, formatKey: string) => {\n if (formatKey === \"%\") {\n return \"%\";\n }\n\n replacementIndex++;\n\n const formatter = this.#options.formatters[formatKey];\n\n if (typeof formatter !== \"function\") {\n return match;\n }\n\n const replacement = replacements[replacementIndex];\n const formattedMatch = formatter(replacement);\n\n replacements.splice(replacementIndex, 1);\n replacementIndex--;\n\n return formattedMatch;\n },\n )\n .split(\"\\n\")\n .join(`\\n${\" \".repeat(2)}`);\n\n return util.formatWithOptions(this.#options.formatOptions, formattedMessage, ...replacements);\n }\n}\n\nexport function createLoggy(options: CreateOptions) {\n return new Loggy({\n namespace: options.namespace,\n formatOptions: {\n ...DEFAULT_FORMAT_OPTIONS,\n ...options.formatOptions,\n },\n formatters: {\n ...DEFAULT_FORMATTERS,\n ...options.formatters,\n },\n });\n}\n","import util from \"node:util\";\nimport type { FormatOptions, Formatters } from \"./types\";\n\nexport const DEFAULT_FORMAT_OPTIONS: FormatOptions = {\n colors: true,\n depth: 3,\n maxArrayLength: 50,\n breakLength: 80,\n};\n\nexport const DEFAULT_FORMATTERS: Formatters = {\n o: (arg: unknown) => {\n return util\n .inspect(arg, DEFAULT_FORMAT_OPTIONS)\n .split(\"\\n\")\n .map((str) => str.trim())\n .join(\" \");\n },\n O: (arg: unknown) => {\n return util.inspect(arg, DEFAULT_FORMAT_OPTIONS);\n },\n};\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACAA,IAAAA,oBAAiB;AACjB,qBAAoD;AACpD,mBAAwB;;;ACFxB,uBAAiB;AAGV,IAAM,yBAAwC;AAAA,EACnD,QAAQ;AAAA,EACR,OAAO;AAAA,EACP,gBAAgB;AAAA,EAChB,aAAa;AACf;AAEO,IAAM,qBAAiC;AAAA,EAC5C,GAAG,CAAC,QAAiB;AACnB,WAAO,iBAAAC,QACJ,QAAQ,KAAK,sBAAsB,EACnC,MAAM,IAAI,EACV,IAAI,CAAC,QAAQ,IAAI,KAAK,CAAC,EACvB,KAAK,GAAG;AAAA,EACb;AAAA,EACA,GAAG,CAAC,QAAiB;AACnB,WAAO,iBAAAA,QAAK,QAAQ,KAAK,sBAAsB;AAAA,EACjD;AACF;;;ADfO,IAAM,QAAN,MAAM,OAA2B;AAAA,EACtC;AAAA,EACA;AAAA,EACA;AAAA,EAEA,YAAY,SAAwB;AAClC,SAAK,WAAW;AAChB,SAAK,aAAS,aAAAC,SAAY,GAAG,QAAQ,SAAS,OAAO;AACrD,SAAK,eAAW,8BAAc;AAAA,MAC5B,eAAe,QAAQ;AAAA,IACzB,CAAC;AAAA,EACH;AAAA,EAEA,IAAI,YAAY;AACd,WAAO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rB,iBAAO;AAAA,QACT;AAEA;AAEA,cAAM,YAAY,KAAK,SAAS,WAAW,SAAS;AAEpD,YAAI,OAAO,cAAc,YAAY;AACnC,iBAAO;AAAA,QACT;AAEA,cAAM,cAAc,aAAa,gBAAgB;AACjD,cAAM,iBAAiB,UAAU,WAAW;AAE5C,qBAAa,OAAO,kBAAkB,CAAC;AACvC;AAEA,eAAO;AAAA,MACT;AAAA,IACF,EACC,MAAM,IAAI,EACV,KAAK;AAAA,EAAK,IAAI,OAAO,CAAC,CAAC,EAAE;AAE5B,WAAO,kBAAAC,QAAK,kBAAkB,KAAK,SAAS,eAAe,kBAAkB,GAAG,YAAY;AAAA,EAC9F;AACF;AAEO,SAAS,YAAY,SAAwB;AAClD,SAAO,IAAI,MAAM;AAAA,IACf,WAAW,QAAQ;AAAA,IACnB,eAAe;AAAA,MACb,GAAG;AAAA,MACH,GAAG,QAAQ;AAAA,IACb;AAAA,IACA,YAAY;AAAA,MACV,GAAG;AAAA,MACH,GAAG,QAAQ;AAAA,IACb;AAAA,EACF,CAAC;AACH;","names":["import_node_util","util","createDebug","util"]}
@@ -0,0 +1,46 @@
1
+ import createDebug from 'debug';
2
+ import { FormatOptions } from 'consola';
3
+ export { FormatOptions } from 'consola';
4
+
5
+ type AnyLogFn = (...args: unknown[]) => void;
6
+ type Formatters = Record<string, (arg: unknown) => string>;
7
+ type AnyLogger = {
8
+ namespace: string;
9
+ debug: AnyLogFn;
10
+ error: AnyLogFn;
11
+ info: AnyLogFn;
12
+ trace: AnyLogFn;
13
+ warn: AnyLogFn;
14
+ child: (namespace: string) => AnyLogger;
15
+ subdebug: (namespace: string) => AnyLogFn;
16
+ start: AnyLogFn;
17
+ success: AnyLogFn;
18
+ };
19
+ type CreateOptions = {
20
+ namespace: string;
21
+ formatOptions?: FormatOptions;
22
+ formatters?: Formatters;
23
+ };
24
+ type LoggerOptions = {
25
+ namespace: string;
26
+ formatOptions: FormatOptions;
27
+ formatters: Formatters;
28
+ };
29
+
30
+ declare class Loggy implements AnyLogger {
31
+ #private;
32
+ constructor(options: LoggerOptions);
33
+ get namespace(): string;
34
+ debug(...args: unknown[]): void;
35
+ error(messageOrError: string | unknown, ...args: unknown[]): void;
36
+ info(...args: unknown[]): void;
37
+ trace(...args: unknown[]): void;
38
+ warn(...args: unknown[]): void;
39
+ child(namespace: string): Loggy;
40
+ start(...args: unknown[]): void;
41
+ success(...args: unknown[]): void;
42
+ subdebug(namespace: string): createDebug.Debugger;
43
+ }
44
+ declare function createLoggy(options: CreateOptions): Loggy;
45
+
46
+ export { type AnyLogFn, type AnyLogger, type CreateOptions, type Formatters, type LoggerOptions, Loggy, createLoggy };
@@ -0,0 +1,46 @@
1
+ import createDebug from 'debug';
2
+ import { FormatOptions } from 'consola';
3
+ export { FormatOptions } from 'consola';
4
+
5
+ type AnyLogFn = (...args: unknown[]) => void;
6
+ type Formatters = Record<string, (arg: unknown) => string>;
7
+ type AnyLogger = {
8
+ namespace: string;
9
+ debug: AnyLogFn;
10
+ error: AnyLogFn;
11
+ info: AnyLogFn;
12
+ trace: AnyLogFn;
13
+ warn: AnyLogFn;
14
+ child: (namespace: string) => AnyLogger;
15
+ subdebug: (namespace: string) => AnyLogFn;
16
+ start: AnyLogFn;
17
+ success: AnyLogFn;
18
+ };
19
+ type CreateOptions = {
20
+ namespace: string;
21
+ formatOptions?: FormatOptions;
22
+ formatters?: Formatters;
23
+ };
24
+ type LoggerOptions = {
25
+ namespace: string;
26
+ formatOptions: FormatOptions;
27
+ formatters: Formatters;
28
+ };
29
+
30
+ declare class Loggy implements AnyLogger {
31
+ #private;
32
+ constructor(options: LoggerOptions);
33
+ get namespace(): string;
34
+ debug(...args: unknown[]): void;
35
+ error(messageOrError: string | unknown, ...args: unknown[]): void;
36
+ info(...args: unknown[]): void;
37
+ trace(...args: unknown[]): void;
38
+ warn(...args: unknown[]): void;
39
+ child(namespace: string): Loggy;
40
+ start(...args: unknown[]): void;
41
+ success(...args: unknown[]): void;
42
+ subdebug(namespace: string): createDebug.Debugger;
43
+ }
44
+ declare function createLoggy(options: CreateOptions): Loggy;
45
+
46
+ export { type AnyLogFn, type AnyLogger, type CreateOptions, type Formatters, type LoggerOptions, Loggy, createLoggy };
package/dist/index.js ADDED
@@ -0,0 +1,115 @@
1
+ // src/loggy.ts
2
+ import util2 from "node:util";
3
+ import { createConsola } from "consola";
4
+ import createDebug from "debug";
5
+
6
+ // src/const.ts
7
+ import util from "node:util";
8
+ var DEFAULT_FORMAT_OPTIONS = {
9
+ colors: true,
10
+ depth: 3,
11
+ maxArrayLength: 50,
12
+ breakLength: 80
13
+ };
14
+ var DEFAULT_FORMATTERS = {
15
+ o: (arg) => {
16
+ return util.inspect(arg, DEFAULT_FORMAT_OPTIONS).split("\n").map((str) => str.trim()).join(" ");
17
+ },
18
+ O: (arg) => {
19
+ return util.inspect(arg, DEFAULT_FORMAT_OPTIONS);
20
+ }
21
+ };
22
+
23
+ // src/loggy.ts
24
+ var Loggy = class _Loggy {
25
+ #options;
26
+ #debug;
27
+ #consola;
28
+ constructor(options) {
29
+ this.#options = options;
30
+ this.#debug = createDebug(`${options.namespace}:root`);
31
+ this.#consola = createConsola({
32
+ formatOptions: options.formatOptions
33
+ });
34
+ }
35
+ get namespace() {
36
+ return this.#options.namespace;
37
+ }
38
+ debug(...args) {
39
+ this.#debug(...args);
40
+ }
41
+ error(messageOrError, ...args) {
42
+ this.#consola.error(messageOrError, ...args);
43
+ }
44
+ info(...args) {
45
+ this.#consola.info(this.#format(...args));
46
+ }
47
+ trace(...args) {
48
+ this.#consola.trace(this.#format(...args));
49
+ }
50
+ warn(...args) {
51
+ this.#consola.warn(this.#format(...args));
52
+ }
53
+ child(namespace) {
54
+ return new _Loggy({
55
+ ...this.#options,
56
+ namespace: `${this.#options.namespace}:${namespace}`
57
+ });
58
+ }
59
+ start(...args) {
60
+ this.#consola.start(this.#format(...args));
61
+ }
62
+ success(...args) {
63
+ this.#consola.success(this.#format(...args));
64
+ }
65
+ subdebug(namespace) {
66
+ return createDebug(`${this.#options.namespace}:${namespace}`);
67
+ }
68
+ #format(...args) {
69
+ const formattedArgs = [...args];
70
+ if (typeof formattedArgs[0] !== "string") {
71
+ formattedArgs.unshift("%O");
72
+ }
73
+ const [message, ...replacements] = formattedArgs;
74
+ let replacementIndex = -1;
75
+ const formattedMessage = message.replace(
76
+ /%([a-zA-Z%])/g,
77
+ // matches %o, %O, %%, etc.
78
+ (match, formatKey) => {
79
+ if (formatKey === "%") {
80
+ return "%";
81
+ }
82
+ replacementIndex++;
83
+ const formatter = this.#options.formatters[formatKey];
84
+ if (typeof formatter !== "function") {
85
+ return match;
86
+ }
87
+ const replacement = replacements[replacementIndex];
88
+ const formattedMatch = formatter(replacement);
89
+ replacements.splice(replacementIndex, 1);
90
+ replacementIndex--;
91
+ return formattedMatch;
92
+ }
93
+ ).split("\n").join(`
94
+ ${" ".repeat(2)}`);
95
+ return util2.formatWithOptions(this.#options.formatOptions, formattedMessage, ...replacements);
96
+ }
97
+ };
98
+ function createLoggy(options) {
99
+ return new Loggy({
100
+ namespace: options.namespace,
101
+ formatOptions: {
102
+ ...DEFAULT_FORMAT_OPTIONS,
103
+ ...options.formatOptions
104
+ },
105
+ formatters: {
106
+ ...DEFAULT_FORMATTERS,
107
+ ...options.formatters
108
+ }
109
+ });
110
+ }
111
+ export {
112
+ Loggy,
113
+ createLoggy
114
+ };
115
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/loggy.ts","../src/const.ts"],"sourcesContent":["import util from \"node:util\";\nimport { type ConsolaInstance, createConsola } from \"consola\";\nimport createDebug from \"debug\";\nimport { DEFAULT_FORMATTERS, DEFAULT_FORMAT_OPTIONS } from \"./const\";\nimport type { AnyLogger, CreateOptions, LoggerOptions } from \"./types\";\n\nexport class Loggy implements AnyLogger {\n #options: LoggerOptions;\n #debug: ReturnType<typeof createDebug>;\n #consola: ConsolaInstance;\n\n constructor(options: LoggerOptions) {\n this.#options = options;\n this.#debug = createDebug(`${options.namespace}:root`);\n this.#consola = createConsola({\n formatOptions: options.formatOptions,\n });\n }\n\n get namespace() {\n return this.#options.namespace;\n }\n\n debug(...args: unknown[]) {\n // @ts-expect-error - it really accepts this signature\n this.#debug(...args);\n }\n\n error(messageOrError: string | unknown, ...args: unknown[]) {\n this.#consola.error(messageOrError, ...args);\n }\n\n info(...args: unknown[]) {\n this.#consola.info(this.#format(...args));\n }\n\n trace(...args: unknown[]) {\n this.#consola.trace(this.#format(...args));\n }\n\n warn(...args: unknown[]) {\n this.#consola.warn(this.#format(...args));\n }\n\n child(namespace: string) {\n return new Loggy({\n ...this.#options,\n namespace: `${this.#options.namespace}:${namespace}`,\n });\n }\n\n start(...args: unknown[]) {\n this.#consola.start(this.#format(...args));\n }\n\n success(...args: unknown[]) {\n this.#consola.success(this.#format(...args));\n }\n\n subdebug(namespace: string) {\n return createDebug(`${this.#options.namespace}:${namespace}`);\n }\n\n #format(...args: unknown[]) {\n const formattedArgs = [...args];\n\n if (typeof formattedArgs[0] !== \"string\") {\n formattedArgs.unshift(\"%O\");\n }\n\n const [message, ...replacements] = formattedArgs;\n let replacementIndex = -1;\n\n // @ts-expect-error - we're sure that message is a string due to the above check\n const formattedMessage = message\n .replace(\n /%([a-zA-Z%])/g, // matches %o, %O, %%, etc.\n (match: string, formatKey: string) => {\n if (formatKey === \"%\") {\n return \"%\";\n }\n\n replacementIndex++;\n\n const formatter = this.#options.formatters[formatKey];\n\n if (typeof formatter !== \"function\") {\n return match;\n }\n\n const replacement = replacements[replacementIndex];\n const formattedMatch = formatter(replacement);\n\n replacements.splice(replacementIndex, 1);\n replacementIndex--;\n\n return formattedMatch;\n },\n )\n .split(\"\\n\")\n .join(`\\n${\" \".repeat(2)}`);\n\n return util.formatWithOptions(this.#options.formatOptions, formattedMessage, ...replacements);\n }\n}\n\nexport function createLoggy(options: CreateOptions) {\n return new Loggy({\n namespace: options.namespace,\n formatOptions: {\n ...DEFAULT_FORMAT_OPTIONS,\n ...options.formatOptions,\n },\n formatters: {\n ...DEFAULT_FORMATTERS,\n ...options.formatters,\n },\n });\n}\n","import util from \"node:util\";\nimport type { FormatOptions, Formatters } from \"./types\";\n\nexport const DEFAULT_FORMAT_OPTIONS: FormatOptions = {\n colors: true,\n depth: 3,\n maxArrayLength: 50,\n breakLength: 80,\n};\n\nexport const DEFAULT_FORMATTERS: Formatters = {\n o: (arg: unknown) => {\n return util\n .inspect(arg, DEFAULT_FORMAT_OPTIONS)\n .split(\"\\n\")\n .map((str) => str.trim())\n .join(\" \");\n },\n O: (arg: unknown) => {\n return util.inspect(arg, DEFAULT_FORMAT_OPTIONS);\n },\n};\n"],"mappings":";AAAA,OAAOA,WAAU;AACjB,SAA+B,qBAAqB;AACpD,OAAO,iBAAiB;;;ACFxB,OAAO,UAAU;AAGV,IAAM,yBAAwC;AAAA,EACnD,QAAQ;AAAA,EACR,OAAO;AAAA,EACP,gBAAgB;AAAA,EAChB,aAAa;AACf;AAEO,IAAM,qBAAiC;AAAA,EAC5C,GAAG,CAAC,QAAiB;AACnB,WAAO,KACJ,QAAQ,KAAK,sBAAsB,EACnC,MAAM,IAAI,EACV,IAAI,CAAC,QAAQ,IAAI,KAAK,CAAC,EACvB,KAAK,GAAG;AAAA,EACb;AAAA,EACA,GAAG,CAAC,QAAiB;AACnB,WAAO,KAAK,QAAQ,KAAK,sBAAsB;AAAA,EACjD;AACF;;;ADfO,IAAM,QAAN,MAAM,OAA2B;AAAA,EACtC;AAAA,EACA;AAAA,EACA;AAAA,EAEA,YAAY,SAAwB;AAClC,SAAK,WAAW;AAChB,SAAK,SAAS,YAAY,GAAG,QAAQ,SAAS,OAAO;AACrD,SAAK,WAAW,cAAc;AAAA,MAC5B,eAAe,QAAQ;AAAA,IACzB,CAAC;AAAA,EACH;AAAA,EAEA,IAAI,YAAY;AACd,WAAO,KAAK,SAAS;AAAA,EACvB;AAAA,EAEA,SAAS,MAAiB;AAExB,SAAK,OAAO,GAAG,IAAI;AAAA,EACrB;AAA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nC,iBAAO;AAAA,QACT;AAEA,cAAM,cAAc,aAAa,gBAAgB;AACjD,cAAM,iBAAiB,UAAU,WAAW;AAE5C,qBAAa,OAAO,kBAAkB,CAAC;AACvC;AAEA,eAAO;AAAA,MACT;AAAA,IACF,EACC,MAAM,IAAI,EACV,KAAK;AAAA,EAAK,IAAI,OAAO,CAAC,CAAC,EAAE;AAE5B,WAAOC,MAAK,kBAAkB,KAAK,SAAS,eAAe,kBAAkB,GAAG,YAAY;AAAA,EAC9F;AACF;AAEO,SAAS,YAAY,SAAwB;AAClD,SAAO,IAAI,MAAM;AAAA,IACf,WAAW,QAAQ;AAAA,IACnB,eAAe;AAAA,MACb,GAAG;AAAA,MACH,GAAG,QAAQ;AAAA,IACb;AAAA,IACA,YAAY;AAAA,MACV,GAAG;AAAA,MACH,GAAG,QAAQ;AAAA,IACb;AAAA,EACF,CAAC;AACH;","names":["util","util"]}
package/package.json ADDED
@@ -0,0 +1,48 @@
1
+ {
2
+ "name": "@vlandoss/loggy",
3
+ "version": "0.0.1",
4
+ "description": "Console wrapper to make logging fun again",
5
+ "homepage": "https://github.com/variableland/dx/tree/main/packages/loggy#readme",
6
+ "bugs": {
7
+ "url": "https://github.com/variableland/dx/issues"
8
+ },
9
+ "repository": {
10
+ "type": "git",
11
+ "url": "git+https://github.com/variableland/dx.git"
12
+ },
13
+ "license": "MIT",
14
+ "author": "rcrd <rcrd@variable.land>",
15
+ "type": "module",
16
+ "exports": {
17
+ ".": {
18
+ "bun": "./src/index.ts",
19
+ "import": "./dist/index.js",
20
+ "require": "./dist/index.cjs",
21
+ "default": "./dist/index.js"
22
+ }
23
+ },
24
+ "main": "./dist/index.cjs",
25
+ "module": "./dist/index.js",
26
+ "types": "./dist/index.d.ts",
27
+ "files": [
28
+ "dist",
29
+ "src"
30
+ ],
31
+ "publishConfig": {
32
+ "access": "public"
33
+ },
34
+ "engines": {
35
+ "bun": ">=1.0.0"
36
+ },
37
+ "dependencies": {
38
+ "consola": "^3.4.2",
39
+ "debug": "^4.4.0"
40
+ },
41
+ "devDependencies": {
42
+ "@types/debug": "^4.1.12"
43
+ },
44
+ "scripts": {
45
+ "build": "tsup",
46
+ "typecheck": "rr tsc"
47
+ }
48
+ }
package/src/const.ts ADDED
@@ -0,0 +1,22 @@
1
+ import util from "node:util";
2
+ import type { FormatOptions, Formatters } from "./types";
3
+
4
+ export const DEFAULT_FORMAT_OPTIONS: FormatOptions = {
5
+ colors: true,
6
+ depth: 3,
7
+ maxArrayLength: 50,
8
+ breakLength: 80,
9
+ };
10
+
11
+ export const DEFAULT_FORMATTERS: Formatters = {
12
+ o: (arg: unknown) => {
13
+ return util
14
+ .inspect(arg, DEFAULT_FORMAT_OPTIONS)
15
+ .split("\n")
16
+ .map((str) => str.trim())
17
+ .join(" ");
18
+ },
19
+ O: (arg: unknown) => {
20
+ return util.inspect(arg, DEFAULT_FORMAT_OPTIONS);
21
+ },
22
+ };
package/src/index.ts ADDED
@@ -0,0 +1,2 @@
1
+ export * from "./loggy";
2
+ export * from "./types";
package/src/loggy.ts ADDED
@@ -0,0 +1,119 @@
1
+ import util from "node:util";
2
+ import { type ConsolaInstance, createConsola } from "consola";
3
+ import createDebug from "debug";
4
+ import { DEFAULT_FORMATTERS, DEFAULT_FORMAT_OPTIONS } from "./const";
5
+ import type { AnyLogger, CreateOptions, LoggerOptions } from "./types";
6
+
7
+ export class Loggy implements AnyLogger {
8
+ #options: LoggerOptions;
9
+ #debug: ReturnType<typeof createDebug>;
10
+ #consola: ConsolaInstance;
11
+
12
+ constructor(options: LoggerOptions) {
13
+ this.#options = options;
14
+ this.#debug = createDebug(`${options.namespace}:root`);
15
+ this.#consola = createConsola({
16
+ formatOptions: options.formatOptions,
17
+ });
18
+ }
19
+
20
+ get namespace() {
21
+ return this.#options.namespace;
22
+ }
23
+
24
+ debug(...args: unknown[]) {
25
+ // @ts-expect-error - it really accepts this signature
26
+ this.#debug(...args);
27
+ }
28
+
29
+ error(messageOrError: string | unknown, ...args: unknown[]) {
30
+ this.#consola.error(messageOrError, ...args);
31
+ }
32
+
33
+ info(...args: unknown[]) {
34
+ this.#consola.info(this.#format(...args));
35
+ }
36
+
37
+ trace(...args: unknown[]) {
38
+ this.#consola.trace(this.#format(...args));
39
+ }
40
+
41
+ warn(...args: unknown[]) {
42
+ this.#consola.warn(this.#format(...args));
43
+ }
44
+
45
+ child(namespace: string) {
46
+ return new Loggy({
47
+ ...this.#options,
48
+ namespace: `${this.#options.namespace}:${namespace}`,
49
+ });
50
+ }
51
+
52
+ start(...args: unknown[]) {
53
+ this.#consola.start(this.#format(...args));
54
+ }
55
+
56
+ success(...args: unknown[]) {
57
+ this.#consola.success(this.#format(...args));
58
+ }
59
+
60
+ subdebug(namespace: string) {
61
+ return createDebug(`${this.#options.namespace}:${namespace}`);
62
+ }
63
+
64
+ #format(...args: unknown[]) {
65
+ const formattedArgs = [...args];
66
+
67
+ if (typeof formattedArgs[0] !== "string") {
68
+ formattedArgs.unshift("%O");
69
+ }
70
+
71
+ const [message, ...replacements] = formattedArgs;
72
+ let replacementIndex = -1;
73
+
74
+ // @ts-expect-error - we're sure that message is a string due to the above check
75
+ const formattedMessage = message
76
+ .replace(
77
+ /%([a-zA-Z%])/g, // matches %o, %O, %%, etc.
78
+ (match: string, formatKey: string) => {
79
+ if (formatKey === "%") {
80
+ return "%";
81
+ }
82
+
83
+ replacementIndex++;
84
+
85
+ const formatter = this.#options.formatters[formatKey];
86
+
87
+ if (typeof formatter !== "function") {
88
+ return match;
89
+ }
90
+
91
+ const replacement = replacements[replacementIndex];
92
+ const formattedMatch = formatter(replacement);
93
+
94
+ replacements.splice(replacementIndex, 1);
95
+ replacementIndex--;
96
+
97
+ return formattedMatch;
98
+ },
99
+ )
100
+ .split("\n")
101
+ .join(`\n${" ".repeat(2)}`);
102
+
103
+ return util.formatWithOptions(this.#options.formatOptions, formattedMessage, ...replacements);
104
+ }
105
+ }
106
+
107
+ export function createLoggy(options: CreateOptions) {
108
+ return new Loggy({
109
+ namespace: options.namespace,
110
+ formatOptions: {
111
+ ...DEFAULT_FORMAT_OPTIONS,
112
+ ...options.formatOptions,
113
+ },
114
+ formatters: {
115
+ ...DEFAULT_FORMATTERS,
116
+ ...options.formatters,
117
+ },
118
+ });
119
+ }
package/src/types.ts ADDED
@@ -0,0 +1,34 @@
1
+ import type { FormatOptions } from "consola";
2
+
3
+ export type AnyLogFn = (...args: unknown[]) => void;
4
+
5
+ export type Formatters = Record<string, (arg: unknown) => string>;
6
+
7
+ export type AnyLogger = {
8
+ namespace: string;
9
+ debug: AnyLogFn;
10
+ error: AnyLogFn;
11
+ info: AnyLogFn;
12
+ trace: AnyLogFn;
13
+ warn: AnyLogFn;
14
+ child: (namespace: string) => AnyLogger;
15
+ // { extras
16
+ subdebug: (namespace: string) => AnyLogFn;
17
+ start: AnyLogFn;
18
+ success: AnyLogFn;
19
+ // }
20
+ };
21
+
22
+ export type CreateOptions = {
23
+ namespace: string;
24
+ formatOptions?: FormatOptions;
25
+ formatters?: Formatters;
26
+ };
27
+
28
+ export type LoggerOptions = {
29
+ namespace: string;
30
+ formatOptions: FormatOptions;
31
+ formatters: Formatters;
32
+ };
33
+
34
+ export type { FormatOptions };