@ttoss/logger 0.4.0 → 0.5.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 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('Hello! This is a notification for Discord.');
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(message)`
67
+ ### `notify(notification)`
63
68
 
64
69
  Sends a notification to the configured services.
65
70
 
66
71
  - **Parameters**:
67
- - `message` (string): The message to send.
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('Application started successfully!');
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
@@ -6,22 +6,38 @@ var configureLogger = params => {
6
6
  setup = params;
7
7
  };
8
8
  var sendNotificationToDiscord = async ({
9
- message,
10
- url
9
+ notification,
10
+ url,
11
+ project
11
12
  }) => {
13
+ const embedMessage = {
14
+ embeds: [{
15
+ title: notification.title || `${notification.type.toUpperCase()} from ${project}`,
16
+ description: notification.message,
17
+ color: notification.type === "error" ? 16711680 : notification.type === "warn" ? 16776960 : 65280,
18
+ footer: {
19
+ text: `Project: ${project}`
20
+ }
21
+ }]
22
+ };
23
+ const body = JSON.stringify(embedMessage);
12
24
  await fetch(url, {
13
25
  method: "POST",
14
26
  headers: {
15
27
  "Content-Type": "application/json"
16
28
  },
17
- body: JSON.stringify(message)
29
+ body
18
30
  });
19
31
  };
20
- var notify = async message => {
32
+ var notify = async notification => {
33
+ if (!setup?.project) {
34
+ return;
35
+ }
21
36
  if (setup?.discordWebhookUrl) {
22
37
  await sendNotificationToDiscord({
23
- message,
24
- url: setup.discordWebhookUrl
38
+ notification,
39
+ url: setup.discordWebhookUrl,
40
+ project: setup.project
25
41
  });
26
42
  }
27
43
  };
package/dist/index.d.mts CHANGED
@@ -1,12 +1,20 @@
1
- type Setup = {
1
+ type NotificationType = 'info' | 'warn' | 'error';
2
+ type NotificationMessage = {
3
+ type: NotificationType;
4
+ title?: string;
5
+ message: string;
6
+ };
7
+ type Configuration = {
2
8
  discordWebhookUrl?: string;
9
+ project?: string;
3
10
  };
4
- declare const configureLogger: (params: Setup) => void;
5
- declare const sendNotificationToDiscord: ({ message, url, }: {
6
- message: string;
11
+ declare const configureLogger: (params: Configuration) => void;
12
+ declare const sendNotificationToDiscord: ({ notification, url, project, }: {
13
+ notification: NotificationMessage;
7
14
  url: string;
15
+ project: string;
8
16
  }) => Promise<void>;
9
- declare const notify: (message: string) => Promise<void>;
17
+ declare const notify: (notification: NotificationMessage) => Promise<void>;
10
18
  declare const log: {
11
19
  warn: {
12
20
  (...data: any[]): void;
package/dist/index.d.ts CHANGED
@@ -1,12 +1,20 @@
1
- type Setup = {
1
+ type NotificationType = 'info' | 'warn' | 'error';
2
+ type NotificationMessage = {
3
+ type: NotificationType;
4
+ title?: string;
5
+ message: string;
6
+ };
7
+ type Configuration = {
2
8
  discordWebhookUrl?: string;
9
+ project?: string;
3
10
  };
4
- declare const configureLogger: (params: Setup) => void;
5
- declare const sendNotificationToDiscord: ({ message, url, }: {
6
- message: string;
11
+ declare const configureLogger: (params: Configuration) => void;
12
+ declare const sendNotificationToDiscord: ({ notification, url, project, }: {
13
+ notification: NotificationMessage;
7
14
  url: string;
15
+ project: string;
8
16
  }) => Promise<void>;
9
- declare const notify: (message: string) => Promise<void>;
17
+ declare const notify: (notification: NotificationMessage) => Promise<void>;
10
18
  declare const log: {
11
19
  warn: {
12
20
  (...data: any[]): void;
package/dist/index.js CHANGED
@@ -38,22 +38,38 @@ var configureLogger = params => {
38
38
  setup = params;
39
39
  };
40
40
  var sendNotificationToDiscord = async ({
41
- message,
42
- url
41
+ notification,
42
+ url,
43
+ project
43
44
  }) => {
45
+ const embedMessage = {
46
+ embeds: [{
47
+ title: notification.title || `${notification.type.toUpperCase()} from ${project}`,
48
+ description: notification.message,
49
+ color: notification.type === "error" ? 16711680 : notification.type === "warn" ? 16776960 : 65280,
50
+ footer: {
51
+ text: `Project: ${project}`
52
+ }
53
+ }]
54
+ };
55
+ const body = JSON.stringify(embedMessage);
44
56
  await fetch(url, {
45
57
  method: "POST",
46
58
  headers: {
47
59
  "Content-Type": "application/json"
48
60
  },
49
- body: JSON.stringify(message)
61
+ body
50
62
  });
51
63
  };
52
- var notify = async message => {
64
+ var notify = async notification => {
65
+ if (!setup?.project) {
66
+ return;
67
+ }
53
68
  if (setup?.discordWebhookUrl) {
54
69
  await sendNotificationToDiscord({
55
- message,
56
- url: setup.discordWebhookUrl
70
+ notification,
71
+ url: setup.discordWebhookUrl,
72
+ project: setup.project
57
73
  });
58
74
  }
59
75
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ttoss/logger",
3
- "version": "0.4.0",
3
+ "version": "0.5.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": [],