mynth-logger 2.0.9 → 2.0.11

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.
@@ -21,7 +21,7 @@ jobs:
21
21
  check-latest: true
22
22
 
23
23
  - name: Update npm
24
- run: npm install -g npm@latest
24
+ run: npm install -g npm@"<11"
25
25
 
26
26
  - name: Install dependencies
27
27
  run: npm ci --include dev
@@ -43,7 +43,7 @@ jobs:
43
43
  check-latest: true
44
44
 
45
45
  - name: Update npm
46
- run: npm install -g npm@latest
46
+ run: npm install -g npm@"<11"
47
47
 
48
48
  - name: Install dependencies
49
49
  run: npm ci --include dev
@@ -66,7 +66,7 @@ jobs:
66
66
  check-latest: true
67
67
 
68
68
  - name: Update npm
69
- run: npm install -g npm@latest
69
+ run: npm install -g npm@"<11"
70
70
 
71
71
  - name: Install dependencies
72
72
  run: npm install --include dev
@@ -29,7 +29,7 @@ jobs:
29
29
  check-latest: true
30
30
 
31
31
  - name: Update npm
32
- run: npm install -g npm@latest
32
+ run: npm install -g npm@"<11"
33
33
 
34
34
  - name: Install dependencies
35
35
  run: npm install
@@ -0,0 +1,13 @@
1
+ declare enum color {
2
+ green = "2404635",
3
+ red = "11606811",
4
+ yellow = "11644443"
5
+ }
6
+ declare const discord: {
7
+ configure: (webhookUrl: string) => void;
8
+ debug: (title: string, message: string, color?: color | string) => void;
9
+ error: (title: string, message: string, color?: color | string) => void;
10
+ info: (title: string, message: string, color?: color | string) => void;
11
+ log: (title: string, message: string, color?: color | string) => void;
12
+ };
13
+ export { color, discord };
@@ -0,0 +1,33 @@
1
+ import { type } from "arktype";
2
+ var color;
3
+ (function (color) {
4
+ color["green"] = "2404635";
5
+ color["red"] = "11606811";
6
+ color["yellow"] = "11644443";
7
+ })(color || (color = {}));
8
+ const green = color.green;
9
+ const red = color.red;
10
+ const yellow = color.yellow;
11
+ const log = (level, color, title, message) => {
12
+ const settings = {
13
+ color,
14
+ discord: true,
15
+ title,
16
+ };
17
+ console[level](message, settings);
18
+ };
19
+ const discord = {
20
+ configure: (webhookUrl) => {
21
+ type("string.url").assert(webhookUrl);
22
+ const settings = {
23
+ discord: true,
24
+ setWebhookUrl: webhookUrl,
25
+ };
26
+ console.debug(settings);
27
+ },
28
+ debug: (title, message, color = yellow) => log("debug", color, title, message),
29
+ error: (title, message, color = red) => log("error", color, title, message),
30
+ info: (title, message, color = green) => log("info", color, title, message),
31
+ log: (title, message, color = green) => log("log", color, title, message),
32
+ };
33
+ export { color, discord };
@@ -1 +1,2 @@
1
1
  export * from "./logging.js";
2
+ export * from "./discord.js";
package/dist/src/index.js CHANGED
@@ -1 +1,2 @@
1
1
  export * from "./logging.js";
2
+ export * from "./discord.js";
@@ -1,5 +1,19 @@
1
1
  import { LogObject } from "consola";
2
+ declare const Discord: import("arktype/internal/methods/object.ts").ObjectType<{
3
+ discord: true;
4
+ color: string;
5
+ title: string;
6
+ webhookUrl?: string | undefined;
7
+ }, {}>;
8
+ declare const ConfigureDiscord: import("arktype/internal/methods/object.ts").ObjectType<{
9
+ discord: true;
10
+ setWebhookUrl: string;
11
+ }, {}>;
12
+ type Discord = typeof Discord.infer;
13
+ type ConfigureDiscord = typeof ConfigureDiscord.infer;
2
14
  declare const Reporter: {
15
+ webhookUrl: string;
3
16
  log: (logObj: LogObject) => void;
4
17
  };
5
18
  export default Reporter;
19
+ export { ConfigureDiscord, Discord };
@@ -2,23 +2,47 @@ import { format } from "../format.js";
2
2
  import { type } from "arktype";
