@vlandoss/loggy 0.0.1 → 0.0.2-git-8d13199.0

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/index.cjs CHANGED
@@ -38,6 +38,7 @@ module.exports = __toCommonJS(index_exports);
38
38
  // src/loggy.ts
39
39
  var import_node_util2 = __toESM(require("util"), 1);
40
40
  var import_consola = require("consola");
41
+ var import_utils = require("consola/utils");
41
42
  var import_debug = __toESM(require("debug"), 1);
42
43
 
43
44
  // src/const.ts
@@ -58,6 +59,13 @@ var DEFAULT_FORMATTERS = {
58
59
  };
59
60
 
60
61
  // src/loggy.ts
62
+ function isLogFnOptions(arg) {
63
+ try {
64
+ return typeof arg.message === "string" && typeof arg.tag === "string";
65
+ } catch {
66
+ return false;
67
+ }
68
+ }
61
69
  var Loggy = class _Loggy {
62
70
  #options;
63
71
  #debug;
@@ -75,41 +83,47 @@ var Loggy = class _Loggy {
75
83
  debug(...args) {
76
84
  this.#debug(...args);
77
85
  }
78
- error(messageOrError, ...args) {
79
- this.#consola.error(messageOrError, ...args);
86
+ error(...args) {
87
+ this.#consola.error(this.#format(...args));
80
88
  }
81
- info(...args) {
82
- this.#consola.info(this.#format(...args));
89
+ info(opts, ...args) {
90
+ this.#consola.info(this.#format(opts, ...args));
83
91
  }
84
- trace(...args) {
85
- this.#consola.trace(this.#format(...args));
92
+ trace(opts, ...args) {
93
+ this.#consola.trace(this.#format(opts, ...args));
86
94
  }
87
- warn(...args) {
88
- this.#consola.warn(this.#format(...args));
95
+ warn(opts, ...args) {
96
+ this.#consola.warn(this.#format(opts, ...args));
89
97
  }
90
- child(namespace) {
98
+ child(options) {
91
99
  return new _Loggy({
92
100
  ...this.#options,
93
- namespace: `${this.#options.namespace}:${namespace}`
101
+ ...options,
102
+ namespace: `${this.#options.namespace}:${options.namespace}`
94
103
  });
95
104
  }
96
- start(...args) {
97
- this.#consola.start(this.#format(...args));
105
+ start(opts, ...args) {
106
+ this.#consola.start(this.#format(opts, ...args));
98
107
  }
99
- success(...args) {
100
- this.#consola.success(this.#format(...args));
108
+ success(opts, ...args) {
109
+ this.#consola.success(this.#format(opts, ...args));
101
110
  }
102
111
  subdebug(namespace) {
103
112
  return (0, import_debug.default)(`${this.#options.namespace}:${namespace}`);
104
113
  }
105
114
  #format(...args) {
106
- const formattedArgs = [...args];
115
+ const [firstArg, ...restArgs] = args;
116
+ const tag = isLogFnOptions(firstArg) ? firstArg.tag : this.#options.tag;
117
+ const formattedArgs = isLogFnOptions(firstArg) ? [firstArg.message, ...restArgs] : args;
107
118
  if (typeof formattedArgs[0] !== "string") {
108
119
  formattedArgs.unshift("%O");
109
120
  }
110
121
  const [message, ...replacements] = formattedArgs;
111
122
  let replacementIndex = -1;
112
- const formattedMessage = message.replace(
123
+ if (typeof message !== "string") {
124
+ throw new TypeError("message must be a string");
125
+ }
126
+ let formattedMessage = message.replace(
113
127
  /%([a-zA-Z%])/g,
114
128
  // matches %o, %O, %%, etc.
115
129
  (match, formatKey) => {
@@ -129,12 +143,14 @@ var Loggy = class _Loggy {
129
143
  }
130
144
  ).split("\n").join(`
131
145
  ${" ".repeat(2)}`);
146
+ formattedMessage = !tag ? formattedMessage : `${import_utils.colors.cyan(`[${tag}]`)} ${formattedMessage}`;
132
147
  return import_node_util2.default.formatWithOptions(this.#options.formatOptions, formattedMessage, ...replacements);
133
148
  }
134
149
  };
135
150
  function createLoggy(options) {
136
151
  return new Loggy({
137
152
  namespace: options.namespace,
153
+ tag: options.tag,
138
154
  formatOptions: {
139
155
  ...DEFAULT_FORMAT_OPTIONS,
140
156
  ...options.formatOptions
@@ -1 +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,aAA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AM,CAAC,SAAS,GAAG,YAAY,IAAI;AACnC,QAAI,mBAAmB;AAGvB,UAAM,mBAAmB,QACtB;AAAA,MACC;AAAA;AAAA,MACA,CAAC,OAAe,cAAsB;AACpC,YAAI,cAAc,KAAK;AACrB,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"]}
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 { colors } from \"consola/utils\";\nimport createDebug from \"debug\";\nimport { DEFAULT_FORMATTERS, DEFAULT_FORMAT_OPTIONS } from \"./const\";\nimport type { AnyLogger, CreateOptions, LogFnOptions, LoggerOptions } from \"./types\";\n\nfunction isLogFnOptions(arg: unknown): arg is LogFnOptions {\n try {\n // @ts-expect-error\n return typeof arg.message === \"string\" && typeof arg.tag === \"string\";\n } catch {\n return false;\n }\n}\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(...args: unknown[]) {\n this.#consola.error(this.#format(...args));\n }\n\n info(opts: LogFnOptions | unknown, ...args: unknown[]) {\n this.#consola.info(this.#format(opts, ...args));\n }\n\n trace(opts: LogFnOptions | unknown, ...args: unknown[]) {\n this.#consola.trace(this.#format(opts, ...args));\n }\n\n warn(opts: LogFnOptions | unknown, ...args: unknown[]) {\n this.#consola.warn(this.#format(opts, ...args));\n }\n\n child(options: CreateOptions) {\n return new Loggy({\n ...this.#options,\n ...options,\n namespace: `${this.#options.namespace}:${options.namespace}`,\n });\n }\n\n start(opts: LogFnOptions | unknown, ...args: unknown[]) {\n this.#consola.start(this.#format(opts, ...args));\n }\n\n success(opts: LogFnOptions | unknown, ...args: unknown[]) {\n this.#consola.success(this.#format(opts, ...args));\n }\n\n subdebug(namespace: string) {\n return createDebug(`${this.#options.namespace}:${namespace}`);\n }\n\n #format(...args: unknown[]) {\n const [firstArg, ...restArgs] = args;\n\n const tag = isLogFnOptions(firstArg) ? firstArg.tag : this.#options.tag;\n const formattedArgs = isLogFnOptions(firstArg) ? [firstArg.message, ...restArgs] : 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 if (typeof message !== \"string\") {\n throw new TypeError(\"message must be a string\");\n }\n\n let 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 formattedMessage = !tag ? formattedMessage : `${colors.cyan(`[${tag}]`)} ${formattedMessage}`;\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 tag: options.tag,\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,mBAAuB;AACvB,mBAAwB;;;ACHxB,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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E;AAE5B,uBAAmB,CAAC,MAAM,mBAAmB,GAAG,oBAAO,KAAK,IAAI,GAAG,GAAG,CAAC,IAAI,gBAAgB;AAE3F,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,KAAK,QAAQ;AAAA,IACb,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"]}
package/dist/index.d.cts CHANGED
@@ -11,36 +11,42 @@ type AnyLogger = {
11
11
  info: AnyLogFn;
12
12
  trace: AnyLogFn;
13
13
  warn: AnyLogFn;
14
- child: (namespace: string) => AnyLogger;
14
+ child: (options: CreateOptions) => AnyLogger;
15
15
  subdebug: (namespace: string) => AnyLogFn;
16
16
  start: AnyLogFn;
17
17
  success: AnyLogFn;
18
18
  };
19
19
  type CreateOptions = {
20
20
  namespace: string;
21
+ tag?: string;
21
22
  formatOptions?: FormatOptions;
22
23
  formatters?: Formatters;
23
24
  };
24
25
  type LoggerOptions = {
26
+ tag?: string;
25
27
  namespace: string;
26
28
  formatOptions: FormatOptions;
27
29
  formatters: Formatters;
28
30
  };
31
+ type LogFnOptions = {
32
+ tag: string;
33
+ message: string;
34
+ };
29
35
 
30
36
  declare class Loggy implements AnyLogger {
31
37
  #private;
32
38
  constructor(options: LoggerOptions);
33
39
  get namespace(): string;
34
40
  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;
41
+ error(...args: unknown[]): void;
42
+ info(opts: LogFnOptions | unknown, ...args: unknown[]): void;
43
+ trace(opts: LogFnOptions | unknown, ...args: unknown[]): void;
44
+ warn(opts: LogFnOptions | unknown, ...args: unknown[]): void;
45
+ child(options: CreateOptions): Loggy;
46
+ start(opts: LogFnOptions | unknown, ...args: unknown[]): void;
47
+ success(opts: LogFnOptions | unknown, ...args: unknown[]): void;
42
48
  subdebug(namespace: string): createDebug.Debugger;
43
49
  }
44
50
  declare function createLoggy(options: CreateOptions): Loggy;
45
51
 
46
- export { type AnyLogFn, type AnyLogger, type CreateOptions, type Formatters, type LoggerOptions, Loggy, createLoggy };
52
+ export { type AnyLogFn, type AnyLogger, type CreateOptions, type Formatters, type LogFnOptions, type LoggerOptions, Loggy, createLoggy };
package/dist/index.d.ts CHANGED
@@ -11,36 +11,42 @@ type AnyLogger = {
11
11
  info: AnyLogFn;
12
12
  trace: AnyLogFn;
13
13
  warn: AnyLogFn;
14
- child: (namespace: string) => AnyLogger;
14
+ child: (options: CreateOptions) => AnyLogger;
15
15
  subdebug: (namespace: string) => AnyLogFn;
16
16
  start: AnyLogFn;
17
17
  success: AnyLogFn;
18
18
  };
19
19
  type CreateOptions = {
20
20
  namespace: string;
21
+ tag?: string;
21
22
  formatOptions?: FormatOptions;
22
23
  formatters?: Formatters;
23
24
  };
24
25
  type LoggerOptions = {
26
+ tag?: string;
25
27
  namespace: string;
26
28
  formatOptions: FormatOptions;
27
29
  formatters: Formatters;
28
30
  };
31
+ type LogFnOptions = {
32
+ tag: string;
33
+ message: string;
34
+ };
29
35
 
30
36
  declare class Loggy implements AnyLogger {
31
37
  #private;
32
38
  constructor(options: LoggerOptions);
33
39
  get namespace(): string;
34
40
  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;
41
+ error(...args: unknown[]): void;
42
+ info(opts: LogFnOptions | unknown, ...args: unknown[]): void;
43
+ trace(opts: LogFnOptions | unknown, ...args: unknown[]): void;
44
+ warn(opts: LogFnOptions | unknown, ...args: unknown[]): void;
45
+ child(options: CreateOptions): Loggy;
46
+ start(opts: LogFnOptions | unknown, ...args: unknown[]): void;
47
+ success(opts: LogFnOptions | unknown, ...args: unknown[]): void;
42
48
  subdebug(namespace: string): createDebug.Debugger;
43
49
  }
44
50
  declare function createLoggy(options: CreateOptions): Loggy;
45
51
 
46
- export { type AnyLogFn, type AnyLogger, type CreateOptions, type Formatters, type LoggerOptions, Loggy, createLoggy };
52
+ export { type AnyLogFn, type AnyLogger, type CreateOptions, type Formatters, type LogFnOptions, type LoggerOptions, Loggy, createLoggy };
package/dist/index.js CHANGED
@@ -1,6 +1,7 @@
1
1
  // src/loggy.ts
2
2
  import util2 from "node:util";
3
3
  import { createConsola } from "consola";
4
+ import { colors } from "consola/utils";
4
5
  import createDebug from "debug";
5
6
 
6
7
  // src/const.ts
@@ -21,6 +22,13 @@ var DEFAULT_FORMATTERS = {
21
22
  };
22
23
 
23
24
  // src/loggy.ts
25
+ function isLogFnOptions(arg) {
26
+ try {
27
+ return typeof arg.message === "string" && typeof arg.tag === "string";
28
+ } catch {
29
+ return false;
30
+ }
31
+ }
24
32
  var Loggy = class _Loggy {
25
33
  #options;
26
34
  #debug;
@@ -38,41 +46,47 @@ var Loggy = class _Loggy {
38
46
  debug(...args) {
39
47
  this.#debug(...args);
40
48
  }
41
- error(messageOrError, ...args) {
42
- this.#consola.error(messageOrError, ...args);
49
+ error(...args) {
50
+ this.#consola.error(this.#format(...args));
43
51
  }
44
- info(...args) {
45
- this.#consola.info(this.#format(...args));
52
+ info(opts, ...args) {
53
+ this.#consola.info(this.#format(opts, ...args));
46
54
  }
47
- trace(...args) {
48
- this.#consola.trace(this.#format(...args));
55
+ trace(opts, ...args) {
56
+ this.#consola.trace(this.#format(opts, ...args));
49
57
  }
50
- warn(...args) {
51
- this.#consola.warn(this.#format(...args));
58
+ warn(opts, ...args) {
59
+ this.#consola.warn(this.#format(opts, ...args));
52
60
  }
53
- child(namespace) {
61
+ child(options) {
54
62
  return new _Loggy({
55
63
  ...this.#options,
56
- namespace: `${this.#options.namespace}:${namespace}`
64
+ ...options,
65
+ namespace: `${this.#options.namespace}:${options.namespace}`
57
66
  });
58
67
  }
59
- start(...args) {
60
- this.#consola.start(this.#format(...args));
68
+ start(opts, ...args) {
69
+ this.#consola.start(this.#format(opts, ...args));
61
70
  }
62
- success(...args) {
63
- this.#consola.success(this.#format(...args));
71
+ success(opts, ...args) {
72
+ this.#consola.success(this.#format(opts, ...args));
64
73
  }
65
74
  subdebug(namespace) {
66
75
  return createDebug(`${this.#options.namespace}:${namespace}`);
67
76
  }
68
77
  #format(...args) {
69
- const formattedArgs = [...args];
78
+ const [firstArg, ...restArgs] = args;
79
+ const tag = isLogFnOptions(firstArg) ? firstArg.tag : this.#options.tag;
80
+ const formattedArgs = isLogFnOptions(firstArg) ? [firstArg.message, ...restArgs] : args;
70
81
  if (typeof formattedArgs[0] !== "string") {
71
82
  formattedArgs.unshift("%O");
72
83
  }
73
84
  const [message, ...replacements] = formattedArgs;
74
85
  let replacementIndex = -1;
75
- const formattedMessage = message.replace(
86
+ if (typeof message !== "string") {
87
+ throw new TypeError("message must be a string");
88
+ }
89
+ let formattedMessage = message.replace(
76
90
  /%([a-zA-Z%])/g,
77
91
  // matches %o, %O, %%, etc.
78
92
  (match, formatKey) => {
@@ -92,12 +106,14 @@ var Loggy = class _Loggy {
92
106
  }
93
107
  ).split("\n").join(`
94
108
  ${" ".repeat(2)}`);
109
+ formattedMessage = !tag ? formattedMessage : `${colors.cyan(`[${tag}]`)} ${formattedMessage}`;
95
110
  return util2.formatWithOptions(this.#options.formatOptions, formattedMessage, ...replacements);
96
111
  }
97
112
  };
98
113
  function createLoggy(options) {
99
114
  return new Loggy({
100
115
  namespace: options.namespace,
116
+ tag: options.tag,
101
117
  formatOptions: {
102
118
  ...DEFAULT_FORMAT_OPTIONS,
103
119
  ...options.formatOptions
package/dist/index.js.map CHANGED
@@ -1 +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,E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S,IAAI,SAAS,EAAE;AAAA,EAC9D;AAAA,EAEA,WAAW,MAAiB;AAC1B,UAAM,gBAAgB,CAAC,GAAG,IAAI;AAE9B,QAAI,OAAO,cAAc,CAAC,MAAM,UAAU;AACxC,oBAAc,QAAQ,IAAI;AAAA,IAC5B;AAEA,UAAM,CAAC,SAAS,GAAG,YAAY,IAAI;AACnC,QAAI,mBAAmB;AAGvB,UAAM,mBAAmB,QACtB;AAAA,MACC;AAAA;AAAA,MACA,CAAC,OAAe,cAAsB;AACpC,YAAI,cAAc,KAAK;AACrB,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,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"]}
1
+ {"version":3,"sources":["../src/loggy.ts","../src/const.ts"],"sourcesContent":["import util from \"node:util\";\nimport { type ConsolaInstance, createConsola } from \"consola\";\nimport { colors } from \"consola/utils\";\nimport createDebug from \"debug\";\nimport { DEFAULT_FORMATTERS, DEFAULT_FORMAT_OPTIONS } from \"./const\";\nimport type { AnyLogger, CreateOptions, LogFnOptions, LoggerOptions } from \"./types\";\n\nfunction isLogFnOptions(arg: unknown): arg is LogFnOptions {\n try {\n // @ts-expect-error\n return typeof arg.message === \"string\" && typeof arg.tag === \"string\";\n } catch {\n return false;\n }\n}\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(...args: unknown[]) {\n this.#consola.error(this.#format(...args));\n }\n\n info(opts: LogFnOptions | unknown, ...args: unknown[]) {\n this.#consola.info(this.#format(opts, ...args));\n }\n\n trace(opts: LogFnOptions | unknown, ...args: unknown[]) {\n this.#consola.trace(this.#format(opts, ...args));\n }\n\n warn(opts: LogFnOptions | unknown, ...args: unknown[]) {\n this.#consola.warn(this.#format(opts, ...args));\n }\n\n child(options: CreateOptions) {\n return new Loggy({\n ...this.#options,\n ...options,\n namespace: `${this.#options.namespace}:${options.namespace}`,\n });\n }\n\n start(opts: LogFnOptions | unknown, ...args: unknown[]) {\n this.#consola.start(this.#format(opts, ...args));\n }\n\n success(opts: LogFnOptions | unknown, ...args: unknown[]) {\n this.#consola.success(this.#format(opts, ...args));\n }\n\n subdebug(namespace: string) {\n return createDebug(`${this.#options.namespace}:${namespace}`);\n }\n\n #format(...args: unknown[]) {\n const [firstArg, ...restArgs] = args;\n\n const tag = isLogFnOptions(firstArg) ? firstArg.tag : this.#options.tag;\n const formattedArgs = isLogFnOptions(firstArg) ? [firstArg.message, ...restArgs] : 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 if (typeof message !== \"string\") {\n throw new TypeError(\"message must be a string\");\n }\n\n let 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 formattedMessage = !tag ? formattedMessage : `${colors.cyan(`[${tag}]`)} ${formattedMessage}`;\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 tag: options.tag,\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,SAAS,cAAc;AACvB,OAAO,iBAAiB;;;ACHxB,OAAO,UAAU;AAGV,IAAM,yBAAwC;AAAA,EACnD,QAAQ;AAAA,EACR,OAAO;AAAA,EACP,gBAAgB;AAAA,EAChB,aAAa;AACf;AAEO,IAAM,qBAAiC;AAAA,EAC5C,GAAG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;AAAA,EAAK,IAAI,OAAO,CAAC,CAAC,EAAE;AAE5B,uBAAmB,CAAC,MAAM,mBAAmB,GAAG,OAAO,KAAK,IAAI,GAAG,GAAG,CAAC,IAAI,gBAAgB;AAE3F,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,KAAK,QAAQ;AAAA,IACb,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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@vlandoss/loggy",
3
- "version": "0.0.1",
3
+ "version": "0.0.2-git-8d13199.0",
4
4
  "description": "Console wrapper to make logging fun again",
5
5
  "homepage": "https://github.com/variableland/dx/tree/main/packages/loggy#readme",
6
6
  "bugs": {
@@ -42,7 +42,6 @@
42
42
  "@types/debug": "^4.1.12"
43
43
  },
44
44
  "scripts": {
45
- "build": "tsup",
46
- "typecheck": "rr tsc"
45
+ "build": "tsup"
47
46
  }
48
47
  }
package/src/loggy.ts CHANGED
@@ -1,8 +1,18 @@
1
1
  import util from "node:util";
2
2
  import { type ConsolaInstance, createConsola } from "consola";
3
+ import { colors } from "consola/utils";
3
4
  import createDebug from "debug";
4
5
  import { DEFAULT_FORMATTERS, DEFAULT_FORMAT_OPTIONS } from "./const";
5
- import type { AnyLogger, CreateOptions, LoggerOptions } from "./types";
6
+ import type { AnyLogger, CreateOptions, LogFnOptions, LoggerOptions } from "./types";
7
+
8
+ function isLogFnOptions(arg: unknown): arg is LogFnOptions {
9
+ try {
10
+ // @ts-expect-error
11
+ return typeof arg.message === "string" && typeof arg.tag === "string";
12
+ } catch {
13
+ return false;
14
+ }
15
+ }
6
16
 
7
17
  export class Loggy implements AnyLogger {
8
18
  #options: LoggerOptions;
@@ -26,35 +36,36 @@ export class Loggy implements AnyLogger {
26
36
  this.#debug(...args);
27
37
  }
28
38
 
29
- error(messageOrError: string | unknown, ...args: unknown[]) {
30
- this.#consola.error(messageOrError, ...args);
39
+ error(...args: unknown[]) {
40
+ this.#consola.error(this.#format(...args));
31
41
  }
32
42
 
33
- info(...args: unknown[]) {
34
- this.#consola.info(this.#format(...args));
43
+ info(opts: LogFnOptions | unknown, ...args: unknown[]) {
44
+ this.#consola.info(this.#format(opts, ...args));
35
45
  }
36
46
 
37
- trace(...args: unknown[]) {
38
- this.#consola.trace(this.#format(...args));
47
+ trace(opts: LogFnOptions | unknown, ...args: unknown[]) {
48
+ this.#consola.trace(this.#format(opts, ...args));
39
49
  }
40
50
 
41
- warn(...args: unknown[]) {
42
- this.#consola.warn(this.#format(...args));
51
+ warn(opts: LogFnOptions | unknown, ...args: unknown[]) {
52
+ this.#consola.warn(this.#format(opts, ...args));
43
53
  }
44
54
 
45
- child(namespace: string) {
55
+ child(options: CreateOptions) {
46
56
  return new Loggy({
47
57
  ...this.#options,
48
- namespace: `${this.#options.namespace}:${namespace}`,
58
+ ...options,
59
+ namespace: `${this.#options.namespace}:${options.namespace}`,
49
60
  });
50
61
  }
51
62
 
52
- start(...args: unknown[]) {
53
- this.#consola.start(this.#format(...args));
63
+ start(opts: LogFnOptions | unknown, ...args: unknown[]) {
64
+ this.#consola.start(this.#format(opts, ...args));
54
65
  }
55
66
 
56
- success(...args: unknown[]) {
57
- this.#consola.success(this.#format(...args));
67
+ success(opts: LogFnOptions | unknown, ...args: unknown[]) {
68
+ this.#consola.success(this.#format(opts, ...args));
58
69
  }
59
70
 
60
71
  subdebug(namespace: string) {
@@ -62,7 +73,10 @@ export class Loggy implements AnyLogger {
62
73
  }
63
74
 
64
75
  #format(...args: unknown[]) {
65
- const formattedArgs = [...args];
76
+ const [firstArg, ...restArgs] = args;
77
+
78
+ const tag = isLogFnOptions(firstArg) ? firstArg.tag : this.#options.tag;
79
+ const formattedArgs = isLogFnOptions(firstArg) ? [firstArg.message, ...restArgs] : args;
66
80
 
67
81
  if (typeof formattedArgs[0] !== "string") {
68
82
  formattedArgs.unshift("%O");
@@ -71,8 +85,11 @@ export class Loggy implements AnyLogger {
71
85
  const [message, ...replacements] = formattedArgs;
72
86
  let replacementIndex = -1;
73
87
 
74
- // @ts-expect-error - we're sure that message is a string due to the above check
75
- const formattedMessage = message
88
+ if (typeof message !== "string") {
89
+ throw new TypeError("message must be a string");
90
+ }
91
+
92
+ let formattedMessage = message
76
93
  .replace(
77
94
  /%([a-zA-Z%])/g, // matches %o, %O, %%, etc.
78
95
  (match: string, formatKey: string) => {
@@ -100,6 +117,8 @@ export class Loggy implements AnyLogger {
100
117
  .split("\n")
101
118
  .join(`\n${" ".repeat(2)}`);
102
119
 
120
+ formattedMessage = !tag ? formattedMessage : `${colors.cyan(`[${tag}]`)} ${formattedMessage}`;
121
+
103
122
  return util.formatWithOptions(this.#options.formatOptions, formattedMessage, ...replacements);
104
123
  }
105
124
  }
@@ -107,6 +126,7 @@ export class Loggy implements AnyLogger {
107
126
  export function createLoggy(options: CreateOptions) {
108
127
  return new Loggy({
109
128
  namespace: options.namespace,
129
+ tag: options.tag,
110
130
  formatOptions: {
111
131
  ...DEFAULT_FORMAT_OPTIONS,
112
132
  ...options.formatOptions,
package/src/types.ts CHANGED
@@ -11,7 +11,7 @@ export type AnyLogger = {
11
11
  info: AnyLogFn;
12
12
  trace: AnyLogFn;
13
13
  warn: AnyLogFn;
14
- child: (namespace: string) => AnyLogger;
14
+ child: (options: CreateOptions) => AnyLogger;
15
15
  // { extras
16
16
  subdebug: (namespace: string) => AnyLogFn;
17
17
  start: AnyLogFn;
@@ -21,14 +21,21 @@ export type AnyLogger = {
21
21
 
22
22
  export type CreateOptions = {
23
23
  namespace: string;
24
+ tag?: string;
24
25
  formatOptions?: FormatOptions;
25
26
  formatters?: Formatters;
26
27
  };
27
28
 
28
29
  export type LoggerOptions = {
30
+ tag?: string;
29
31
  namespace: string;
30
32
  formatOptions: FormatOptions;
31
33
  formatters: Formatters;
32
34
  };
33
35
 
36
+ export type LogFnOptions = {
37
+ tag: string;
38
+ message: string;
39
+ };
40
+
34
41
  export type { FormatOptions };