powr-sdk-api
Version:
Shared API core library for PowrStack projects
61 lines (58 loc) • 1.51 kB
JavaScript
"use strict";
/**
* S3 Transport for Winston logger
*/
const winston = require('winston');
const {
S3Client,
PutObjectCommand
} = require('@aws-sdk/client-s3');
/**
* 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 this.s3Client.send(command);
} catch (error) {
console.error('Error flushing logs to S3:', error);
// Put logs back in buffer
this.buffer.unshift(...logs);
}
}
log(info, callback) {
setImmediate(() => {
this.emit('logged', info);
});
this.buffer.push(info);
if (this.buffer.length >= this.bufferSize) {
this.flush();
}
callback();
}
}
module.exports = S3Transport;