@bsb/observable-winston 9.0.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 ADDED
@@ -0,0 +1,62 @@
1
+ # @bsb/observable-winston
2
+
3
+ Winston observable plugin for BSB - integrate with the popular Winston logging framework.
4
+
5
+ ## Installation
6
+
7
+ ```bash
8
+ npm install @bsb/observable-winston
9
+ ```
10
+
11
+ ## Features
12
+
13
+ - **Winston integration** - Full Winston ecosystem support
14
+ - **Multiple transports** - Console, file, daily-rotate-file
15
+ - **Flexible formatting** - JSON, pretty-print, or simple text
16
+ - **Child loggers** - Per-plugin Winston loggers
17
+ - **Error serialization** - Proper error stack trace handling
18
+
19
+ ## Configuration
20
+
21
+ ```yaml
22
+ plugins:
23
+ observables:
24
+ - plugin: "@bsb/observable-winston"
25
+ enabled: true
26
+ config:
27
+ level: "info"
28
+ transports:
29
+ console:
30
+ enabled: true
31
+ colorize: true
32
+ file:
33
+ enabled: false
34
+ filename: "./logs/application.log"
35
+ maxsize: 10485760
36
+ maxFiles: 5
37
+ tailable: true
38
+ dailyRotate:
39
+ enabled: true
40
+ dirname: "./logs"
41
+ filename: "application-%DATE%.log"
42
+ datePattern: "YYYY-MM-DD"
43
+ maxSize: "20m"
44
+ maxFiles: "14d"
45
+ zippedArchive: true
46
+ format:
47
+ timestamp: true
48
+ json: true
49
+ prettyPrint: false
50
+ ```
51
+
52
+ ### Configuration Options
53
+
54
+ - **level**: Minimum log level ("error", "warn", "info", "debug")
55
+ - **transports.console**: Console output settings
56
+ - **transports.file**: Standard file output
57
+ - **transports.dailyRotate**: Daily rotating files with retention
58
+ - **format**: Timestamp, JSON, and pretty-print options
59
+
60
+ ## License
61
+
62
+ Dual-licensed under AGPL-3.0-only OR Commercial License.
package/lib/index.d.ts ADDED
@@ -0,0 +1,27 @@
1
+ /**
2
+ * BSB (Better-Service-Base) is an event-bus based microservice framework.
3
+ * Copyright (C) 2016 - 2025 BetterCorp (PTY) Ltd
4
+ *
5
+ * This program is free software: you can redistribute it and/or modify
6
+ * it under the terms of the GNU Affero General Public License as published
7
+ * by the Free Software Foundation, either version 3 of the License, or
8
+ * (at your option) any later version.
9
+ *
10
+ * Alternatively, you may obtain a commercial license for this program.
11
+ * The commercial license allows you to use the Program in a closed-source manner,
12
+ * including the right to create derivative works that are not subject to the terms
13
+ * of the AGPL.
14
+ *
15
+ * To obtain a commercial license, please contact the copyright holders at
16
+ * https://www.bettercorp.dev. The terms and conditions of the commercial license
17
+ * will be provided upon request.
18
+ *
19
+ * This program is distributed in the hope that it will be useful,
20
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
21
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22
+ * GNU Affero General Public License for more details.
23
+ *
24
+ * You should have received a copy of the GNU Affero General Public License
25
+ * along with this program. If not, see <https://www.gnu.org/licenses/>.
26
+ */
27
+ export { WinstonConfigSchema, type WinstonConfig, Config, Plugin, } from "./plugins/observable-winston";
package/lib/index.js ADDED
@@ -0,0 +1,33 @@
1
+ "use strict";
2
+ /**
3
+ * BSB (Better-Service-Base) is an event-bus based microservice framework.
4
+ * Copyright (C) 2016 - 2025 BetterCorp (PTY) Ltd
5
+ *
6
+ * This program is free software: you can redistribute it and/or modify
7
+ * it under the terms of the GNU Affero General Public License as published
8
+ * by the Free Software Foundation, either version 3 of the License, or
9
+ * (at your option) any later version.
10
+ *
11
+ * Alternatively, you may obtain a commercial license for this program.
12
+ * The commercial license allows you to use the Program in a closed-source manner,
13
+ * including the right to create derivative works that are not subject to the terms
14
+ * of the AGPL.
15
+ *
16
+ * To obtain a commercial license, please contact the copyright holders at
17
+ * https://www.bettercorp.dev. The terms and conditions of the commercial license
18
+ * will be provided upon request.
19
+ *
20
+ * This program is distributed in the hope that it will be useful,
21
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
22
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23
+ * GNU Affero General Public License for more details.
24
+ *
25
+ * You should have received a copy of the GNU Affero General Public License
26
+ * along with this program. If not, see <https://www.gnu.org/licenses/>.
27
+ */
28
+ Object.defineProperty(exports, "__esModule", { value: true });
29
+ exports.Plugin = exports.Config = exports.WinstonConfigSchema = void 0;
30
+ var observable_winston_1 = require("./plugins/observable-winston");
31
+ Object.defineProperty(exports, "WinstonConfigSchema", { enumerable: true, get: function () { return observable_winston_1.WinstonConfigSchema; } });
32
+ Object.defineProperty(exports, "Config", { enumerable: true, get: function () { return observable_winston_1.Config; } });
33
+ Object.defineProperty(exports, "Plugin", { enumerable: true, get: function () { return observable_winston_1.Plugin; } });
@@ -0,0 +1,159 @@
1
+ /**
2
+ * BSB (Better-Service-Base) is an event-bus based microservice framework.
3
+ * Copyright (C) 2016 - 2025 BetterCorp (PTY) Ltd
4
+ *
5
+ * This program is free software: you can redistribute it and/or modify
6
+ * it under the terms of the GNU Affero General Public License as published
7
+ * by the Free Software Foundation, either version 3 of the License, or
8
+ * (at your option) any later version.
9
+ *
10
+ * Alternatively, you may obtain a commercial license for this program.
11
+ * The commercial license allows you to use the Program in a closed-source manner,
12
+ * including the right to create derivative works that are not subject to the terms
13
+ * of the AGPL.
14
+ *
15
+ * To obtain a commercial license, please contact the copyright holders at
16
+ * https://www.bettercorp.dev. The terms and conditions of the commercial license
17
+ * will be provided upon request.
18
+ *
19
+ * This program is distributed in the hope that it will be useful,
20
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
21
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22
+ * GNU Affero General Public License for more details.
23
+ *
24
+ * You should have received a copy of the GNU Affero General Public License
25
+ * along with this program. If not, see <https://www.gnu.org/licenses/>.
26
+ */
27
+ import { BSBObservable, BSBObservableConstructor, BSBError } from "@bsb/base";
28
+ import { DTrace, LogMeta } from "@bsb/base";
29
+ import { z } from "zod";
30
+ import "winston-daily-rotate-file";
31
+ /**
32
+ * Configuration schema for Winston observable
33
+ */
34
+ export declare const WinstonConfigSchema: z.ZodObject<{
35
+ level: z.ZodDefault<z.ZodEnum<{
36
+ error: "error";
37
+ warn: "warn";
38
+ info: "info";
39
+ debug: "debug";
40
+ }>>;
41
+ transports: z.ZodObject<{
42
+ console: z.ZodObject<{
43
+ enabled: z.ZodDefault<z.ZodBoolean>;
44
+ colorize: z.ZodDefault<z.ZodBoolean>;
45
+ }, z.core.$strip>;
46
+ file: z.ZodObject<{
47
+ enabled: z.ZodDefault<z.ZodBoolean>;
48
+ filename: z.ZodDefault<z.ZodString>;
49
+ maxsize: z.ZodDefault<z.ZodNumber>;
50
+ maxFiles: z.ZodDefault<z.ZodNumber>;
51
+ tailable: z.ZodDefault<z.ZodBoolean>;
52
+ }, z.core.$strip>;
53
+ dailyRotate: z.ZodObject<{
54
+ enabled: z.ZodDefault<z.ZodBoolean>;
55
+ dirname: z.ZodDefault<z.ZodString>;
56
+ filename: z.ZodDefault<z.ZodString>;
57
+ datePattern: z.ZodDefault<z.ZodString>;
58
+ maxSize: z.ZodDefault<z.ZodString>;
59
+ maxFiles: z.ZodDefault<z.ZodString>;
60
+ zippedArchive: z.ZodDefault<z.ZodBoolean>;
61
+ }, z.core.$strip>;
62
+ }, z.core.$strip>;
63
+ format: z.ZodObject<{
64
+ timestamp: z.ZodDefault<z.ZodBoolean>;
65
+ json: z.ZodDefault<z.ZodBoolean>;
66
+ prettyPrint: z.ZodDefault<z.ZodBoolean>;
67
+ }, z.core.$strip>;
68
+ }, z.core.$strip>;
69
+ export type WinstonConfig = z.infer<typeof WinstonConfigSchema>;
70
+ export declare const Config: import("@bsb/base").BSBPluginConfigClass<z.ZodObject<{
71
+ level: z.ZodDefault<z.ZodEnum<{
72
+ error: "error";
73
+ warn: "warn";
74
+ info: "info";
75
+ debug: "debug";
76
+ }>>;
77
+ transports: z.ZodObject<{
78
+ console: z.ZodObject<{
79
+ enabled: z.ZodDefault<z.ZodBoolean>;
80
+ colorize: z.ZodDefault<z.ZodBoolean>;
81
+ }, z.core.$strip>;
82
+ file: z.ZodObject<{
83
+ enabled: z.ZodDefault<z.ZodBoolean>;
84
+ filename: z.ZodDefault<z.ZodString>;
85
+ maxsize: z.ZodDefault<z.ZodNumber>;
86
+ maxFiles: z.ZodDefault<z.ZodNumber>;
87
+ tailable: z.ZodDefault<z.ZodBoolean>;
88
+ }, z.core.$strip>;
89
+ dailyRotate: z.ZodObject<{
90
+ enabled: z.ZodDefault<z.ZodBoolean>;
91
+ dirname: z.ZodDefault<z.ZodString>;
92
+ filename: z.ZodDefault<z.ZodString>;
93
+ datePattern: z.ZodDefault<z.ZodString>;
94
+ maxSize: z.ZodDefault<z.ZodString>;
95
+ maxFiles: z.ZodDefault<z.ZodString>;
96
+ zippedArchive: z.ZodDefault<z.ZodBoolean>;
97
+ }, z.core.$strip>;
98
+ }, z.core.$strip>;
99
+ format: z.ZodObject<{
100
+ timestamp: z.ZodDefault<z.ZodBoolean>;
101
+ json: z.ZodDefault<z.ZodBoolean>;
102
+ prettyPrint: z.ZodDefault<z.ZodBoolean>;
103
+ }, z.core.$strip>;
104
+ }, z.core.$strip>>;
105
+ /**
106
+ * Winston observable plugin - integrate with Winston logger
107
+ */
108
+ export declare class Plugin extends BSBObservable<InstanceType<typeof Config>> {
109
+ static Config: import("@bsb/base").BSBPluginConfigClass<z.ZodObject<{
110
+ level: z.ZodDefault<z.ZodEnum<{
111
+ error: "error";
112
+ warn: "warn";
113
+ info: "info";
114
+ debug: "debug";
115
+ }>>;
116
+ transports: z.ZodObject<{
117
+ console: z.ZodObject<{
118
+ enabled: z.ZodDefault<z.ZodBoolean>;
119
+ colorize: z.ZodDefault<z.ZodBoolean>;
120
+ }, z.core.$strip>;
121
+ file: z.ZodObject<{
122
+ enabled: z.ZodDefault<z.ZodBoolean>;
123
+ filename: z.ZodDefault<z.ZodString>;
124
+ maxsize: z.ZodDefault<z.ZodNumber>;
125
+ maxFiles: z.ZodDefault<z.ZodNumber>;
126
+ tailable: z.ZodDefault<z.ZodBoolean>;
127
+ }, z.core.$strip>;
128
+ dailyRotate: z.ZodObject<{
129
+ enabled: z.ZodDefault<z.ZodBoolean>;
130
+ dirname: z.ZodDefault<z.ZodString>;
131
+ filename: z.ZodDefault<z.ZodString>;
132
+ datePattern: z.ZodDefault<z.ZodString>;
133
+ maxSize: z.ZodDefault<z.ZodString>;
134
+ maxFiles: z.ZodDefault<z.ZodString>;
135
+ zippedArchive: z.ZodDefault<z.ZodBoolean>;
136
+ }, z.core.$strip>;
137
+ }, z.core.$strip>;
138
+ format: z.ZodObject<{
139
+ timestamp: z.ZodDefault<z.ZodBoolean>;
140
+ json: z.ZodDefault<z.ZodBoolean>;
141
+ prettyPrint: z.ZodDefault<z.ZodBoolean>;
142
+ }, z.core.$strip>;
143
+ }, z.core.$strip>>;
144
+ private logFormatter;
145
+ private logger;
146
+ private isDisposed;
147
+ constructor(config: BSBObservableConstructor<InstanceType<typeof Config>>);
148
+ init(): Promise<void>;
149
+ run(): Promise<void>;
150
+ /**
151
+ * Send log to Winston
152
+ */
153
+ private sendLog;
154
+ debug(trace: DTrace, pluginName: string, message: string, meta: LogMeta<any>): void;
155
+ info(trace: DTrace, pluginName: string, message: string, meta: LogMeta<any>): void;
156
+ warn(trace: DTrace, pluginName: string, message: string, meta: LogMeta<any>): void;
157
+ error(trace: DTrace, pluginName: string, message: string | BSBError<any>, meta?: LogMeta<any>): void;
158
+ dispose(): void;
159
+ }
@@ -0,0 +1,252 @@
1
+ "use strict";
2
+ /**
3
+ * BSB (Better-Service-Base) is an event-bus based microservice framework.
4
+ * Copyright (C) 2016 - 2025 BetterCorp (PTY) Ltd
5
+ *
6
+ * This program is free software: you can redistribute it and/or modify
7
+ * it under the terms of the GNU Affero General Public License as published
8
+ * by the Free Software Foundation, either version 3 of the License, or
9
+ * (at your option) any later version.
10
+ *
11
+ * Alternatively, you may obtain a commercial license for this program.
12
+ * The commercial license allows you to use the Program in a closed-source manner,
13
+ * including the right to create derivative works that are not subject to the terms
14
+ * of the AGPL.
15
+ *
16
+ * To obtain a commercial license, please contact the copyright holders at
17
+ * https://www.bettercorp.dev. The terms and conditions of the commercial license
18
+ * will be provided upon request.
19
+ *
20
+ * This program is distributed in the hope that it will be useful,
21
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
22
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23
+ * GNU Affero General Public License for more details.
24
+ *
25
+ * You should have received a copy of the GNU Affero General Public License
26
+ * along with this program. If not, see <https://www.gnu.org/licenses/>.
27
+ */
28
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
29
+ if (k2 === undefined) k2 = k;
30
+ var desc = Object.getOwnPropertyDescriptor(m, k);
31
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
32
+ desc = { enumerable: true, get: function() { return m[k]; } };
33
+ }
34
+ Object.defineProperty(o, k2, desc);
35
+ }) : (function(o, m, k, k2) {
36
+ if (k2 === undefined) k2 = k;
37
+ o[k2] = m[k];
38
+ }));
39
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
40
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
41
+ }) : function(o, v) {
42
+ o["default"] = v;
43
+ });
44
+ var __importStar = (this && this.__importStar) || (function () {
45
+ var ownKeys = function(o) {
46
+ ownKeys = Object.getOwnPropertyNames || function (o) {
47
+ var ar = [];
48
+ for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
49
+ return ar;
50
+ };
51
+ return ownKeys(o);
52
+ };
53
+ return function (mod) {
54
+ if (mod && mod.__esModule) return mod;
55
+ var result = {};
56
+ if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
57
+ __setModuleDefault(result, mod);
58
+ return result;
59
+ };
60
+ })();
61
+ Object.defineProperty(exports, "__esModule", { value: true });
62
+ exports.Plugin = exports.Config = exports.WinstonConfigSchema = void 0;
63
+ const base_1 = require("@bsb/base");
64
+ const zod_1 = require("zod");
65
+ const winston = __importStar(require("winston"));
66
+ require("winston-daily-rotate-file");
67
+ /**
68
+ * Configuration schema for Winston observable
69
+ */
70
+ exports.WinstonConfigSchema = zod_1.z.object({
71
+ level: zod_1.z.enum(["error", "warn", "info", "debug"]).default("info"),
72
+ transports: zod_1.z.object({
73
+ console: zod_1.z.object({
74
+ enabled: zod_1.z.boolean().default(true),
75
+ colorize: zod_1.z.boolean().default(true),
76
+ }),
77
+ file: zod_1.z.object({
78
+ enabled: zod_1.z.boolean().default(false),
79
+ filename: zod_1.z.string().default("./logs/application.log"),
80
+ maxsize: zod_1.z.number().int().default(10485760), // 10MB
81
+ maxFiles: zod_1.z.number().int().default(5),
82
+ tailable: zod_1.z.boolean().default(true),
83
+ }),
84
+ dailyRotate: zod_1.z.object({
85
+ enabled: zod_1.z.boolean().default(false),
86
+ dirname: zod_1.z.string().default("./logs"),
87
+ filename: zod_1.z.string().default("application-%DATE%.log"),
88
+ datePattern: zod_1.z.string().default("YYYY-MM-DD"),
89
+ maxSize: zod_1.z.string().default("20m"),
90
+ maxFiles: zod_1.z.string().default("14d"),
91
+ zippedArchive: zod_1.z.boolean().default(true),
92
+ }),
93
+ }),
94
+ format: zod_1.z.object({
95
+ timestamp: zod_1.z.boolean().default(true),
96
+ json: zod_1.z.boolean().default(true),
97
+ prettyPrint: zod_1.z.boolean().default(false),
98
+ }),
99
+ });
100
+ exports.Config = (0, base_1.createConfigSchema)({
101
+ name: 'observable-winston',
102
+ description: 'Winston observable plugin with console, file, and rotation transports',
103
+ version: '9.0.0',
104
+ image: './observable-winston.png',
105
+ tags: ['winston', 'logging', 'observability', 'transports'],
106
+ documentation: ['./docs/plugin.md'],
107
+ }, exports.WinstonConfigSchema);
108
+ /**
109
+ * Convert BSB log level to Winston log level
110
+ */
111
+ function bsbLevelToWinstonLevel(level) {
112
+ const normalized = level.toLowerCase();
113
+ switch (normalized) {
114
+ case "error":
115
+ return "error";
116
+ case "warn":
117
+ case "warning":
118
+ return "warn";
119
+ case "info":
120
+ return "info";
121
+ case "debug":
122
+ return "debug";
123
+ default:
124
+ return "info";
125
+ }
126
+ }
127
+ /**
128
+ * Winston observable plugin - integrate with Winston logger
129
+ */
130
+ class Plugin extends base_1.BSBObservable {
131
+ static Config = exports.Config;
132
+ logFormatter = new base_1.LogFormatter();
133
+ logger;
134
+ isDisposed = false;
135
+ constructor(config) {
136
+ super(config);
137
+ }
138
+ async init() {
139
+ const transports = [];
140
+ // Console transport
141
+ if (this.config.transports.console.enabled) {
142
+ const consoleFormat = this.config.transports.console.colorize
143
+ ? winston.format.combine(winston.format.colorize(), winston.format.simple())
144
+ : winston.format.simple();
145
+ transports.push(new winston.transports.Console({
146
+ format: consoleFormat,
147
+ }));
148
+ }
149
+ // File transport
150
+ if (this.config.transports.file.enabled) {
151
+ transports.push(new winston.transports.File({
152
+ filename: this.config.transports.file.filename,
153
+ maxsize: this.config.transports.file.maxsize,
154
+ maxFiles: this.config.transports.file.maxFiles,
155
+ tailable: this.config.transports.file.tailable,
156
+ }));
157
+ }
158
+ // Daily rotate file transport
159
+ if (this.config.transports.dailyRotate.enabled) {
160
+ const DailyRotateFile = require("winston-daily-rotate-file");
161
+ transports.push(new DailyRotateFile({
162
+ dirname: this.config.transports.dailyRotate.dirname,
163
+ filename: this.config.transports.dailyRotate.filename,
164
+ datePattern: this.config.transports.dailyRotate.datePattern,
165
+ maxSize: this.config.transports.dailyRotate.maxSize,
166
+ maxFiles: this.config.transports.dailyRotate.maxFiles,
167
+ zippedArchive: this.config.transports.dailyRotate.zippedArchive,
168
+ }));
169
+ }
170
+ // Build format
171
+ const formats = [];
172
+ if (this.config.format.timestamp) {
173
+ formats.push(winston.format.timestamp());
174
+ }
175
+ if (this.config.format.json) {
176
+ formats.push(winston.format.json());
177
+ }
178
+ else if (this.config.format.prettyPrint) {
179
+ formats.push(winston.format.prettyPrint());
180
+ }
181
+ else {
182
+ formats.push(winston.format.simple());
183
+ }
184
+ // Create logger
185
+ this.logger = winston.createLogger({
186
+ level: this.config.level,
187
+ format: winston.format.combine(...formats),
188
+ transports,
189
+ });
190
+ }
191
+ async run() {
192
+ // No runtime setup needed
193
+ }
194
+ /**
195
+ * Send log to Winston
196
+ */
197
+ sendLog(level, trace, pluginName, message, meta) {
198
+ if (this.isDisposed) {
199
+ return;
200
+ }
201
+ try {
202
+ const formattedMessage = this.logFormatter.formatLog(trace, message, meta);
203
+ const winstonLevel = bsbLevelToWinstonLevel(level);
204
+ // Build metadata object
205
+ const metadata = {
206
+ plugin: pluginName,
207
+ trace_id: trace.t,
208
+ span_id: trace.s,
209
+ level,
210
+ ...meta,
211
+ };
212
+ // Log to Winston
213
+ this.logger.log(winstonLevel, formattedMessage, metadata);
214
+ }
215
+ catch (err) {
216
+ console.error(`[observable-winston] Send error: ${err.message}`);
217
+ }
218
+ }
219
+ // Logging methods
220
+ debug(trace, pluginName, message, meta) {
221
+ if (this.mode === "production") {
222
+ return;
223
+ }
224
+ this.sendLog("debug", trace, pluginName, message, meta);
225
+ }
226
+ info(trace, pluginName, message, meta) {
227
+ this.sendLog("info", trace, pluginName, message, meta);
228
+ }
229
+ warn(trace, pluginName, message, meta) {
230
+ this.sendLog("warn", trace, pluginName, message, meta);
231
+ }
232
+ error(trace, pluginName, message, meta) {
233
+ if (message instanceof base_1.BSBError) {
234
+ if (message.raw !== null) {
235
+ this.sendLog("error", message.raw.trace, pluginName, message.raw.message, message.raw.meta);
236
+ }
237
+ else {
238
+ this.sendLog("error", trace, pluginName, message.message);
239
+ }
240
+ }
241
+ else {
242
+ this.sendLog("error", trace, pluginName, message, meta);
243
+ }
244
+ }
245
+ dispose() {
246
+ this.isDisposed = true;
247
+ if (this.logger) {
248
+ this.logger.close();
249
+ }
250
+ }
251
+ }
252
+ exports.Plugin = Plugin;