@ttoss/logger 0.3.17 → 0.4.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
@@ -1,57 +1,109 @@
1
1
  # @ttoss/logger
2
2
 
3
- Simple environment agnostic logger.
3
+ A simple module to configure and send notifications to services like Discord from your applications.
4
4
 
5
- ## Motivation
5
+ ## Installation
6
6
 
7
- Often, for debugging some piece of code, developers use console.log for it. When the application goes for production, some of these logs still appears, most because they forget to clear these logs.
8
- These package solves this, providing some levels of log that are emitted based on the environment of the application.
7
+ Install the package via pnpm:
9
8
 
10
- ## Log Levels vs Environments
9
+ ```bash
10
+ pnpm add @ttoss/logger
11
+ ```
12
+
13
+ ## Usage
11
14
 
12
- <table>
13
- <thead>
14
- <tr><th>Log Level</th><th>Environment</th></tr>
15
- </thead>
16
- <tbody>
17
- <tr><td>Warn</td><td>Dev-only</td></tr>
18
- <tr><td>Error</td><td>All</td></tr>
19
- <tr><td>Info</td><td>Dev-only</td></tr>
20
- </tbody>
21
- </table>
15
+ The `@ttoss/logger` module allows you to configure a logger and send notifications to external services, such as Discord, with ease. It also provides basic methods for local console logging.
22
16
 
23
- ## How to use
17
+ ### Configuration
24
18
 
25
- Just instantiate the logger, providing the `isDev` value configuration and start to use:
19
+ First, configure the logger with the necessary parameters, such as a Discord webhook URL:
26
20
 
27
21
  ```ts
28
- // createLogger.ts
22
+ import { configureLogger } from '@ttoss/logger';
23
+
24
+ configureLogger({
25
+ discordWebhookUrl: 'https://discord.com/api/webhooks/your-webhook-here',
26
+ });
27
+ ```
29
28
 
30
- import { Logger } from '@ttoss/logger';
31
- import { config } from 'dotenv';
29
+ ### Sending Notifications
32
30
 
33
- config();
31
+ Use the `notify` method to send messages to the configured services:
34
32
 
35
- export const createLogger = Logger(process.env.DEV === 'true');
33
+ ```ts
34
+ import { notify } from '@ttoss/logger';
35
+
36
+ await notify('Hello! This is a notification for Discord.');
37
+ ```
36
38
 
37
- // randomFile.ts
39
+ ### Local Logging
38
40
 
39
- const logger = createLogger('randomFile');
41
+ For console logs, use the `log` object:
40
42
 
41
- logger.warn('This will emit an warn on console');
43
+ ```ts
44
+ import { log } from '@ttoss/logger';
42
45
 
43
- logger.error('This will emit a log of type error on console');
46
+ log.info('Useful information');
44
47
 
45
- loggger.info('This will emit a simple log on console');
48
+ log.warn('Important warning');
49
+
50
+ log.error('Critical error');
46
51
  ```
47
52
 
48
- If you not pass any parameter to `Logger`, it gonna considers to be in dev environment:
53
+ ## API
54
+
55
+ ### `configureLogger(params)`
56
+
57
+ Configures the logger with notification sending options.
58
+
59
+ - **Parameters**:
60
+ - `params.discordWebhookUrl` (string): The Discord webhook URL.
61
+
62
+ ### `notify(message)`
63
+
64
+ Sends a notification to the configured services.
65
+
66
+ - **Parameters**:
67
+ - `message` (string): The message to send.
68
+ - **Returns**: A Promise that resolves when the sending is complete.
69
+
70
+ ### `log`
71
+
72
+ Object with methods for local logging:
73
+
74
+ - `log.warn(message)`: Displays a warning in the console.
75
+ - `log.error(message)`: Displays an error in the console.
76
+ - `log.info(message)`: Displays an info message in the console.
77
+
78
+ ## Complete Example
49
79
 
