ntlogger 2.5.1 → 2.5.2

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/index.js CHANGED
@@ -12,29 +12,32 @@
12
12
 
13
13
  /**
14
14
  * Change Log:
15
+ *
16
+ * v2.5.2 - 08/11/2024:
17
+ * [BUG] Fixed an issue where the /plugins/lib folder was not being pushed to npmjs.com.
15
18
  *
16
19
  * v2.5.1 - 08/10/2024:
17
- * [Bug] Fixed a bug where Jest plugin was not working due to a missing config parameter. This rule was not originally enforced until v2.5.0.
20
+ * [BUG] Fixed a bug where Jest plugin was not working due to a missing config parameter. This rule was not originally enforced until v2.5.0.
18
21
  *
19
22
  * v2.5.0 - 08/10/2024:
20
23
  * [NOTE] Syslog plugin can send logs using TLS but it is not tested. Avoid sensitive data.
21
- * [Feature] Added a plugin for Discord webhook integration, allowing log messages to be sent directly to a specified Discord channel.
22
- * [Feature] Added a plugin for Syslog server integration, enabling log messages to be sent to a Syslog server using UDP, TCP, or TLS protocols.
23
- * [Feature] Implemented clean signal handling for graceful shutdowns (SIGINT, SIGTERM).
24
+ * [FEATURE] Added a plugin for Discord webhook integration, allowing log messages to be sent directly to a specified Discord channel.
25
+ * [FEATURE] Added a plugin for Syslog server integration, enabling log messages to be sent to a Syslog server using UDP, TCP, or TLS protocols.
26
+ * [FEATURE] Implemented clean signal handling for graceful shutdowns (SIGINT, SIGTERM).
24
27
  *
25
- * [Update] @sentry/node ^8.17.0 --> ^8.25.0
26
- * [Update] mysql2 ^3.10.2 --> ^3.11.0
27
- * [Update] winston ^3.13.1 --> ^3.14.1
28
- * [Update] @sentry/profiling-node ^8.17.0 --> ^8.25.0
29
- * [Update] jest ^29.0.0 --> ^29.7.0
28
+ * [UPDATED] @sentry/node ^8.17.0 --> ^8.25.0
29
+ * [UPDATED] mysql2 ^3.10.2 --> ^3.11.0
30
+ * [UPDATED] winston ^3.13.1 --> ^3.14.1
31
+ * [UPDATED] @sentry/profiling-node ^8.17.0 --> ^8.25.0
32
+ * [UPDATED] jest ^29.0.0 --> ^29.7.0
30
33
  *
31
34
  * v2.4.0 - 08/01/2024:
32
35
  * [NOTE] Damn its been 4 months.. kinda. forgot to log last changes
33
36
  * [CRITICAL] Fixed `includes` in package.json which prevented plugins folder from being pushed to npmjs.com
34
37
  * [QA] Added test cases using jest
35
- * [Feature] Added session ID to logger meta, try `console.log(log.defaultMeta.ID)`
36
- * [Feature] Created the plugin jest. Added a custom transport for in memory logging and testing
37
- * [Feature] Created the config var `skipCache` to allow you to create a new logger instance within the same file. (Usual behavior is to return the same instance within the same file)
38
+ * [FEATURE] Added session ID to logger meta, try `console.log(log.defaultMeta.ID)`
39
+ * [FEATURE] Created the plugin jest. Added a custom transport for in memory logging and testing
40
+ * [FEATURE] Created the config var `skipCache` to allow you to create a new logger instance within the same file. (Usual behavior is to return the same instance within the same file)
38
41
  *
39
42
  * v2.2.3 - 04/01/2024:
40
43
  * Reduced GitHub Actions to only NPM publish
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ntlogger",
3
- "version": "2.5.1",
3
+ "version": "2.5.2",
4
4
  "repository": {
5
5
  "type": "git",
6
6
  "url": "git+https://github.com/NightSquawk/NightTimeLogger.git"
@@ -25,7 +25,7 @@
25
25
  "files": [
26
26
  "index.js",
27
27
  "lib/*.js",
28
- "plugins/*.js"
28
+ "plugins/**/*"
29
29
  ],