3
3
  import got from "got";
4
4
  const Discord = type({
5
- discord: type("boolean").narrow((v) => v),
5
+ discord: "true",
6
6
  color: "string",
7
7
  title: "string",
8
- webhookUrl: "string",
8
+ "webhookUrl?": "string.url",
9
+ });
10
+ const ConfigureDiscord = type({
11
+ discord: "true",
12
+ setWebhookUrl: "string.url",
9
13
  });
10
14
  const NullDiscord = { discord: false };
11
15
  const Reporter = {
16
+ webhookUrl: "",
12
17
  log: (logObj) => {
18
+ if (configureDiscord(logObj.args)) {
19
+ logObj.args = ["Set Discord webhook URL"];
20
+ return;
21
+ }
13
22
  const [discord, args] = getDiscord(logObj.args);
14
23
  if (!discord.discord)
15
24
  return;
16
- sendToDiscord(format(args), discord);
25
+ const webhookUrl = discord.webhookUrl || Reporter.webhookUrl;
26
+ if (!webhookUrl) {
27
+ console.error("Discord webhook URL is missing");
28
+ return;
29
+ }
30
+ sendToDiscord(format(args), discord, webhookUrl);
17
31
  // Filter Discord data before the other reporters
18
32
  // process the message
19
33
  logObj.args = args;
20
34
  },
21
35
  };
