powr-sdk-api 1.4.3 → 1.5.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/dist/logger/gcs.js +78 -0
- package/dist/logger/index.js +24 -11
- package/package.json +1 -1
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
const winston = require('winston');
|
|
4
|
+
const {
|
|
5
|
+
Storage
|
|
6
|
+
} = require('@google-cloud/storage');
|
|
7
|
+
|
|
8
|
+
// Create GCS client using environment variables for credentials
|
|
9
|
+
const storage = new Storage({
|
|
10
|
+
projectId: process.env.GCS_PROJECT_ID,
|
|
11
|
+
credentials: {
|
|
12
|
+
client_email: process.env.GCS_CLIENT_EMAIL,
|
|
13
|
+
private_key: process.env.GCS_PRIVATE_KEY ? process.env.GCS_PRIVATE_KEY.replace(/\\n/g, '\n') : undefined
|
|
14
|
+
}
|
|
15
|
+
});
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Custom Winston transport for logging to Google Cloud Storage
|
|
19
|
+
*/
|
|
20
|
+
class GCSTransport extends winston.Transport {
|
|
21
|
+
constructor(opts) {
|
|
22
|
+
super(opts);
|
|
23
|
+
this.bucket = opts.bucket;
|
|
24
|
+
this.prefix = opts.prefix || '';
|
|
25
|
+
this.buffer = [];
|
|
26
|
+
this.bufferSize = opts.bufferSize || 100;
|
|
27
|
+
this.flushInterval = opts.flushInterval || 5000;
|
|
28
|
+
this.setupFlushInterval();
|
|
29
|
+
}
|
|
30
|
+
setupFlushInterval() {
|
|
31
|
+
setInterval(() => {
|
|
32
|
+
this.flush();
|
|
33
|
+
}, this.flushInterval);
|
|
34
|
+
}
|
|
35
|
+
async flush() {
|
|
36
|
+
if (this.buffer.length === 0) return;
|
|
37
|
+
const logs = this.buffer.splice(0, this.buffer.length);
|
|
38
|
+
const date = new Date().toISOString().split('T')[0];
|
|
39
|
+
const filename = `${this.prefix}/${date}/${Date.now()}.json`;
|
|
40
|
+
try {
|
|
41
|
+
const bucket = storage.bucket(this.bucket);
|
|
42
|
+
const file = bucket.file(filename);
|
|
43
|
+
await file.save(JSON.stringify(logs), {
|
|
44
|
+
contentType: 'application/json',
|
|
45
|
+
metadata: {
|
|
46
|
+
contentType: 'application/json'
|
|
47
|
+
}
|
|
48
|
+
});
|
|
49
|
+
} catch (error) {
|
|
50
|
+
console.error('Failed to write logs to Google Cloud Storage:', {
|
|
51
|
+
error: error.message,
|
|
52
|
+
code: error.code,
|
|
53
|
+
bucket: this.bucket,
|
|
54
|
+
filename: filename,
|
|
55
|
+
projectId: process.env.GCS_PROJECT_ID,
|
|
56
|
+
stack: error.stack
|
|
57
|
+
});
|
|
58
|
+
// Put the logs back in the buffer
|
|
59
|
+
this.buffer.unshift(...logs);
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
log(info, callback) {
|
|
63
|
+
setImmediate(() => {
|
|
64
|
+
this.emit('logged', info);
|
|
65
|
+
});
|
|
66
|
+
this.buffer.push({
|
|
67
|
+
timestamp: new Date().toISOString(),
|
|
68
|
+
...info
|
|
69
|
+
});
|
|
70
|
+
if (this.buffer.length >= this.bufferSize) {
|
|
71
|
+
this.flush();
|
|
72
|
+
}
|
|
73
|
+
callback();
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
module.exports = {
|
|
77
|
+
GCSTransport
|
|
78
|
+
};
|
package/dist/logger/index.js
CHANGED
|
@@ -1,9 +1,12 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
|
|
3
|
-
const winston = require(
|
|
3
|
+
const winston = require("winston");
|
|
4
4
|
const {
|
|
5
5
|
S3Transport
|
|
6
|
-
} = require(
|
|
6
|
+
} = require("./s3");
|
|
7
|
+
const {
|
|
8
|
+
GCSTransport
|
|
9
|
+
} = require("./gcs");
|
|
7
10
|
|
|
8
11
|
// Custom format for logs
|
|
9
12
|
const logFormat = winston.format.combine(winston.format.timestamp(), winston.format.errors({
|
|
@@ -13,19 +16,29 @@ const logFormat = winston.format.combine(winston.format.timestamp(), winston.for
|
|
|
13
16
|
// Create base transports array with console transport
|
|
14
17
|
const transports = [new winston.transports.Console()];
|
|
15
18
|
|
|
16
|
-
// Add S3 transport only in production
|
|
17
|
-
if (process.env.NODE_ENV ===
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
19
|
+
// Add S3 transport only in production and if enabled
|
|
20
|
+
if (process.env.NODE_ENV === "production") {
|
|
21
|
+
if (process.env.LOG_DESTINATION === "s3") {
|
|
22
|
+
transports.push(new S3Transport({
|
|
23
|
+
bucket: process.env.LOG_BUCKET_NAME,
|
|
24
|
+
prefix: "logs",
|
|
25
|
+
bufferSize: 100,
|
|
26
|
+
flushInterval: 5000
|
|
27
|
+
}));
|
|
28
|
+
}
|
|
29
|
+
if (process.env.LOG_DESTINATION === "gcs") {
|
|
30
|
+
transports.push(new GCSTransport({
|
|
31
|
+
bucket: process.env.LOG_BUCKET_NAME,
|
|
32
|
+
prefix: "logs",
|
|
33
|
+
bufferSize: 100,
|
|
34
|
+
flushInterval: 5000
|
|
35
|
+
}));
|
|
36
|
+
}
|
|
24
37
|
}
|
|
25
38
|
|
|
26
39
|
// Create the logger
|
|
27
40
|
const logger = winston.createLogger({
|
|
28
|
-
level: process.env.LOG_LEVEL ||
|
|
41
|
+
level: process.env.LOG_LEVEL || "info",
|
|
29
42
|
format: logFormat,
|
|
30
43
|
defaultMeta: {
|
|
31
44
|
service: process.env.LOG_SERVICE_NAME
|