50
80
  ```ts
51
- // createLogger.ts
81
+ import { configureLogger, notify, log } from '@ttoss/logger';
82
+
83
+ // Configure the logger
84
+
85
+ configureLogger({
86
+ discordWebhookUrl: 'https://discord.com/api/webhooks/your-webhook-here',
87
+ });
88
+
89
+ // Send a notification
52
90
 
53
- import { Logger } from '@ttoss/logger';
91
+ await notify('Application started successfully!');
54
92
 
55
- // In dev environment, gonna log everything
56
- export const createLogger = Logger();
93
+ // Local logs
94
+
95
+ log.info('Starting the server...');
96
+
97
+ log.warn('Low memory.');
98
+
99
+ log.error('Failed to connect to the database.');
57
100
  ```
101
+
102
+ ## Notes
103
+
104
+ - Currently, support is limited to Discord via webhooks, but more services will be added in the future.
105
+ - Ensure the `discordWebhookUrl` is valid to avoid silent failures.
106
+
107
+ ## License
108
+
109
+ [MIT](https://github.com/ttoss/ttoss/blob/main/LICENSE)
package/dist/esm/index.js CHANGED
@@ -1,32 +1,33 @@
1
1
  /** Powered by @ttoss/config. https://ttoss.dev/docs/modules/packages/config/ */
2
2
 
3
3
  // src/index.ts
4
- var {
5
- log,
6
- warn,
7
- error
8
- } = console;
9
- var Logger = isDev => {
10
- const devEnv = isDev !== void 0 ? isDev : true;
11
- return prefix => {
12
- return {
13
- warn: value => {
14
- if (devEnv) {
15
- const now = /* @__PURE__ */new Date();
16
- warn(`[${now}] - ${prefix} - ${value}`);
17
- }
18
- },
19
- info: value => {
20
- if (devEnv) {
21
- const now = /* @__PURE__ */new Date();
22
- log(`[${now}] - ${prefix} - ${value}`);
23
- }
24
- },
25
- error: value => {
26
- const now = /* @__PURE__ */new Date();
27
- error(`[${now}] - ${prefix} - ${value}`);
28
- }
29
- };
30
- };
4
+ var setup = null;
5
+ var configureLogger = params => {
6
+ setup = params;
31
7
  };
32
- export { Logger };
8
+ var sendNotificationToDiscord = async ({
9
+ message,
10
+ url
11
+ }) => {
12
+ await fetch(url, {
13
+ method: "POST",
14
+ headers: {
15
+ "Content-Type": "application/json"
16
+ },
17
+ body: JSON.stringify(message)
18
+ });
19
+ };
20
+ var notify = async message => {
21
+ if (setup?.discordWebhookUrl) {
22
+ await sendNotificationToDiscord({
23
+ message,
24
+ url: setup.discordWebhookUrl
25
+ });
26
+ }
27
+ };
28
+ var log = {
29
+ warn: console.warn,
30
+ error: console.error,
31
+ info: console.info
32
+ };
33
+ export { configureLogger, log, notify, sendNotificationToDiscord };
package/dist/index.d.mts CHANGED
@@ -1,7 +1,25 @@
1
- declare const Logger: (isDev?: boolean) => (prefix: string) => {
2
- warn: (value: string) => void;
3
- info: (value: string) => void;
4
- error: (value: string) => void;
1
+ type Setup = {
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>;
10
+ declare const log: {
11
+ warn: {
12
+ (...data: any[]): void;
13
+ (message?: any, ...optionalParams: any[]): void;
14
+ };
15
+ error: {
16
+ (...data: any[]): void;
17
+ (message?: any, ...optionalParams: any[]): void;
18
+ };
19
+ info: {
20
+ (...data: any[]): void;
21
+ (message?: any, ...optionalParams: any[]): void;
22
+ };
5
23
  };
6
24
 
7
- export { Logger };
25
+ export { configureLogger, log, notify, sendNotificationToDiscord };
package/dist/index.d.ts CHANGED
@@ -1,7 +1,25 @@
1
- declare const Logger: (isDev?: boolean) => (prefix: string) => {
2
- warn: (value: string) => void;
3
- info: (value: string) => void;
4
- error: (value: string) => void;
1
+ type Setup = {
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>;
10
+ declare const log: {
11
+ warn: {
12
+ (...data: any[]): void;
13
+ (message?: any, ...optionalParams: any[]): void;
14
+ };
15
+ error: {
16
+ (...data: any[]): void;
17
+ (message?: any, ...optionalParams: any[]): void;
18
+ };
19
+ info: {
20
+ (...data: any[]): void;
21
+ (message?: any, ...optionalParams: any[]): void;
22
+ };
5
23
  };
6
24
 
7
- export { Logger };
25
+ export { configureLogger, log, notify, sendNotificationToDiscord };
package/dist/index.js CHANGED
@@ -27,38 +27,45 @@ var __toCommonJS = mod => __copyProps(__defProp({}, "__esModule", {
27
27
  // src/index.ts
28
28
  var index_exports = {};
29
29
  __export(index_exports, {
30
- Logger: () => Logger
30
+ configureLogger: () => configureLogger,
31
+ log: () => log,
32
+ notify: () => notify,
33
+ sendNotificationToDiscord: () => sendNotificationToDiscord
31
34
  });
32
35
  module.exports = __toCommonJS(index_exports);
33
- var {
34
- log,
35
- warn,
36
- error
37
- } = console;
38
- var Logger = isDev => {
39
- const devEnv = isDev !== void 0 ? isDev : true;
40
- return prefix => {
41
- return {
42
- warn: value => {
43
- if (devEnv) {
44
- const now = /* @__PURE__ */new Date();
45
- warn(`[${now}] - ${prefix} - ${value}`);
46
- }
47
- },
48
- info: value => {
49
- if (devEnv) {
50
- const now = /* @__PURE__ */new Date();
51
- log(`[${now}] - ${prefix} - ${value}`);
52
- }
53
- },
54
- error: value => {
55
- const now = /* @__PURE__ */new Date();
56
- error(`[${now}] - ${prefix} - ${value}`);
57
- }
58
- };
59
- };
36
+ var setup = null;
37
+ var configureLogger = params => {
38
+ setup = params;
39
+ };
40
+ var sendNotificationToDiscord = async ({
41
+ message,
42
+ url
43
+ }) => {
44
+ await fetch(url, {
45
+ method: "POST",
46
+ headers: {
47
+ "Content-Type": "application/json"
48
+ },
49
+ body: JSON.stringify(message)
50
+ });
51
+ };
52
+ var notify = async message => {
53
+ if (setup?.discordWebhookUrl) {
54
+ await sendNotificationToDiscord({
55
+ message,
56
+ url: setup.discordWebhookUrl
57
+ });
58
+ }
59
+ };
60
+ var log = {
61
+ warn: console.warn,
62
+ error: console.error,
63
+ info: console.info
60
64
  };
61
65
  // Annotate the CommonJS export names for ESM import in node:
62
66
  0 && (module.exports = {
63
- Logger
67
+ configureLogger,
68
+ log,
69
+ notify,
70
+ sendNotificationToDiscord
64
71
  });
package/package.json CHANGED
@@ -1,11 +1,9 @@
1
1
  {
2
2
  "name": "@ttoss/logger",
3
- "version": "0.3.17",
4
- "description": "Simple environment agnostic logger",
3
+ "version": "0.4.0",
4
+ "description": "A simple module to configure and send notifications to services like Discord from your applications.",
5
5
  "license": "MIT",
6
- "contributors": [
7
- "Eron Alves <eron.alves@rocketmail.com>"
8
- ],
6
+ "contributors": [],
9
7
  "repository": {
10
8
  "type": "git",
11
9
  "url": "https://github.com/ttoss/ttoss.git",
@@ -18,12 +16,12 @@
18
16
  "types": "./dist/index.d.ts"
19
17
  }
20
18
  },
21
- "module": "dist/esm/index.js",
22
19
  "files": [
23
20
  "dist"
24
21
  ],
25
22
  "sideEffects": false,
26
23
  "devDependencies": {
24
+ "jest": "^29.7.0",
27
25
  "tsup": "^8.3.5",
28
26
  "@ttoss/config": "^1.35.2"
29
27
  },
@@ -33,6 +31,7 @@
33
31
  "provenance": true
34
32
  },
35
33
  "scripts": {
36
- "build": "tsup"
34
+ "build": "tsup",
35
+ "test": "jest --projects tests/unit"
37
36
  }
38
37
  }