@devopsplaybook.io/otel-utils 1.0.2 → 1.0.3-beta1
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/dist/src/ModuleLogger.d.ts +4 -3
- package/dist/src/ModuleLogger.js +26 -16
- package/package.json +1 -1
- package/src/ModuleLogger.ts +32 -17
|
@@ -1,10 +1,11 @@
|
|
|
1
1
|
import { StandardLoggerInterface } from "./models/StandardLoggerInterface";
|
|
2
|
+
import { Span } from "@opentelemetry/sdk-trace-base";
|
|
2
3
|
export declare class ModuleLogger {
|
|
3
4
|
private module;
|
|
4
5
|
private standardLogger?;
|
|
5
6
|
constructor(module: string, standardLogger: StandardLoggerInterface);
|
|
6
|
-
info(message: Error | string | any): void;
|
|
7
|
-
warn(message: Error | string | any): void;
|
|
8
|
-
error(message: Error | string | any): void;
|
|
7
|
+
info(message: Error | string | any, context?: Span): void;
|
|
8
|
+
warn(message: Error | string | any, context?: Span): void;
|
|
9
|
+
error(message: Error | string | any, context?: Span): void;
|
|
9
10
|
private display;
|
|
10
11
|
}
|
package/dist/src/ModuleLogger.js
CHANGED
|
@@ -8,43 +8,53 @@ class ModuleLogger {
|
|
|
8
8
|
this.standardLogger = standardLogger;
|
|
9
9
|
}
|
|
10
10
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
11
|
-
info(message) {
|
|
12
|
-
this.display("info", message, api_logs_1.SeverityNumber.
|
|
11
|
+
info(message, context) {
|
|
12
|
+
this.display("info", message, api_logs_1.SeverityNumber.INFO, context);
|
|
13
13
|
}
|
|
14
14
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
15
|
-
warn(message) {
|
|
16
|
-
this.display("warn", message, api_logs_1.SeverityNumber.WARN);
|
|
15
|
+
warn(message, context) {
|
|
16
|
+
this.display("warn", message, api_logs_1.SeverityNumber.WARN, context);
|
|
17
17
|
}
|
|
18
18
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
19
|
-
error(message) {
|
|
20
|
-
this.display("error", message, api_logs_1.SeverityNumber.ERROR);
|
|
19
|
+
error(message, context) {
|
|
20
|
+
this.display("error", message, api_logs_1.SeverityNumber.ERROR, context);
|
|
21
21
|
}
|
|
22
22
|
display(level,
|
|
23
23
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
24
|
-
message, severityNumber = api_logs_1.SeverityNumber.INFO) {
|
|
24
|
+
message, severityNumber = api_logs_1.SeverityNumber.INFO, context) {
|
|
25
25
|
var _a, _b;
|
|
26
|
+
let formattedMessage = "";
|
|
27
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
28
|
+
const attributes = { "log.type": "custom" };
|
|
26
29
|
if (typeof message === "string") {
|
|
27
|
-
|
|
28
|
-
console.log(`[${level}] [${this.module}] ${message}`);
|
|
30
|
+
formattedMessage = message;
|
|
29
31
|
}
|
|
30
32
|
else if (message instanceof Error) {
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
33
|
+
formattedMessage = message.message;
|
|
34
|
+
attributes["exception.type"] = message.name;
|
|
35
|
+
attributes["exception.message"] = message.message;
|
|
36
|
+
attributes["exception.stacktrace"] = message.stack;
|
|
34
37
|
console.log(message.stack);
|
|
35
38
|
}
|
|
36
39
|
else if (typeof message === "object") {
|
|
37
|
-
|
|
38
|
-
console.log(`${level} [${this.module}] ${JSON.stringify(message)}`);
|
|
40
|
+
formattedMessage = JSON.stringify(message);
|
|
39
41
|
}
|
|
42
|
+
if (context) {
|
|
43
|
+
const spanCtx = context.spanContext();
|
|
44
|
+
if (spanCtx) {
|
|
45
|
+
attributes["span.id"] = spanCtx.spanId;
|
|
46
|
+
attributes["trace.id"] = spanCtx.traceId;
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
console.log(`[${level}] [${this.module}] ${formattedMessage}`);
|
|
40
50
|
if (!((_a = this.standardLogger) === null || _a === void 0 ? void 0 : _a.getLogger())) {
|
|
41
51
|
return;
|
|
42
52
|
}
|
|
43
53
|
(_b = this.standardLogger.getLogger()) === null || _b === void 0 ? void 0 : _b.emit({
|
|
44
54
|
severityNumber,
|
|
45
55
|
severityText: level,
|
|
46
|
-
body:
|
|
47
|
-
attributes
|
|
56
|
+
body: `[${this.module}] ${formattedMessage}`,
|
|
57
|
+
attributes,
|
|
48
58
|
});
|
|
49
59
|
}
|
|
50
60
|
}
|
package/package.json
CHANGED
package/src/ModuleLogger.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { SeverityNumber } from "@opentelemetry/api-logs";
|
|
2
2
|
import { StandardLoggerInterface } from "./models/StandardLoggerInterface";
|
|
3
|
+
import { Span } from "@opentelemetry/sdk-trace-base";
|
|
3
4
|
|
|
4
5
|
export class ModuleLogger {
|
|
5
6
|
private module: string;
|
|
@@ -11,44 +12,58 @@ export class ModuleLogger {
|
|
|
11
12
|
}
|
|
12
13
|
|
|
13
14
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
14
|
-
public info(message: Error | string | any): void {
|
|
15
|
-
this.display("info", message, SeverityNumber.
|
|
15
|
+
public info(message: Error | string | any, context?: Span): void {
|
|
16
|
+
this.display("info", message, SeverityNumber.INFO, context);
|
|
16
17
|
}
|
|
17
18
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
18
|
-
public warn(message: Error | string | any): void {
|
|
19
|
-
this.display("warn", message, SeverityNumber.WARN);
|
|
19
|
+
public warn(message: Error | string | any, context?: Span): void {
|
|
20
|
+
this.display("warn", message, SeverityNumber.WARN, context);
|
|
20
21
|
}
|
|
21
22
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
22
|
-
public error(message: Error | string | any): void {
|
|
23
|
-
this.display("error", message, SeverityNumber.ERROR);
|
|
23
|
+
public error(message: Error | string | any, context?: Span): void {
|
|
24
|
+
this.display("error", message, SeverityNumber.ERROR, context);
|
|
24
25
|
}
|
|
25
26
|
|
|
26
27
|
private display(
|
|
27
28
|
level: string,
|
|
28
29
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
29
|
-
message: any,
|
|
30
|
-
severityNumber = SeverityNumber.INFO
|
|
30
|
+
message: string | Error | any,
|
|
31
|
+
severityNumber = SeverityNumber.INFO,
|
|
32
|
+
context?: Span
|
|
31
33
|
): void {
|
|
34
|
+
let formattedMessage = "";
|
|
35
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
36
|
+
const attributes: Record<string, any> = { "log.type": "custom" };
|
|
37
|
+
|
|
32
38
|
if (typeof message === "string") {
|
|
33
|
-
|
|
34
|
-
console.log(`[${level}] [${this.module}] ${message}`);
|
|
39
|
+
formattedMessage = message;
|
|
35
40
|
} else if (message instanceof Error) {
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
41
|
+
formattedMessage = message.message;
|
|
42
|
+
attributes["exception.type"] = message.name;
|
|
43
|
+
attributes["exception.message"] = message.message;
|
|
44
|
+
attributes["exception.stacktrace"] = message.stack;
|
|
39
45
|
console.log((message as Error).stack);
|
|
40
46
|
} else if (typeof message === "object") {
|
|
41
|
-
|
|
42
|
-
console.log(`${level} [${this.module}] ${JSON.stringify(message)}`);
|
|
47
|
+
formattedMessage = JSON.stringify(message);
|
|
43
48
|
}
|
|
49
|
+
|
|
50
|
+
if (context) {
|
|
51
|
+
const spanCtx = context.spanContext();
|
|
52
|
+
if (spanCtx) {
|
|
53
|
+
attributes["span.id"] = spanCtx.spanId;
|
|
54
|
+
attributes["trace.id"] = spanCtx.traceId;
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
console.log(`[${level}] [${this.module}] ${formattedMessage}`);
|
|
44
59
|
if (!this.standardLogger?.getLogger()) {
|
|
45
60
|
return;
|
|
46
61
|
}
|
|
47
62
|
this.standardLogger.getLogger()?.emit({
|
|
48
63
|
severityNumber,
|
|
49
64
|
severityText: level,
|
|
50
|
-
body:
|
|
51
|
-
attributes
|
|
65
|
+
body: `[${this.module}] ${formattedMessage}`,
|
|
66
|
+
attributes,
|
|
52
67
|
});
|
|
53
68
|
}
|
|
54
69
|
}
|