36
+ const configureDiscord = (args) => {
37
+ for (const arg of args) {
38
+ const config = ConfigureDiscord(arg);
39
+ if (!(config instanceof type.errors)) {
40
+ Reporter.webhookUrl = config.setWebhookUrl;
41
+ return true;
42
+ }
43
+ }
44
+ return false;
45
+ };
22
46
  const getDiscord = (args) => {
23
47
  for (let i = 0; i < args.length; i++) {
24
48
  const discord = Discord(args[i]);
@@ -28,7 +52,7 @@ const getDiscord = (args) => {
28
52
  }
29
53
  return [NullDiscord, args];
30
54
  };
31
- const sendToDiscord = async (description, options) => {
55
+ const sendToDiscord = async (description, options, webhookUrl) => {
32
56
  const data = {
33
57
  json: {
34
58
  embeds: [
@@ -42,10 +66,11 @@ const sendToDiscord = async (description, options) => {
42
66
  retry: { limit: 5 },
43
67
  };
44
68
  try {
45
- await got.post(options.webhookUrl, data);
69
+ await got.post(webhookUrl, data);
46
70
  }
47
71
  catch (error) {
48
72
  console.error("Unable to send message to Discord", error);
49
73
  }
50
74
  };
51
75
  export default Reporter;
76
+ export { ConfigureDiscord, Discord };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "mynth-logger",
3
- "version": "2.0.9",
3
+ "version": "2.0.11",
4
4
  "description": "Package to format logs for mynth microservices.",
5
5
  "main": "dist/src/index.js",
6
6
  "typings": "dist/src/index.d.ts",
@@ -22,7 +22,7 @@
22
22
  "url": "git@github.com-mynth:MynthAI/mynth-logger.git"
23
23
  },
24
24
  "peerDependencies": {
25
- "arktype": "^2.0.0-rc.7"
25
+ "arktype": "^2.0.4"
26
26
  },
27
27
  "dependencies": {
28
28
  "@ungap/structured-clone": "^1.2.0",
package/src/discord.ts ADDED
@@ -0,0 +1,46 @@
1
+ import { ConfigureDiscord, Discord } from "./reporters/discord.js";
2
+ import { type } from "arktype";
3
+
4
+ enum color {
5
+ green = "2404635",
6
+ red = "11606811",
7
+ yellow = "11644443",
8
+ }
9
+ const green = color.green;
10
+ const red = color.red;
11
+ const yellow = color.yellow;
12
+
13
+ const log = (
14
+ level: "debug" | "info" | "log" | "error",
15
+ color: color | string,
16
+ title: string,
17
+ message: string
18
+ ) => {
19
+ const settings: Discord = {
20
+ color,
21
+ discord: true,
22
+ title,
23
+ };
24
+ console[level](message, settings);
25
+ };
26
+
27
+ const discord = {
28
+ configure: (webhookUrl: string) => {
29
+ type("string.url").assert(webhookUrl);
30
+ const settings: ConfigureDiscord = {
31
+ discord: true,
32
+ setWebhookUrl: webhookUrl,
33
+ };
34
+ console.debug(settings);
35
+ },
36
+ debug: (title: string, message: string, color: color | string = yellow) =>
37
+ log("debug", color, title, message),
38
+ error: (title: string, message: string, color: color | string = red) =>
39
+ log("error", color, title, message),
40
+ info: (title: string, message: string, color: color | string = green) =>
41
+ log("info", color, title, message),
42
+ log: (title: string, message: string, color: color | string = green) =>
43
+ log("log", color, title, message),
44
+ };
45
+
46
+ export { color, discord };
package/src/index.ts CHANGED
@@ -1 +1,2 @@
1
1
  export * from "./logging.js";
2
+ export * from "./discord.js";
@@ -4,24 +4,43 @@ import { type } from "arktype";
4
4
  import got from "got";
5
5
 
6
6
  const Discord = type({
7
- discord: type("boolean").narrow((v) => v),
7
+ discord: "true",
8
8
  color: "string",
9
9
  title: "string",
10
- webhookUrl: "string",
10
+ "webhookUrl?": "string.url",
11
+ });
12
+
13
+ const ConfigureDiscord = type({
14
+ discord: "true",
15
+ setWebhookUrl: "string.url",
11
16
  });
12
17
 
13
18
  type Discord = typeof Discord.infer;
19
+ type ConfigureDiscord = typeof ConfigureDiscord.infer;
14
20
 
15
21
  const NullDiscord = { discord: false } as const;
16
22
 
17
23
  type NullDiscord = typeof NullDiscord;
18
24
 
19
25
  const Reporter = {
26
+ webhookUrl: "",
20
27
  log: (logObj: LogObject) => {
28
+ if (configureDiscord(logObj.args)) {
29
+ logObj.args = ["Set Discord webhook URL"];
30
+ return;
31
+ }
32
+
21
33
  const [discord, args] = getDiscord(logObj.args);
22
34
  if (!discord.discord) return;
23
35
 
24
- sendToDiscord(format(args), discord);
36
+ const webhookUrl = discord.webhookUrl || Reporter.webhookUrl;
37
+
38
+ if (!webhookUrl) {
39
+ console.error("Discord webhook URL is missing");
40
+ return;
41
+ }
42
+
43
+ sendToDiscord(format(args), discord, webhookUrl);
25
44
 
26
45
  // Filter Discord data before the other reporters
27
46
  // process the message
@@ -29,6 +48,19 @@ const Reporter = {
29
48
  },
30
49
  };
31
50
 
51
+ const configureDiscord = (args: unknown[]): boolean => {
52
+ for (const arg of args) {
53
+ const config = ConfigureDiscord(arg);
54
+
55
+ if (!(config instanceof type.errors)) {
56
+ Reporter.webhookUrl = config.setWebhookUrl;
57
+ return true;
58
+ }
59
+ }
60
+
61
+ return false;
62
+ };
63
+
32
64
  const getDiscord = (args: unknown[]): [Discord | NullDiscord, unknown[]] => {
33
65
  for (let i = 0; i < args.length; i++) {
34
66
  const discord = Discord(args[i]);
@@ -40,7 +72,11 @@ const getDiscord = (args: unknown[]): [Discord | NullDiscord, unknown[]] => {
40
72
  return [NullDiscord, args];
41
73
  };
42
74
 
43
- const sendToDiscord = async (description: string, options: Discord) => {
75
+ const sendToDiscord = async (
76
+ description: string,
77
+ options: Discord,
78
+ webhookUrl: string
79
+ ) => {
44
80
  const data = {
45
81
  json: {
46
82
  embeds: [
@@ -55,10 +91,11 @@ const sendToDiscord = async (description: string, options: Discord) => {
55
91
  };
56
92
 
57
93
  try {
58
- await got.post(options.webhookUrl, data);
94
+ await got.post(webhookUrl, data);
59
95
  } catch (error) {
60
96
  console.error("Unable to send message to Discord", error);
61
97
  }
62
98
  };
63
99
 
64
100
  export default Reporter;
101
+ export { ConfigureDiscord, Discord };
package/src/index.d.ts DELETED
@@ -1,3 +0,0 @@
1
- declare function setupLogging(): void;
2
-
3
- export { setupLogging };