@ttoss/logger 0.4.0 → 0.6.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/README.md +15 -4
- package/dist/esm/index.js +48 -11
- package/dist/index.d.mts +24 -10
- package/dist/index.d.ts +24 -10
- package/dist/index.js +49 -10
- package/package.json +3 -2
package/README.md
CHANGED
|
@@ -23,6 +23,7 @@ import { configureLogger } from '@ttoss/logger';
|
|
|
23
23
|
|
|
24
24
|
configureLogger({
|
|
25
25
|
discordWebhookUrl: 'https://discord.com/api/webhooks/your-webhook-here',
|
|
26
|
+
project: 'My project',
|
|
26
27
|
});
|
|
27
28
|
```
|
|
28
29
|
|
|
@@ -33,7 +34,10 @@ Use the `notify` method to send messages to the configured services:
|
|
|
33
34
|
```ts
|
|
34
35
|
import { notify } from '@ttoss/logger';
|
|
35
36
|
|
|
36
|
-
await notify(
|
|
37
|
+
await notify({
|
|
38
|
+
type: 'error',
|
|
39
|
+
message: 'Hello! This is a notification for Discord.',
|
|
40
|
+
});
|
|
37
41
|
```
|
|
38
42
|
|
|
39
43
|
### Local Logging
|
|
@@ -58,13 +62,17 @@ Configures the logger with notification sending options.
|
|
|
58
62
|
|
|
59
63
|
- **Parameters**:
|
|
60
64
|
- `params.discordWebhookUrl` (string): The Discord webhook URL.
|
|
65
|
+
- `params.project` (string): The project identifier to prefix notifications.
|
|
61
66
|
|
|
62
|
-
### `notify(
|
|
67
|
+
### `notify(notification)`
|
|
63
68
|
|
|
64
69
|
Sends a notification to the configured services.
|
|
65
70
|
|
|
66
71
|
- **Parameters**:
|
|
67
|
-
- `
|
|
72
|
+
- `notification` (object): The notification to send, with the following properties:
|
|
73
|
+
- `type` ('info' | 'warn' | 'error'): The type of the notification.
|
|
74
|
+
- `title` (string, optional): The title of the notification.
|
|
75
|
+
- `message` (string): The main message content.
|
|
68
76
|
- **Returns**: A Promise that resolves when the sending is complete.
|
|
69
77
|
|
|
70
78
|
### `log`
|
|
@@ -88,7 +96,10 @@ configureLogger({
|
|
|
88
96
|
|
|
89
97
|
// Send a notification
|
|
90
98
|
|
|
91
|
-
await notify(
|
|
99
|
+
await notify({
|
|
100
|
+
type: 'info',
|
|
101
|
+
message: 'Application started successfully!',
|
|
102
|
+
});
|
|
92
103
|
|
|
93
104
|
// Local logs
|
|
94
105
|
|
package/dist/esm/index.js
CHANGED
|
@@ -1,33 +1,70 @@
|
|
|
1
1
|
/** Powered by @ttoss/config. https://ttoss.dev/docs/modules/packages/config/ */
|
|
2
2
|
|
|
3
3
|
// src/index.ts
|
|
4
|
+
var log = {
|
|
5
|
+
warn: console.warn,
|
|
6
|
+
error: console.error,
|
|
7
|
+
info: console.info
|
|
8
|
+
};
|
|
4
9
|
var setup = null;
|
|
5
10
|
var configureLogger = params => {
|
|
6
11
|
setup = params;
|
|
7
12
|
};
|
|
8
13
|
var sendNotificationToDiscord = async ({
|
|
9
|
-
|
|
10
|
-
url
|
|
14
|
+
notification,
|
|
15
|
+
url,
|
|
16
|
+
project
|
|
11
17
|
}) => {
|
|
18
|
+
const embedMessage = {
|
|
19
|
+
embeds: [{
|
|
20
|
+
title: notification.title || `${notification.type.toUpperCase()} from ${project}`,
|
|
21
|
+
description: notification.message,
|
|
22
|
+
color: notification.type === "error" ? 16711680 : notification.type === "warn" ? 16776960 : 65280,
|
|
23
|
+
footer: {
|
|
24
|
+
text: `Project: ${project}`
|
|
25
|
+
}
|
|
26
|
+
}]
|
|
27
|
+
};
|
|
28
|
+
const body = JSON.stringify(embedMessage);
|
|
12
29
|
await fetch(url, {
|
|
13
30
|
method: "POST",
|
|
14
31
|
headers: {
|
|
15
32
|
"Content-Type": "application/json"
|
|
16
33
|
},
|
|
17
|
-
body
|
|
34
|
+
body
|
|
18
35
|
});
|
|
19
36
|
};
|
|
20
|
-
var notify = async
|
|
37
|
+
var notify = async notification => {
|
|
38
|
+
if (notification.log) {
|
|
39
|
+
const message = [notification.title, notification.message].join(": ");
|
|
40
|
+
log[notification.type](message);
|
|
41
|
+
}
|
|
42
|
+
if (!setup?.project) {
|
|
43
|
+
return;
|
|
44
|
+
}
|
|
21
45
|
if (setup?.discordWebhookUrl) {
|
|
22
46
|
await sendNotificationToDiscord({
|
|
23
|
-
|
|
24
|
-
url: setup.discordWebhookUrl
|
|
47
|
+
notification,
|
|
48
|
+
url: setup.discordWebhookUrl,
|
|
49
|
+
project: setup.project
|
|
25
50
|
});
|
|
26
51
|
}
|
|
27
52
|
};
|
|
28
|
-
var
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
53
|
+
var getErrorMessage = error => {
|
|
54
|
+
if (error instanceof Error) {
|
|
55
|
+
return error.message;
|
|
56
|
+
}
|
|
57
|
+
if (typeof error === "string") {
|
|
58
|
+
return error;
|
|
59
|
+
}
|
|
60
|
+
return "Unknown error:" + JSON.stringify(error);
|
|
61
|
+
};
|
|
62
|
+
var notifyError = async notification => {
|
|
63
|
+
return notify({
|
|
64
|
+
type: "error",
|
|
65
|
+
message: getErrorMessage(notification.error),
|
|
66
|
+
title: notification.title,
|
|
67
|
+
log: notification.log
|
|
68
|
+
});
|
|
32
69
|
};
|
|
33
|
-
export { configureLogger, log, notify, sendNotificationToDiscord };
|
|
70
|
+
export { configureLogger, log, notify, notifyError, sendNotificationToDiscord };
|
package/dist/index.d.mts
CHANGED
|
@@ -1,12 +1,4 @@
|
|
|
1
|
-
type
|
|
2
|
-
discordWebhookUrl?: string;
|
|
3
|
-
};
|
|
4
|
-
declare const configureLogger: (params: Setup) => void;
|
|
5
|
-
declare const sendNotificationToDiscord: ({ message, url, }: {
|
|
6
|
-
message: string;
|
|
7
|
-
url: string;
|
|
8
|
-
}) => Promise<void>;
|
|
9
|
-
declare const notify: (message: string) => Promise<void>;
|
|
1
|
+
type NotificationType = 'info' | 'warn' | 'error';
|
|
10
2
|
declare const log: {
|
|
11
3
|
warn: {
|
|
12
4
|
(...data: any[]): void;
|
|
@@ -21,5 +13,27 @@ declare const log: {
|
|
|
21
13
|
(message?: any, ...optionalParams: any[]): void;
|
|
22
14
|
};
|
|
23
15
|
};
|
|
16
|
+
type NotificationMessage = {
|
|
17
|
+
type: NotificationType;
|
|
18
|
+
title?: string;
|
|
19
|
+
message: string;
|
|
20
|
+
log?: boolean;
|
|
21
|
+
};
|
|
22
|
+
type Configuration = {
|
|
23
|
+
discordWebhookUrl?: string;
|
|
24
|
+
project?: string;
|
|
25
|
+
};
|
|
26
|
+
declare const configureLogger: (params: Configuration) => void;
|
|
27
|
+
declare const sendNotificationToDiscord: ({ notification, url, project, }: {
|
|
28
|
+
notification: NotificationMessage;
|
|
29
|
+
url: string;
|
|
30
|
+
project: string;
|
|
31
|
+
}) => Promise<void>;
|
|
32
|
+
declare const notify: (notification: NotificationMessage) => Promise<void>;
|
|
33
|
+
declare const notifyError: (notification: {
|
|
34
|
+
error: unknown;
|
|
35
|
+
title?: string;
|
|
36
|
+
log?: boolean;
|
|
37
|
+
}) => Promise<void>;
|
|
24
38
|
|
|
25
|
-
export { configureLogger, log, notify, sendNotificationToDiscord };
|
|
39
|
+
export { configureLogger, log, notify, notifyError, sendNotificationToDiscord };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,12 +1,4 @@
|
|
|
1
|
-
type
|
|
2
|
-
discordWebhookUrl?: string;
|
|
3
|
-
};
|
|
4
|
-
declare const configureLogger: (params: Setup) => void;
|
|
5
|
-
declare const sendNotificationToDiscord: ({ message, url, }: {
|
|
6
|
-
message: string;
|
|
7
|
-
url: string;
|
|
8
|
-
}) => Promise<void>;
|
|
9
|
-
declare const notify: (message: string) => Promise<void>;
|
|
1
|
+
type NotificationType = 'info' | 'warn' | 'error';
|
|
10
2
|
declare const log: {
|
|
11
3
|
warn: {
|
|
12
4
|
(...data: any[]): void;
|
|
@@ -21,5 +13,27 @@ declare const log: {
|
|
|
21
13
|
(message?: any, ...optionalParams: any[]): void;
|
|
22
14
|
};
|
|
23
15
|
};
|
|
16
|
+
type NotificationMessage = {
|
|
17
|
+
type: NotificationType;
|
|
18
|
+
title?: string;
|
|
19
|
+
message: string;
|
|
20
|
+
log?: boolean;
|
|
21
|
+
};
|
|
22
|
+
type Configuration = {
|
|
23
|
+
discordWebhookUrl?: string;
|
|
24
|
+
project?: string;
|
|
25
|
+
};
|
|
26
|
+
declare const configureLogger: (params: Configuration) => void;
|
|
27
|
+
declare const sendNotificationToDiscord: ({ notification, url, project, }: {
|
|
28
|
+
notification: NotificationMessage;
|
|
29
|
+
url: string;
|
|
30
|
+
project: string;
|
|
31
|
+
}) => Promise<void>;
|
|
32
|
+
declare const notify: (notification: NotificationMessage) => Promise<void>;
|
|
33
|
+
declare const notifyError: (notification: {
|
|
34
|
+
error: unknown;
|
|
35
|
+
title?: string;
|
|
36
|
+
log?: boolean;
|
|
37
|
+
}) => Promise<void>;
|
|
24
38
|
|
|
25
|
-
export { configureLogger, log, notify, sendNotificationToDiscord };
|
|
39
|
+
export { configureLogger, log, notify, notifyError, sendNotificationToDiscord };
|
package/dist/index.js
CHANGED
|
@@ -30,42 +30,81 @@ __export(index_exports, {
|
|
|
30
30
|
configureLogger: () => configureLogger,
|
|
31
31
|
log: () => log,
|
|
32
32
|
notify: () => notify,
|
|
33
|
+
notifyError: () => notifyError,
|
|
33
34
|
sendNotificationToDiscord: () => sendNotificationToDiscord
|
|
34
35
|
});
|
|
35
36
|
module.exports = __toCommonJS(index_exports);
|
|
37
|
+
var log = {
|
|
38
|
+
warn: console.warn,
|
|
39
|
+
error: console.error,
|
|
40
|
+
info: console.info
|
|
41
|
+
};
|
|
36
42
|
var setup = null;
|
|
37
43
|
var configureLogger = params => {
|
|
38
44
|
setup = params;
|
|
39
45
|
};
|
|
40
46
|
var sendNotificationToDiscord = async ({
|
|
41
|
-
|
|
42
|
-
url
|
|
47
|
+
notification,
|
|
48
|
+
url,
|
|
49
|
+
project
|
|
43
50
|
}) => {
|
|
51
|
+
const embedMessage = {
|
|
52
|
+
embeds: [{
|
|
53
|
+
title: notification.title || `${notification.type.toUpperCase()} from ${project}`,
|
|
54
|
+
description: notification.message,
|
|
55
|
+
color: notification.type === "error" ? 16711680 : notification.type === "warn" ? 16776960 : 65280,
|
|
56
|
+
footer: {
|
|
57
|
+
text: `Project: ${project}`
|
|
58
|
+
}
|
|
59
|
+
}]
|
|
60
|
+
};
|
|
61
|
+
const body = JSON.stringify(embedMessage);
|
|
44
62
|
await fetch(url, {
|
|
45
63
|
method: "POST",
|
|
46
64
|
headers: {
|
|
47
65
|
"Content-Type": "application/json"
|
|
48
66
|
},
|
|
49
|
-
body
|
|
67
|
+
body
|
|
50
68
|
});
|
|
51
69
|
};
|
|
52
|
-
var notify = async
|
|
70
|
+
var notify = async notification => {
|
|
71
|
+
if (notification.log) {
|
|
72
|
+
const message = [notification.title, notification.message].join(": ");
|
|
73
|
+
log[notification.type](message);
|
|
74
|
+
}
|
|
75
|
+
if (!setup?.project) {
|
|
76
|
+
return;
|
|
77
|
+
}
|
|
53
78
|
if (setup?.discordWebhookUrl) {
|
|
54
79
|
await sendNotificationToDiscord({
|
|
55
|
-
|
|
56
|
-
url: setup.discordWebhookUrl
|
|
80
|
+
notification,
|
|
81
|
+
url: setup.discordWebhookUrl,
|
|
82
|
+
project: setup.project
|
|
57
83
|
});
|
|
58
84
|
}
|
|
59
85
|
};
|
|
60
|
-
var
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
86
|
+
var getErrorMessage = error => {
|
|
87
|
+
if (error instanceof Error) {
|
|
88
|
+
return error.message;
|
|
89
|
+
}
|
|
90
|
+
if (typeof error === "string") {
|
|
91
|
+
return error;
|
|
92
|
+
}
|
|
93
|
+
return "Unknown error:" + JSON.stringify(error);
|
|
94
|
+
};
|
|
95
|
+
var notifyError = async notification => {
|
|
96
|
+
return notify({
|
|
97
|
+
type: "error",
|
|
98
|
+
message: getErrorMessage(notification.error),
|
|
99
|
+
title: notification.title,
|
|
100
|
+
log: notification.log
|
|
101
|
+
});
|
|
64
102
|
};
|
|
65
103
|
// Annotate the CommonJS export names for ESM import in node:
|
|
66
104
|
0 && (module.exports = {
|
|
67
105
|
configureLogger,
|
|
68
106
|
log,
|
|
69
107
|
notify,
|
|
108
|
+
notifyError,
|
|
70
109
|
sendNotificationToDiscord
|
|
71
110
|
});
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ttoss/logger",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.6.0",
|
|
4
4
|
"description": "A simple module to configure and send notifications to services like Discord from your applications.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"contributors": [],
|
|
@@ -23,7 +23,8 @@
|
|
|
23
23
|
"devDependencies": {
|
|
24
24
|
"jest": "^29.7.0",
|
|
25
25
|
"tsup": "^8.3.5",
|
|
26
|
-
"@ttoss/config": "^1.35.2"
|
|
26
|
+
"@ttoss/config": "^1.35.2",
|
|
27
|
+
"@ttoss/test-utils": "^2.1.22"
|
|
27
28
|
},
|
|
28
29
|
"keywords": [],
|
|
29
30
|
"publishConfig": {
|