@we-are-singular/logger 1.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.
@@ -0,0 +1,30 @@
1
+ /**
2
+ * Logger class
3
+ * @param app
4
+ * @param module
5
+ * @param context
6
+ * @constructor
7
+ * @example
8
+ * const logger = new Logger("test")
9
+ * logger.info("hello world")
10
+ * logger.addModule("some module")
11
+ * logger.info("hello world", { user: { id: 1, name: "John Doe" } })
12
+ */
13
+ export declare class LoggerClass {
14
+ private app;
15
+ private module;
16
+ private context;
17
+ pino: import("pino").Logger;
18
+ constructor(app?: string, module?: string, context?: string);
19
+ private updateLogger;
20
+ fork(): LoggerClass;
21
+ setModule(module: string): this;
22
+ setContext(context: string): this;
23
+ trace(message: string, data?: unknown): void;
24
+ debug(message: string, data?: unknown): void;
25
+ info(message: string, data?: unknown): void;
26
+ warn(message: string, data?: unknown): void;
27
+ error(message: string, data?: unknown): void;
28
+ fatal(message: string, data?: unknown): void;
29
+ }
30
+ //# sourceMappingURL=LoggerClass.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"LoggerClass.d.ts","sourceRoot":"","sources":["../src/LoggerClass.ts"],"names":[],"mappings":"AAEA;;;;;;;;;;;GAWG;AACH,qBAAa,WAAW;IACtB,OAAO,CAAC,GAAG,CAAK;IAChB,OAAO,CAAC,MAAM,CAAK;IACnB,OAAO,CAAC,OAAO,CAAK;IACb,IAAI,wBAAc;gBACb,GAAG,CAAC,EAAE,MAAM,EAAE,MAAM,CAAC,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,MAAM;IAO3D,OAAO,CAAC,YAAY;IAUpB,IAAI;IAIJ,SAAS,CAAC,MAAM,EAAE,MAAM;IAKxB,UAAU,CAAC,OAAO,EAAE,MAAM;IAK1B,KAAK,CAAC,OAAO,EAAE,MAAM,EAAE,IAAI,CAAC,EAAE,OAAO;IAIrC,KAAK,CAAC,OAAO,EAAE,MAAM,EAAE,IAAI,CAAC,EAAE,OAAO;IAIrC,IAAI,CAAC,OAAO,EAAE,MAAM,EAAE,IAAI,CAAC,EAAE,OAAO;IAIpC,IAAI,CAAC,OAAO,EAAE,MAAM,EAAE,IAAI,CAAC,EAAE,OAAO;IAIpC,KAAK,CAAC,OAAO,EAAE,MAAM,EAAE,IAAI,CAAC,EAAE,OAAO;IAIrC,KAAK,CAAC,OAAO,EAAE,MAAM,EAAE,IAAI,CAAC,EAAE,OAAO;CAGtC"}
@@ -0,0 +1,166 @@
1
+ 'use strict';
2
+
3
+ var pino = require('pino');
4
+ var pretty = require('pino-pretty');
5
+
6
+ const loggerOptions = {
7
+ level: "trace",
8
+ redact: ["user.password", "options.token"], // example of a redaction key
9
+ timestamp: pino.stdTimeFunctions.isoTime,
10
+ };
11
+ const loggerFactory = () => {
12
+ var _a, _b, _c;
13
+ if (process.env.NODE_ENV !== "production" || parseInt((_a = process.env.LOG_HUMAN) !== null && _a !== void 0 ? _a : "") === 1) {
14
+ return pino(loggerOptions, pretty({
15
+ minimumLevel: (_b = process.env.LOG_LEVEL) !== null && _b !== void 0 ? _b : "trace",
16
+ sync: process.env.NODE_ENV === "test",
17
+ colorize: true,
18
+ colorizeObjects: true,
19
+ singleLine: false,
20
+ ignore: "pid,hostname,app,module,context",
21
+ customPrettifiers: {
22
+ //pid: (log) => "{log.pid}",
23
+ //app: (name) => name,
24
+ },
25
+ messageFormat: (log) => {
26
+ const { app, module, context, messageKey = "msg" } = log;
27
+ return `[${[app, module, context].filter(Boolean).join("::")}] ${
28
+ // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
29
+ log[messageKey]}`;
30
+ },
31
+ }));
32
+ }
33
+ return pino(Object.assign(Object.assign({}, loggerOptions), { level: (_c = process.env.LOG_LEVEL) !== null && _c !== void 0 ? _c : "info", messageKey: "message", formatters: {
34
+ level: (label) => {
35
+ return { level: label };
36
+ },
37
+ } }));
38
+ };
39
+ /**
40
+ * Main logger instance
41
+ */
42
+ const mainLogger = loggerFactory();
43
+ /**
44
+ * export the main logger instance
45
+ */
46
+ const getLogger = () => mainLogger;
47
+
48
+ /**
49
+ * Logger class
50
+ * @param app
51
+ * @param module
52
+ * @param context
53
+ * @constructor
54
+ * @example
55
+ * const logger = new Logger("test")
56
+ * logger.info("hello world")
57
+ * logger.addModule("some module")
58
+ * logger.info("hello world", { user: { id: 1, name: "John Doe" } })
59
+ */
60
+ class LoggerClass {
61
+ constructor(app, module, context) {
62
+ this.app = "";
63
+ this.module = "";
64
+ this.context = "";
65
+ this.pino = getLogger();
66
+ this.app = app !== null && app !== void 0 ? app : "";
67
+ this.module = module !== null && module !== void 0 ? module : "";
68
+ this.context = context !== null && context !== void 0 ? context : "";
69
+ this.updateLogger();
70
+ }
71
+ updateLogger() {
72
+ this.pino = getLogger().child(Object.assign(Object.assign(Object.assign({}, (this.app && { app: this.app })), (this.module && { module: this.module })), (this.context && { context: this.context })));
73
+ return this;
74
+ }
75
+ fork() {
76
+ return new LoggerClass(this.app, this.module, this.context);
77
+ }
78
+ setModule(module) {
79
+ this.module = module;
80
+ return this.updateLogger();
81
+ }
82
+ setContext(context) {
83
+ this.context = context;
84
+ return this.updateLogger();
85
+ }
86
+ trace(message, data) {
87
+ this.pino.trace(data || null, message);
88
+ }
89
+ debug(message, data) {
90
+ this.pino.debug(data || null, message);
91
+ }
92
+ info(message, data) {
93
+ this.pino.info(data || null, message);
94
+ }
95
+ warn(message, data) {
96
+ this.pino.warn(data || null, message);
97
+ }
98
+ error(message, data) {
99
+ this.pino.error(data || null, message);
100
+ }
101
+ fatal(message, data) {
102
+ this.pino.fatal(data || null, message);
103
+ }
104
+ }
105
+
106
+ class LoggerService {
107
+ constructor(withLogger) {
108
+ var _a;
109
+ this.logger = withLogger !== null && withLogger !== void 0 ? withLogger : new LoggerClass();
110
+ this.logger.pino.level = (_a = process.env.LOG_LEVEL) !== null && _a !== void 0 ? _a : "info";
111
+ }
112
+ static forFeature(module, useClass = LoggerService) {
113
+ return {
114
+ provide: useClass,
115
+ useFactory: () => new useClass().withModule(module),
116
+ };
117
+ }
118
+ /**
119
+ * This is a workaround to create an interface complying to NestJS's LoggerService for internal logging
120
+ */
121
+ forRoot() {
122
+ return {
123
+ log: (msg, context, extra) => {
124
+ this.logger.info(msg, { context, extra });
125
+ },
126
+ error: (msg, stack, context) => {
127
+ this.logger.error(String(stack), { context, error: msg });
128
+ },
129
+ warn: (msg, context) => {
130
+ this.logger.warn(msg, { context });
131
+ },
132
+ debug: (msg, context) => {
133
+ this.logger.debug(msg, { context });
134
+ },
135
+ verbose: (msg, context) => {
136
+ this.logger.trace(msg, { context });
137
+ },
138
+ };
139
+ }
140
+ withModule(module, useClass = LoggerService) {
141
+ return new useClass(this.logger.fork().setModule(module));
142
+ }
143
+ withContext(context, useClass = LoggerService) {
144
+ return new useClass(this.logger.fork().setContext(context));
145
+ }
146
+ log(msg, ...extra) {
147
+ this.logger.info(msg, ...extra);
148
+ }
149
+ error(msg, ...extra) {
150
+ this.logger.error(msg, ...extra);
151
+ }
152
+ warn(msg, ...extra) {
153
+ this.logger.warn(msg, ...extra);
154
+ }
155
+ debug(msg, ...extra) {
156
+ this.logger.debug(msg, ...extra);
157
+ }
158
+ verbose(msg, ...extra) {
159
+ this.logger.trace(msg, ...extra);
160
+ }
161
+ }
162
+
163
+ exports.LoggerClass = LoggerClass;
164
+ exports.LoggerService = LoggerService;
165
+ exports.getLogger = getLogger;
166
+ //# sourceMappingURL=index.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.cjs","sources":["../src/pino.instance.ts","../src/LoggerClass.ts","../src/nestjs/LoggerService.ts"],"sourcesContent":["import pino, { LoggerOptions, LogDescriptor, stdTimeFunctions, type Logger } from \"pino\"\nimport pretty from \"pino-pretty\"\n\nconst loggerOptions = {\n level: \"trace\",\n redact: [\"user.password\", \"options.token\"], // example of a redaction key\n timestamp: stdTimeFunctions.isoTime,\n} satisfies LoggerOptions\n\nconst loggerFactory = (): Logger => {\n if (process.env.NODE_ENV !== \"production\" || parseInt(process.env.LOG_HUMAN ?? \"\") === 1) {\n return pino(\n loggerOptions,\n pretty({\n minimumLevel: (process.env.LOG_LEVEL as pino.Level) ?? \"trace\",\n sync: process.env.NODE_ENV === \"test\",\n colorize: true,\n colorizeObjects: true,\n singleLine: false,\n ignore: \"pid,hostname,app,module,context\",\n customPrettifiers: {\n //pid: (log) => \"{log.pid}\",\n //app: (name) => name,\n },\n messageFormat: (log: LogDescriptor) => {\n const { app, module, context, messageKey = \"msg\" }: LogDescriptor = log\n return `[${[app, module, context].filter(Boolean).join(\"::\")}] ${\n // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access\n log[messageKey as keyof LogDescriptor]\n }`\n },\n })\n )\n }\n\n return pino({\n ...loggerOptions,\n level: process.env.LOG_LEVEL ?? \"info\",\n messageKey: \"message\",\n formatters: {\n level: (label) => {\n return { level: label }\n },\n },\n })\n}\n\n/**\n * Main logger instance\n */\nconst mainLogger = loggerFactory()\n\n/**\n * export the main logger instance\n */\nexport const getLogger = () => mainLogger\n","import { getLogger } from \"./pino.instance\"\n\n/**\n * Logger class\n * @param app\n * @param module\n * @param context\n * @constructor\n * @example\n * const logger = new Logger(\"test\")\n * logger.info(\"hello world\")\n * logger.addModule(\"some module\")\n * logger.info(\"hello world\", { user: { id: 1, name: \"John Doe\" } })\n */\nexport class LoggerClass {\n private app = \"\"\n private module = \"\"\n private context = \"\"\n public pino = getLogger()\n constructor(app?: string, module?: string, context?: string) {\n this.app = app ?? \"\"\n this.module = module ?? \"\"\n this.context = context ?? \"\"\n this.updateLogger()\n }\n\n private updateLogger() {\n this.pino = getLogger().child({\n ...(this.app && { app: this.app }),\n ...(this.module && { module: this.module }),\n ...(this.context && { context: this.context }),\n })\n\n return this\n }\n\n fork() {\n return new LoggerClass(this.app, this.module, this.context)\n }\n\n setModule(module: string) {\n this.module = module\n return this.updateLogger()\n }\n\n setContext(context: string) {\n this.context = context\n return this.updateLogger()\n }\n\n trace(message: string, data?: unknown) {\n this.pino.trace(data || null, message)\n }\n\n debug(message: string, data?: unknown) {\n this.pino.debug(data || null, message)\n }\n\n info(message: string, data?: unknown) {\n this.pino.info(data || null, message)\n }\n\n warn(message: string, data?: unknown) {\n this.pino.warn(data || null, message)\n }\n\n error(message: string, data?: unknown) {\n this.pino.error(data || null, message)\n }\n\n fatal(message: string, data?: unknown) {\n this.pino.fatal(data || null, message)\n }\n}\n","import { type LoggerService as NestLoggerService } from \"@nestjs/common\"\nimport { LoggerClass } from \"../LoggerClass\"\n\nexport class LoggerService {\n private logger: LoggerClass\n\n constructor(withLogger?: LoggerClass) {\n this.logger = withLogger ?? new LoggerClass()\n this.logger.pino.level = process.env.LOG_LEVEL ?? \"info\"\n }\n\n static forFeature(module: string, useClass: typeof LoggerService = LoggerService) {\n return {\n provide: useClass,\n useFactory: () => new useClass().withModule(module),\n }\n }\n\n /**\n * This is a workaround to create an interface complying to NestJS's LoggerService for internal logging\n */\n forRoot(): NestLoggerService {\n return {\n log: (msg, context: string, extra) => {\n this.logger.info(msg, { context, extra })\n },\n error: (msg, stack: unknown, context: string) => {\n this.logger.error(String(stack), { context, error: msg })\n },\n warn: (msg, context: string) => {\n this.logger.warn(msg, { context })\n },\n debug: (msg, context: string) => {\n this.logger.debug(msg, { context })\n },\n verbose: (msg, context: string) => {\n this.logger.trace(msg, { context })\n },\n }\n }\n\n withModule(module: string, useClass: typeof LoggerService = LoggerService) {\n return new useClass(this.logger.fork().setModule(module))\n }\n\n withContext(context: string, useClass: typeof LoggerService = LoggerService) {\n return new useClass(this.logger.fork().setContext(context))\n }\n\n log(msg: string, ...extra: unknown[]) {\n this.logger.info(msg, ...extra)\n }\n\n error(msg: string, ...extra: unknown[]) {\n this.logger.error(msg, ...extra)\n }\n\n warn(msg: string, ...extra: unknown[]) {\n this.logger.warn(msg, ...extra)\n }\n\n debug(msg: string, ...extra: unknown[]) {\n this.logger.debug(msg, ...extra)\n }\n\n verbose(msg: string, ...extra: unknown[]) {\n this.logger.trace(msg, ...extra)\n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,CAAC,GAAW,EAAE,GAAG,KAAgB,EAAA;QACpC,IAAI,CAAC,MAAM,CAAC,KAAK,CAAC,GAAG,EAAE,GAAG,KAAK,CAAC,CAAA;KACjC;AAED,IAAA,OAAO,CAAC,GAAW,EAAE,GAAG,KAAgB,EAAA;QACtC,IAAI,CAAC,MAAM,CAAC,KAAK,CAAC,GAAG,EAAE,GAAG,KAAK,CAAC,CAAA;KACjC;AACF;;;;;;"}
@@ -0,0 +1,4 @@
1
+ export { getLogger } from "./pino.instance";
2
+ export { LoggerService } from "./nestjs/LoggerService";
3
+ export { LoggerClass } from "./LoggerClass";
4
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,iBAAiB,CAAA;AAC3C,OAAO,EAAE,aAAa,EAAE,MAAM,wBAAwB,CAAA;AACtD,OAAO,EAAE,WAAW,EAAE,MAAM,eAAe,CAAA"}
@@ -0,0 +1,162 @@
1
+ import pino, { stdTimeFunctions } from 'pino';
2
+ import pretty from 'pino-pretty';
3
+
4
+ const loggerOptions = {
5
+ level: "trace",
6
+ redact: ["user.password", "options.token"], // example of a redaction key
7
+ timestamp: stdTimeFunctions.isoTime,
8
+ };
9
+ const loggerFactory = () => {
10
+ var _a, _b, _c;
11
+ if (process.env.NODE_ENV !== "production" || parseInt((_a = process.env.LOG_HUMAN) !== null && _a !== void 0 ? _a : "") === 1) {
12
+ return pino(loggerOptions, pretty({
13
+ minimumLevel: (_b = process.env.LOG_LEVEL) !== null && _b !== void 0 ? _b : "trace",
14
+ sync: process.env.NODE_ENV === "test",
15
+ colorize: true,
16
+ colorizeObjects: true,
17
+ singleLine: false,
18
+ ignore: "pid,hostname,app,module,context",
19
+ customPrettifiers: {
20
+ //pid: (log) => "{log.pid}",
21
+ //app: (name) => name,
22
+ },
23
+ messageFormat: (log) => {
24
+ const { app, module, context, messageKey = "msg" } = log;
25
+ return `[${[app, module, context].filter(Boolean).join("::")}] ${
26
+ // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
27
+ log[messageKey]}`;
28
+ },
29
+ }));
30
+ }
31
+ return pino(Object.assign(Object.assign({}, loggerOptions), { level: (_c = process.env.LOG_LEVEL) !== null && _c !== void 0 ? _c : "info", messageKey: "message", formatters: {
32
+ level: (label) => {
33
+ return { level: label };
34
+ },
35
+ } }));
36
+ };
37
+ /**
38
+ * Main logger instance
39
+ */
40
+ const mainLogger = loggerFactory();
41
+ /**
42
+ * export the main logger instance
43
+ */
44
+ const getLogger = () => mainLogger;
45
+
46
+ /**
47
+ * Logger class
48
+ * @param app
49
+ * @param module
50
+ * @param context
51
+ * @constructor
52
+ * @example
53
+ * const logger = new Logger("test")
54
+ * logger.info("hello world")
55
+ * logger.addModule("some module")
56
+ * logger.info("hello world", { user: { id: 1, name: "John Doe" } })
57
+ */
58
+ class LoggerClass {
59
+ constructor(app, module, context) {
60
+ this.app = "";
61
+ this.module = "";
62
+ this.context = "";
63
+ this.pino = getLogger();
64
+ this.app = app !== null && app !== void 0 ? app : "";
65
+ this.module = module !== null && module !== void 0 ? module : "";
66
+ this.context = context !== null && context !== void 0 ? context : "";
67
+ this.updateLogger();
68
+ }
69
+ updateLogger() {
70
+ this.pino = getLogger().child(Object.assign(Object.assign(Object.assign({}, (this.app && { app: this.app })), (this.module && { module: this.module })), (this.context && { context: this.context })));
71
+ return this;
72
+ }
73
+ fork() {
74
+ return new LoggerClass(this.app, this.module, this.context);
75
+ }
76
+ setModule(module) {
77
+ this.module = module;
78
+ return this.updateLogger();
79
+ }
80
+ setContext(context) {
81
+ this.context = context;
82
+ return this.updateLogger();
83
+ }
84
+ trace(message, data) {
85
+ this.pino.trace(data || null, message);
86
+ }
87
+ debug(message, data) {
88
+ this.pino.debug(data || null, message);
89
+ }
90
+ info(message, data) {
91
+ this.pino.info(data || null, message);
92
+ }
93
+ warn(message, data) {
94
+ this.pino.warn(data || null, message);
95
+ }
96
+ error(message, data) {
97
+ this.pino.error(data || null, message);
98
+ }
99
+ fatal(message, data) {
100
+ this.pino.fatal(data || null, message);
101
+ }
102
+ }
103
+
104
+ class LoggerService {
105
+ constructor(withLogger) {
106
+ var _a;
107
+ this.logger = withLogger !== null && withLogger !== void 0 ? withLogger : new LoggerClass();
108
+ this.logger.pino.level = (_a = process.env.LOG_LEVEL) !== null && _a !== void 0 ? _a : "info";
109
+ }
110
+ static forFeature(module, useClass = LoggerService) {
111
+ return {
112
+ provide: useClass,
113
+ useFactory: () => new useClass().withModule(module),
114
+ };
115
+ }
116
+ /**
117
+ * This is a workaround to create an interface complying to NestJS's LoggerService for internal logging
118
+ */
119
+ forRoot() {
120
+ return {
121
+ log: (msg, context, extra) => {
122
+ this.logger.info(msg, { context, extra });
123
+ },
124
+ error: (msg, stack, context) => {
125
+ this.logger.error(String(stack), { context, error: msg });
126
+ },
127
+ warn: (msg, context) => {
128
+ this.logger.warn(msg, { context });
129
+ },
130
+ debug: (msg, context) => {
131
+ this.logger.debug(msg, { context });
132
+ },
133
+ verbose: (msg, context) => {
134
+ this.logger.trace(msg, { context });
135
+ },
136
+ };
137
+ }
138
+ withModule(module, useClass = LoggerService) {
139
+ return new useClass(this.logger.fork().setModule(module));
140
+ }
141
+ withContext(context, useClass = LoggerService) {
142
+ return new useClass(this.logger.fork().setContext(context));
143
+ }
144
+ log(msg, ...extra) {
145
+ this.logger.info(msg, ...extra);
146
+ }
147
+ error(msg, ...extra) {
148
+ this.logger.error(msg, ...extra);
149
+ }
150
+ warn(msg, ...extra) {
151
+ this.logger.warn(msg, ...extra);
152
+ }
153
+ debug(msg, ...extra) {
154
+ this.logger.debug(msg, ...extra);
155
+ }
156
+ verbose(msg, ...extra) {
157
+ this.logger.trace(msg, ...extra);
158
+ }
159
+ }
160
+
161
+ export { LoggerClass, LoggerService, getLogger };
162
+ //# sourceMappingURL=index.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.mjs","sources":["../src/pino.instance.ts","../src/LoggerClass.ts","../src/nestjs/LoggerService.ts"],"sourcesContent":["import pino, { LoggerOptions, LogDescriptor, stdTimeFunctions, type Logger } from \"pino\"\nimport pretty from \"pino-pretty\"\n\nconst loggerOptions = {\n level: \"trace\",\n redact: [\"user.password\", \"options.token\"], // example of a redaction key\n timestamp: stdTimeFunctions.isoTime,\n} satisfies LoggerOptions\n\nconst loggerFactory = (): Logger => {\n if (process.env.NODE_ENV !== \"production\" || parseInt(process.env.LOG_HUMAN ?? \"\") === 1) {\n return pino(\n loggerOptions,\n pretty({\n minimumLevel: (process.env.LOG_LEVEL as pino.Level) ?? \"trace\",\n sync: process.env.NODE_ENV === \"test\",\n colorize: true,\n colorizeObjects: true,\n singleLine: false,\n ignore: \"pid,hostname,app,module,context\",\n customPrettifiers: {\n //pid: (log) => \"{log.pid}\",\n //app: (name) => name,\n },\n messageFormat: (log: LogDescriptor) => {\n const { app, module, context, messageKey = \"msg\" }: LogDescriptor = log\n return `[${[app, module, context].filter(Boolean).join(\"::\")}] ${\n // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access\n log[messageKey as keyof LogDescriptor]\n }`\n },\n })\n )\n }\n\n return pino({\n ...loggerOptions,\n level: process.env.LOG_LEVEL ?? \"info\",\n messageKey: \"message\",\n formatters: {\n level: (label) => {\n return { level: label }\n },\n },\n })\n}\n\n/**\n * Main logger instance\n */\nconst mainLogger = loggerFactory()\n\n/**\n * export the main logger instance\n */\nexport const getLogger = () => mainLogger\n","import { getLogger } from \"./pino.instance\"\n\n/**\n * Logger class\n * @param app\n * @param module\n * @param context\n * @constructor\n * @example\n * const logger = new Logger(\"test\")\n * logger.info(\"hello world\")\n * logger.addModule(\"some module\")\n * logger.info(\"hello world\", { user: { id: 1, name: \"John Doe\" } })\n */\nexport class LoggerClass {\n private app = \"\"\n private module = \"\"\n private context = \"\"\n public pino = getLogger()\n constructor(app?: string, module?: string, context?: string) {\n this.app = app ?? \"\"\n this.module = module ?? \"\"\n this.context = context ?? \"\"\n this.updateLogger()\n }\n\n private updateLogger() {\n this.pino = getLogger().child({\n ...(this.app && { app: this.app }),\n ...(this.module && { module: this.module }),\n ...(this.context && { context: this.context }),\n })\n\n return this\n }\n\n fork() {\n return new LoggerClass(this.app, this.module, this.context)\n }\n\n setModule(module: string) {\n this.module = module\n return this.updateLogger()\n }\n\n setContext(context: string) {\n this.context = context\n return this.updateLogger()\n }\n\n trace(message: string, data?: unknown) {\n this.pino.trace(data || null, message)\n }\n\n debug(message: string, data?: unknown) {\n this.pino.debug(data || null, message)\n }\n\n info(message: string, data?: unknown) {\n this.pino.info(data || null, message)\n }\n\n warn(message: string, data?: unknown) {\n this.pino.warn(data || null, message)\n }\n\n error(message: string, data?: unknown) {\n this.pino.error(data || null, message)\n }\n\n fatal(message: string, data?: unknown) {\n this.pino.fatal(data || null, message)\n }\n}\n","import { type LoggerService as NestLoggerService } from \"@nestjs/common\"\nimport { LoggerClass } from \"../LoggerClass\"\n\nexport class LoggerService {\n private logger: LoggerClass\n\n constructor(withLogger?: LoggerClass) {\n this.logger = withLogger ?? new LoggerClass()\n this.logger.pino.level = process.env.LOG_LEVEL ?? \"info\"\n }\n\n static forFeature(module: string, useClass: typeof LoggerService = LoggerService) {\n return {\n provide: useClass,\n useFactory: () => new useClass().withModule(module),\n }\n }\n\n /**\n * This is a workaround to create an interface complying to NestJS's LoggerService for internal logging\n */\n forRoot(): NestLoggerService {\n return {\n log: (msg, context: string, extra) => {\n this.logger.info(msg, { context, extra })\n },\n error: (msg, stack: unknown, context: string) => {\n this.logger.error(String(stack), { context, error: msg })\n },\n warn: (msg, context: string) => {\n this.logger.warn(msg, { context })\n },\n debug: (msg, context: string) => {\n this.logger.debug(msg, { context })\n },\n verbose: (msg, context: string) => {\n this.logger.trace(msg, { context })\n },\n }\n }\n\n withModule(module: string, useClass: typeof LoggerService = LoggerService) {\n return new useClass(this.logger.fork().setModule(module))\n }\n\n withContext(context: string, useClass: typeof LoggerService = LoggerService) {\n return new useClass(this.logger.fork().setContext(context))\n }\n\n log(msg: string, ...extra: unknown[]) {\n this.logger.info(msg, ...extra)\n }\n\n error(msg: string, ...extra: unknown[]) {\n this.logger.error(msg, ...extra)\n }\n\n warn(msg: string, ...extra: unknown[]) {\n this.logger.warn(msg, ...extra)\n }\n\n debug(msg: string, ...extra: unknown[]) {\n this.logger.debug(msg, ...extra)\n }\n\n verbose(msg: string, ...extra: unknown[]) {\n this.logger.trace(msg, ...extra)\n }\n}\n"],"names":[],"mappings":";;;AAGA,MAAM,aAAa,GAAG;AACpB,IAAA,KAAK,EAAE,OAAO;AACd,IAAA,MAAM,EAAE,CAAC,eAAe,EAAE,eAAe,CAAC;IAC1C,SAAS,EAAE,gBAAgB,CAAC,OAAO;CACZ,CAAA;AAEzB,MAAM,aAAa,GAAG,MAAa;;IACjC,IAAI,OAAO,CAAC,GAAG,CAAC,QAAQ,KAAK,YAAY,IAAI,QAAQ,CAAC,CAAA,EAAA,GAAA,OAAO,CAAC,GAAG,CAAC,SAAS,MAAA,IAAA,IAAA,EAAA,KAAA,KAAA,CAAA,GAAA,EAAA,GAAI,EAAE,CAAC,KAAK,CAAC,EAAE;AACxF,QAAA,OAAO,IAAI,CACT,aAAa,EACb,MAAM,CAAC;YACL,YAAY,EAAE,MAAC,OAAO,CAAC,GAAG,CAAC,SAAwB,mCAAI,OAAO;AAC9D,YAAA,IAAI,EAAE,OAAO,CAAC,GAAG,CAAC,QAAQ,KAAK,MAAM;AACrC,YAAA,QAAQ,EAAE,IAAI;AACd,YAAA,eAAe,EAAE,IAAI;AACrB,YAAA,UAAU,EAAE,KAAK;AACjB,YAAA,MAAM,EAAE,iCAAiC;AACzC,YAAA,iBAAiB,EAAE;;;AAGlB,aAAA;AACD,YAAA,aAAa,EAAE,CAAC,GAAkB,KAAI;AACpC,gBAAA,MAAM,EAAE,GAAG,EAAE,MAAM,EAAE,OAAO,EAAE,UAAU,GAAG,KAAK,EAAE,GAAkB,GAAG,CAAA;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IAAA,KAAK,CAAC,GAAW,EAAE,GAAG,KAAgB,EAAA;QACpC,IAAI,CAAC,MAAM,CAAC,KAAK,CAAC,GAAG,EAAE,GAAG,KAAK,CAAC,CAAA;KACjC;AAED,IAAA,IAAI,CAAC,GAAW,EAAE,GAAG,KAAgB,EAAA;QACnC,IAAI,CAAC,MAAM,CAAC,IAAI,CAAC,GAAG,EAAE,GAAG,KAAK,CAAC,CAAA;KAChC;AAED,IAAA,KAAK,CAAC,GAAW,EAAE,GAAG,KAAgB,EAAA;QACpC,IAAI,CAAC,MAAM,CAAC,KAAK,CAAC,GAAG,EAAE,GAAG,KAAK,CAAC,CAAA;KACjC;AAED,IAAA,OAAO,CAAC,GAAW,EAAE,GAAG,KAAgB,EAAA;QACtC,IAAI,CAAC,MAAM,CAAC,KAAK,CAAC,GAAG,EAAE,GAAG,KAAK,CAAC,CAAA;KACjC;AACF;;;;"}
@@ -0,0 +1,22 @@
1
+ import { type LoggerService as NestLoggerService } from "@nestjs/common";
2
+ import { LoggerClass } from "../LoggerClass";
3
+ export declare class LoggerService {
4
+ private logger;
5
+ constructor(withLogger?: LoggerClass);
6
+ static forFeature(module: string, useClass?: typeof LoggerService): {
7
+ provide: typeof LoggerService;
8
+ useFactory: () => LoggerService;
9
+ };
10
+ /**
11
+ * This is a workaround to create an interface complying to NestJS's LoggerService for internal logging
12
+ */
13
+ forRoot(): NestLoggerService;
14
+ withModule(module: string, useClass?: typeof LoggerService): LoggerService;
15
+ withContext(context: string, useClass?: typeof LoggerService): LoggerService;
16
+ log(msg: string, ...extra: unknown[]): void;
17
+ error(msg: string, ...extra: unknown[]): void;
18
+ warn(msg: string, ...extra: unknown[]): void;
19
+ debug(msg: string, ...extra: unknown[]): void;
20
+ verbose(msg: string, ...extra: unknown[]): void;
21
+ }
22
+ //# sourceMappingURL=LoggerService.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"LoggerService.d.ts","sourceRoot":"","sources":["../../src/nestjs/LoggerService.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,aAAa,IAAI,iBAAiB,EAAE,MAAM,gBAAgB,CAAA;AACxE,OAAO,EAAE,WAAW,EAAE,MAAM,gBAAgB,CAAA;AAE5C,qBAAa,aAAa;IACxB,OAAO,CAAC,MAAM,CAAa;gBAEf,UAAU,CAAC,EAAE,WAAW;IAKpC,MAAM,CAAC,UAAU,CAAC,MAAM,EAAE,MAAM,EAAE,QAAQ,GAAE,OAAO,aAA6B;;;;IAOhF;;OAEG;IACH,OAAO,IAAI,iBAAiB;IAoB5B,UAAU,CAAC,MAAM,EAAE,MAAM,EAAE,QAAQ,GAAE,OAAO,aAA6B;IAIzE,WAAW,CAAC,OAAO,EAAE,MAAM,EAAE,QAAQ,GAAE,OAAO,aAA6B;IAI3E,GAAG,CAAC,GAAG,EAAE,MAAM,EAAE,GAAG,KAAK,EAAE,OAAO,EAAE;IAIpC,KAAK,CAAC,GAAG,EAAE,MAAM,EAAE,GAAG,KAAK,EAAE,OAAO,EAAE;IAItC,IAAI,CAAC,GAAG,EAAE,MAAM,EAAE,GAAG,KAAK,EAAE,OAAO,EAAE;IAIrC,KAAK,CAAC,GAAG,EAAE,MAAM,EAAE,GAAG,KAAK,EAAE,OAAO,EAAE;IAItC,OAAO,CAAC,GAAG,EAAE,MAAM,EAAE,GAAG,KAAK,EAAE,OAAO,EAAE;CAGzC"}
@@ -0,0 +1,6 @@
1
+ import { type Logger } from "pino";
2
+ /**
3
+ * export the main logger instance
4
+ */
5
+ export declare const getLogger: () => Logger;
6
+ //# sourceMappingURL=pino.instance.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"pino.instance.d.ts","sourceRoot":"","sources":["../src/pino.instance.ts"],"names":[],"mappings":"AAAA,OAAa,EAAkD,KAAK,MAAM,EAAE,MAAM,MAAM,CAAA;AAoDxF;;GAEG;AACH,eAAO,MAAM,SAAS,cAAmB,CAAA"}
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2024 we-are-singular
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,70 @@
1
+ # @we-are-singular/logger
2
+
3
+ [![npm version](https://badge.fury.io/js/%40we-are-singular%2Flogger.svg)](https://badge.fury.io/js/%40we-are-singular%2Flogger)
4
+
5
+ This package provides a shared ESLint configuration for Singular projects.
6
+
7
+ ## Installation
8
+
9
+ ```bash
10
+ npm install --save-dev @we-are-singular/logger
11
+ ```
12
+
13
+ ## Usage
14
+
15
+ ```typescript
16
+ import { LoggerClass } from "@we-are-singular/logger"
17
+
18
+ const logger = new LoggerClass("MyApp", "Module", "Context")
19
+ logger.info("Hello, world!")
20
+ logger.error("Something went wrong!")
21
+ logger.withModule("AnotherModule").info("Hello, world!")
22
+ ```
23
+
24
+ ## NestJS
25
+
26
+ override default logger:
27
+
28
+ ```typescript
29
+ import { LoggerService } from "@we-are-singular/logger"
30
+
31
+ // use forRoot() In main.ts
32
+ const app = await NestFactory.createApplicationContext(AppModule, {
33
+ logger: new LoggerService().withModule("AppModule").forRoot(),
34
+ })
35
+ ```
36
+
37
+ auto context from module provider:
38
+
39
+ ```typescript
40
+ // use forFeature() In other modules
41
+ @Module({
42
+ imports: [],
43
+ controllers: [],
44
+ providers: [
45
+ //
46
+ LoggerService.forFeature("Services"),
47
+ ],
48
+ exports: [
49
+ //
50
+ LoggerService,
51
+ ],
52
+ })
53
+ export class ServicesModule {}
54
+ ```
55
+
56
+ auto context:
57
+
58
+ ```typescript
59
+ // as a base class, adding a logger to all classes with a context of the class name
60
+ @Injectable()
61
+ export abstract class BaseClass {
62
+ readonly logger: LoggerService
63
+ constructor(
64
+ //
65
+ @Inject(LoggerService) logger: LoggerService
66
+ ) {
67
+ this.logger = logger.withContext(this.constructor.name.toString())
68
+ }
69
+ }
70
+ ```
package/package.json ADDED
@@ -0,0 +1,68 @@
1
+ {
2
+ "name": "@we-are-singular/logger",
3
+ "version": "1.0.0",
4
+ "description": "A simple pinojs wrapper",
5
+ "type": "module",
6
+ "types": ".build/index.d.ts",
7
+ "files": [
8
+ ".build/**/*"
9
+ ],
10
+ "exports": {
11
+ ".": {
12
+ "import": "./.build/index.mjs",
13
+ "require": "./.build/index.cjs"
14
+ }
15
+ },
16
+ "scripts": {
17
+ "format": "prettier --write \"src/**/*.ts\" \"test/**/*.ts\"",
18
+ "build": "rollup -c",
19
+ "release": "release-it",
20
+ "lint": "tsc --noEmit && TIMING=1 eslint --color --ext .cjs,.mjs,.js,.ts,.md,.json,.yml,yaml ./"
21
+ },
22
+ "repository": {
23
+ "type": "git",
24
+ "url": "git+https://github.com/we-are-singular/logger.git"
25
+ },
26
+ "author": "singular",
27
+ "license": "MIT",
28
+ "homepage": "https://github.com/we-are-singular/logger#readme",
29
+ "bugs": {
30
+ "url": "https://github.com/we-are-singular/logger/issues"
31
+ },
32
+ "devDependencies": {
33
+ "@nestjs/common": "^10.3.5",
34
+ "@rollup/plugin-typescript": "^11.1.6",
35
+ "@we-are-singular/eslint-config": "^1.2.1",
36
+ "i": "^0.3.7",
37
+ "prettier": "^3.2.5",
38
+ "release-it": "^17.1.1",
39
+ "rollup": "^4.13.0"
40
+ },
41
+ "dependencies": {
42
+ "pino": "^8.19.0",
43
+ "pino-pretty": "^11.0.0"
44
+ },
45
+ "peerDependencies": {
46
+ "pino": "^8.19.0",
47
+ "pino-pretty": "^11.0.0"
48
+ },
49
+ "release-it": {
50
+ "hooks": {
51
+ "before:init": [
52
+ "git pull -r",
53
+ "npm run build"
54
+ ],
55
+ "after:bump": "npx auto-changelog -p"
56
+ },
57
+ "git": {
58
+ "requireBranch": "main",
59
+ "commitMessage": "Release: ${version}"
60
+ },
61
+ "github": {
62
+ "release": true
63
+ },
64
+ "npm": {
65
+ "publish": true
66
+ }
67
+ }
68
+ }