powr-sdk-api
Version:
Shared API core library for PowrStack projects
78 lines (75 loc) • 1.92 kB
JavaScript
;
const winston = require('winston');
const {
S3Client,
PutObjectCommand
} = require('@aws-sdk/client-s3');
// Create S3 client
const s3Client = new S3Client({
region: process.env.AWS_REGION,
credentials: {
accessKeyId: process.env.AWS_ACCESS_KEY_ID,
secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY
}
});
/**
* Custom Winston transport for logging to S3
*/
class S3Transport 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 key = `${this.prefix}/${date}/${Date.now()}.json`;
try {
const command = new PutObjectCommand({
Bucket: this.bucket,
Key: key,
Body: JSON.stringify(logs),
ContentType: 'application/json'
});
await s3Client.send(command);
} catch (error) {
console.error('Failed to write logs to S3:', {
error: error.message,
code: error.code,
bucket: this.bucket,
key: key,
region: process.env.AWS_REGION,
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 = {
S3Transport
};