powr-sdk-api
Version:
Shared API core library for PowrStack projects
78 lines (75 loc) • 2.11 kB
JavaScript
;
const winston = require('winston');
const {
Storage
} = require('@google-cloud/storage');
// Create GCS client using environment variables for credentials
const storage = new Storage({
projectId: process.env.GCS_PROJECT_ID,
credentials: {
client_email: process.env.GCS_CLIENT_EMAIL,
private_key: process.env.GCS_PRIVATE_KEY ? process.env.GCS_PRIVATE_KEY.replace(/\\n/g, '\n') : undefined
}
});
/**
* Custom Winston transport for logging to Google Cloud Storage
*/
class GCSTransport extends winston.Transport {
constructor(opts) {
super(opts);
this.bucket = opts.bucket;
this.prefix = opts.prefix || '';
this.buffer = [];
this.bufferSize = opts.bufferSize || 100;
this.flushInterval = opts.flushInterval || 5000;
this.setupFlushInterval();
}
setupFlushInterval() {
setInterval(() => {
this.flush();
}, this.flushInterval);
}
async flush() {
if (this.buffer.length === 0) return;
const logs = this.buffer.splice(0, this.buffer.length);
const date = new Date().toISOString().split('T')[0];
const filename = `${this.prefix}/${date}/${Date.now()}.json`;
try {
const bucket = storage.bucket(this.bucket);
const file = bucket.file(filename);
await file.save(JSON.stringify(logs), {
contentType: 'application/json',
metadata: {
contentType: 'application/json'
}
});
} catch (error) {
console.error('Failed to write logs to Google Cloud Storage:', {
error: error.message,
code: error.code,
bucket: this.bucket,
filename: filename,
projectId: process.env.GCS_PROJECT_ID,
stack: error.stack
});
// Put the logs back in the buffer
this.buffer.unshift(...logs);
}
}
log(info, callback) {
setImmediate(() => {
this.emit('logged', info);
});
this.buffer.push({
timestamp: new Date().toISOString(),
...info
});
if (this.buffer.length >= this.bufferSize) {
this.flush();
}
callback();
}
}
module.exports = {
GCSTransport
};