30
30
  "license": "GPL-3.0",
31
31
  "dependencies": {
@@ -0,0 +1,163 @@
1
+ # NightTimeLogger Plugins
2
+
3
+ NightTimeLogger is a powerful and flexible logging utility with various plugins to extend its functionality. This README provides an overview of the available plugins and instructions on how to set them up.
4
+
5
+ ![Discord Plugin Output](https://github.com/NightSquawk/NightTimeLogger/blob/main/images/plugins/pluginDiscordOutput.png)
6
+
7
+ ## Available Plugins
8
+
9
+ ### 1. Discord
10
+ **Description:** Sends logs to a Discord webhook, allowing you to receive log messages directly in a specified Discord channel.
11
+
12
+ **Setup:**
13
+ ```javascript
14
+ {
15
+ name: 'Discord',
16
+ config: {
17
+ webhookUrl: process.env.DISCORD_WEBHOOK_URL || 'https://ptb.discord.com/api/webhooks/1271901134663192658/FmzqFlzbIJ7NunR_4rpBkPLb4QQ5aHSNEbpT311su-QM3ZlGDI6sFuC4Ff0MF_TFrf3k',
18
+ avatarUrl: process.env.DISCORD_AVATAR_URL || 'https://pbs.twimg.com/profile_images/997535493624508416/V7Ed1k2o_400x400.jpg',
19
+ username: process.env.DISCORD_USERNAME || 'NTLogger - Info',
20
+ level: "info",
21
+ strict: true,
22
+ },
23
+ }
24
+ ```
25
+
26
+ ### 2. Sentry
27
+ Description: Initializes the Sentry SDK for error tracking and defines a custom Winston transport for capturing and sending error logs to Sentry.
28
+
29
+ Setup:
30
+
31
+ ``` javascript
32
+ {
33
+ name: 'Sentry',
34
+ config: {
35
+ dsn: process.env.SENTRY_DSN || null,
36
+ release: process.env.SENTRY_RELEASE || null,
37
+ tracesSampleRate: 1.0,
38
+ profilesSampleRate: 1.0,
39
+ environment: process.env.NODE_ENV || 'development',
40
+ debug: false,
41
+ attachStacktrace: true,
42
+ integrations: [
43
+ nodeProfilingIntegration()
44
+ ],
45
+ serverName: null,
46
+ maxBreadcrumbs: 100,
47
+ autoSessionTracking: true,
48
+ sessionTracking: {},
49
+ tracesSampler: null
50
+ }
51
+ }
52
+ ```
53
+
54
+ ### . MySQL
55
+ Description: Stores logs in a MySQL database, allowing you to persist log messages and analyze them using SQL queries.
56
+
57
+ Setup:
58
+
59
+ ``` javascript
60
+ {
61
+ name: 'MySQL',
62
+ config: {
63
+ host: process.env.MYSQL_DB_HOST || 'localhost',
64
+ port: process.env.MYSQL_DB_PORT || 3306,
65
+ user: process.env.MYSQL_DB_USER || 'root',
66
+ password: process.env.MYSQL_DB_PASSWORD || '',
67
+ database: process.env.MYSQL_DB_NAME || 'test',
68
+ table: process.env.MYSQL_DB_TABLE || 'logs',
69
+ logLevel: process.env.LOG_LEVEL || 'info',
70
+ },
71
+ }
72
+ ````
73
+ ### 4. Jest
74
+ Description: Stores log messages in memory for testing purposes, particularly useful when running tests with Jest.
75
+
76
+ Setup:
77
+
78
+ ``` javascript
79
+ {
80
+ name: 'Jest',
81
+ }
82
+ ```
83
+
84
+ ### 5. Syslog
85
+ Description: Sends logs to a Syslog server using a custom Syslog client. This allows integration with traditional logging systems that rely on Syslog.
86
+
87
+ Setup:
88
+
89
+ ``` javascript
90
+ {
91
+ name: 'Syslog',
92
+ config: {
93
+ host: process.env.SYSLOG_HOST || 'localhost',
94
+ port: process.env.SYSLOG_PORT || 514,
95
+ protocol: process.env.SYSLOG_PROTOCOL || 'UDP', // Options: 'UDP', 'TCP', 'TLS'
96
+ rfc: process.env.SYSLOG_RFC || 'RFC-5424', // Options: 'RFC-3164', 'RFC-5424'
97
+ facility: process.env.SYSLOG_FACILITY || 1, // Local0
98
+ appName: process.env.SYSLOG_APP_NAME || 'MyApp',
99
+ level: 'info',
100
+ },
101
+ }
102
+ ```
103
+
104
+ ### Example Logger Configuration
105
+ Here is an example configuration for NightTimeLogger using multiple plugins:
106
+
107
+ ``` javascript
108
+ Copy code
109
+ let config = {
110
+ level: 'internal',
111
+ file: false,
112
+ plugins: [
113
+ {
114
+ name: 'Discord',
115
+ config: {
116
+ webhookUrl: process.env.DISCORD_WEBHOOK_URL,
117
+ avatarUrl: process.env.DISCORD_AVATAR_URL,
118
+ username: process.env.DISCORD_USERNAME || 'MyAppLogger',
119
+ level: 'info',
120
+ strict: false,
121
+ },
122
+ },
123
+ {
124
+ name: 'Sentry',
125
+ config: {
126
+ dsn: process.env.SENTRY_DSN,
127
+ release: process.env.SENTRY_RELEASE,
128
+ tracesSampleRate: 1.0,
129
+ environment: process.env.NODE_ENV || 'development',
130
+ },
131
+ },
132
+ {
133
+ name: 'MySQL',
134
+ config: {
135
+ host: process.env.MYSQL_DB_HOST,
136
+ port: process.env.MYSQL_DB_PORT || 3306,
137
+ user: process.env.MYSQL_DB_USER || 'root',
138
+ password: process.env.MYSQL_DB_PASSWORD || '',
139
+ database: process.env.MYSQL_DB_NAME || 'logs',
140
+ table: 'log_entries',
141
+ },
142
+ },
143
+ {
144
+ name: 'Jest',
145
+ },
146
+ {
147
+ name: 'Syslog',
148
+ config: {
149
+ host: process.env.SYSLOG_HOST || 'localhost',
150
+ port: process.env.SYSLOG_PORT || 514,
151
+ protocol: 'UDP',
152
+ facility: 1,
153
+ appName: 'MyAppLogger',
154
+ },
155
+ },
156
+ ],
157
+ };
158
+ ```
159
+
160
+ ### Conclusion
161
+ The NightTimeLogger is highly customizable and supports various plugins for different logging requirements. By leveraging these plugins, you can easily integrate logging into your existing infrastructure, whether it be storing logs in databases, sending them to monitoring tools like Sentry, or receiving alerts directly in Discord.
162
+
163
+ For more details on each plugin, refer to the specific plugin files in the project.
@@ -0,0 +1,112 @@
1
+ /**
2
+ * @file /plugins/lib/syslogClient.js
3
+ * @description Custom Syslog client supporting UDP, TCP, and TLS transports with RFC-3164 and RFC-5424 formats.
4
+ */
5
+
6
+ const net = require('net');
7
+ const tls = require('tls');
8
+ const dgram = require('dgram');
9
+
10
+ class SyslogClient {
11
+ constructor(opts = {}) {
12
+ this.host = opts.host || 'localhost';
13
+ this.port = opts.port || 514;
14
+ this.protocol = opts.protocol || 'UDP'; // 'UDP', 'TCP', 'TLS'
15
+ this.rfc = opts.rfc || 'RFC-5424'; // 'RFC-3164', 'RFC-5424'
16
+ this.facility = opts.facility || 1; // Default to user-level messages
17
+ this.appName = opts.appName || 'NTLogger';
18
+ this.hostname = opts.hostname || require('os').hostname();
19
+ this.transport = null;
20
+
21
+ if (this.protocol === 'UDP') {
22
+ this.transport = dgram.createSocket('udp4');
23
+ } else if (this.protocol === 'TCP') {
24
+ this.transport = new net.Socket();
25
+ } else if (this.protocol === 'TLS') {
26
+ this.transport = new tls.TLSSocket();
27
+ }
28
+ }
29
+
30
+ /**
31
+ * Connects to the Syslog server if using TCP or TLS.
32
+ */
33
+ connect() {
34
+ if (this.protocol === 'TCP' || this.protocol === 'TLS') {
35
+ return new Promise((resolve, reject) => {
36
+ const options = { host: this.host, port: this.port };
37
+ const connectListener = () => resolve();
38
+ const errorListener = (err) => reject(err);
39
+
40
+ if (this.protocol === 'TCP') {
41
+ this.transport.connect(options, connectListener).on('error', errorListener);
42
+ } else if (this.protocol === 'TLS') {
43
+ this.transport.connect(options, connectListener).on('error', errorListener);
44
+ }
45
+ });
46
+ }
47
+ }
48
+
49
+ /**
50
+ * Sends a log message to the Syslog server.
51
+ * @param {string} severity - The severity level of the log.
52
+ * @param {string} message - The log message.
53
+ * @param {object} [meta] - Additional metadata for RFC-5424 structured data.
54
+ */
55
+ send(severity, message, meta = {}) {
56
+ const formattedMessage = this.formatMessage(severity, message, meta);
57
+ if (this.protocol === 'UDP') {
58
+ this.transport.send(Buffer.from(formattedMessage), 0, formattedMessage.length, this.port, this.host);
59
+ } else {
60
+ this.transport.write(formattedMessage + '\n');
61
+ }
62
+ }
63
+
64
+ /**
65
+ * Formats the Syslog message according to the selected RFC standard.
66
+ * @param {string} severity - The severity level of the log.
67
+ * @param {string} message - The log message.
68
+ * @param {object} [meta] - Additional metadata for RFC-5424 structured data.
69
+ * @returns {string} - The formatted Syslog message.
70
+ */
71
+ formatMessage(severity, message, meta) {
72
+ const timestamp = new Date().toISOString();
73
+ const priority = (this.facility * 8) + severity;
74
+
75
+ if (this.rfc === 'RFC-3164') {
76
+ return `<${priority}>${timestamp} ${this.hostname} ${this.appName}: ${message}`;
77
+ } else if (this.rfc === 'RFC-5424') {
78
+ const structuredData = this.formatStructuredData(meta.structuredData || {});
79
+ return `<${priority}>1 ${timestamp} ${this.hostname} ${this.appName} - - ${structuredData} ${message}`;
80
+ }
81
+ }
82
+
83
+ /**
84
+ * Formats the structured data for RFC-5424.
85
+ * @param {object} structuredData - The structured data to include in the log message.
86
+ * @returns {string} - The formatted structured data string.
87
+ */
88
+ formatStructuredData(structuredData) {
89
+ let sdString = '';
90
+ for (const [key, value] of Object.entries(structuredData)) {
91
+ sdString += `[${key}`;
92
+ for (const [param, val] of Object.entries(value)) {
93
+ sdString += ` ${param}="${val}"`;
94
+ }
95
+ sdString += ']';
96
+ }
97
+ return sdString || '-';
98
+ }
99
+
100
+ /**
101
+ * Closes the transport connection.
102
+ */
103
+ close() {
104
+ if (this.transport && (this.protocol === 'TCP' || this.protocol === 'TLS')) {
105
+ this.transport.end();
106
+ } else if (this.protocol === 'UDP') {
107
+ this.transport.close();
108
+ }
109
+ }
110
+ }
111
+
112
+ module.exports = SyslogClient;