@rettersoft/retter-log
Advanced tools
Comparing version
@@ -1,1 +0,14 @@ | ||
export {}; | ||
export default class RetterLog { | ||
private static readonly kinesis; | ||
/** | ||
* Logs the request to the stream. | ||
* | ||
* @param request - The request that is going to be logged | ||
* @param response - The response of the request that is going to be logged | ||
* @param tag - The type of the request | ||
* @param host - The host of the log | ||
* @param [endpoint] - The endpoint of the log | ||
* @returns The given response | ||
*/ | ||
static LogRequest(request: any, response: any, tag: string, host: string, endpoint?: string): Promise<any>; | ||
} |
@@ -6,59 +6,36 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const aws_sdk_1 = __importDefault(require("aws-sdk")); | ||
const { RETTERLOG_KINESIS } = process.env; | ||
const aws_sdk_1 = __importDefault(require("aws-sdk")); | ||
var Kinesis = new aws_sdk_1.default.Kinesis(); | ||
var messages = []; | ||
const origlog = console.log; | ||
function overrideConsoleLog() { | ||
console.log = function (...args) { | ||
const message = arguments[0]; | ||
if (Object.prototype.toString.call(message) === "[object Object]" && message.type && message.type.toLowerCase() === "retterlog") { | ||
if (!message.log) { | ||
origlog("Received empty retterLog"); | ||
} | ||
else if (!message.host) { | ||
origlog("Please specify host information inside message"); | ||
} | ||
else if (!RETTERLOG_KINESIS) { | ||
origlog("Please specify RETTERLOG_KINESIS as environment variable"); | ||
} | ||
else { | ||
delete message.type; | ||
messages.push(Kinesis.putRecord({ | ||
StreamName: RETTERLOG_KINESIS, | ||
PartitionKey: Date.now().toString(), | ||
Data: JSON.stringify({ ...message }), | ||
}).promise()); | ||
} | ||
class RetterLog { | ||
/** | ||
* Logs the request to the stream. | ||
* | ||
* @param request - The request that is going to be logged | ||
* @param response - The response of the request that is going to be logged | ||
* @param tag - The type of the request | ||
* @param host - The host of the log | ||
* @param [endpoint] - The endpoint of the log | ||
* @returns The given response | ||
*/ | ||
static async LogRequest(request, response, tag, host, endpoint) { | ||
if (!RETTERLOG_KINESIS) { | ||
console.log("Please specify RETTERLOG_KINESIS as environment variable"); | ||
} | ||
else { | ||
origlog(args); | ||
await RetterLog.kinesis.putRecord({ | ||
StreamName: RETTERLOG_KINESIS, | ||
PartitionKey: Date.now().toString(), | ||
Data: JSON.stringify({ | ||
request, | ||
response, | ||
tag, | ||
host, | ||
endpoint | ||
}) | ||
}).promise(); | ||
} | ||
}; | ||
return response; | ||
} | ||
} | ||
async function allSettled(promises) { | ||
await Promise.all(promises.map((promise, i) => promise | ||
.then(value => { | ||
console.log("fulfilled"); | ||
return { | ||
status: "fulfilled", | ||
value, | ||
}; | ||
}) | ||
.catch(reason => { | ||
console.log("rejected"); | ||
return { | ||
status: "rejected", | ||
reason, | ||
}; | ||
}))); | ||
} | ||
async function onExit() { | ||
await allSettled(messages); | ||
process.exit(); | ||
} | ||
overrideConsoleLog(); | ||
[ | ||
'beforeExit', 'uncaughtException', 'unhandledRejection', | ||
'SIGTERM', | ||
].forEach(evt => process.on(evt, onExit)); | ||
exports.default = RetterLog; | ||
RetterLog.kinesis = new aws_sdk_1.default.Kinesis(); |
97
index.ts
@@ -0,74 +1,41 @@ | ||
import AWS from "aws-sdk"; | ||
const {RETTERLOG_KINESIS} = process.env; | ||
import { PromiseResult } from "aws-sdk/lib/request"; | ||
import { PutRecordsRequestEntryList } from "aws-sdk/clients/kinesis"; | ||
import AWS from "aws-sdk"; | ||
export default class RetterLog{ | ||
private static readonly kinesis = new AWS.Kinesis() | ||
var Kinesis = new AWS.Kinesis() | ||
/** | ||
* Logs the request to the stream. | ||
* | ||
* @param request - The request that is going to be logged | ||
* @param response - The response of the request that is going to be logged | ||
* @param tag - The type of the request | ||
* @param host - The host of the log | ||
* @param [endpoint] - The endpoint of the log | ||
* @returns The given response | ||
*/ | ||
public static async LogRequest(request: any, response: any, tag: string, host: string, endpoint?:string){ | ||
var messages : Promise<PromiseResult<AWS.Kinesis.Types.PutRecordOutput, AWS.AWSError>>[] = [] | ||
const origlog = console.log; | ||
function overrideConsoleLog(){ | ||
console.log = function(...args: any) { | ||
const message = arguments[0]; | ||
if(Object.prototype.toString.call(message) === "[object Object]" && message.type && message.type.toLowerCase() === "retterlog"){ | ||
if(!message.log){ | ||
origlog("Received empty retterLog"); | ||
} | ||
else if(!message.host){ | ||
origlog("Please specify host information inside message"); | ||
} | ||
else if(!RETTERLOG_KINESIS){ | ||
origlog("Please specify RETTERLOG_KINESIS as environment variable"); | ||
} | ||
else{ | ||
delete message.type; | ||
messages.push( | ||
Kinesis.putRecord({ | ||
StreamName: RETTERLOG_KINESIS!, | ||
PartitionKey: Date.now().toString(), | ||
Data: JSON.stringify({...message} ), | ||
}).promise() | ||
) | ||
} | ||
if(!RETTERLOG_KINESIS){ | ||
console.log("Please specify RETTERLOG_KINESIS as environment variable"); | ||
} | ||
else{ | ||
origlog(args); | ||
await RetterLog.kinesis.putRecord({ | ||
StreamName: RETTERLOG_KINESIS, | ||
PartitionKey: Date.now().toString(), | ||
Data: JSON.stringify( | ||
{ | ||
request, | ||
response, | ||
tag, | ||
host, | ||
endpoint | ||
} | ||
) | ||
}).promise(); | ||
} | ||
}; | ||
} | ||
return response | ||
} | ||
async function allSettled (promises: Promise<PromiseResult<AWS.Kinesis.Types.PutRecordOutput, AWS.AWSError>>[]) { | ||
await Promise.all( | ||
promises.map((promise, i) => | ||
promise | ||
.then(value => { | ||
console.log("fulfilled") | ||
return { | ||
status: "fulfilled", | ||
value, | ||
}}) | ||
.catch(reason => | ||
{ | ||
console.log("rejected") | ||
return { | ||
status: "rejected", | ||
reason, | ||
}}) | ||
) | ||
); | ||
} | ||
async function onExit(){ | ||
await allSettled(messages) | ||
process.exit(); | ||
} | ||
overrideConsoleLog(); | ||
[ | ||
'beforeExit', 'uncaughtException', 'unhandledRejection', | ||
'SIGTERM', | ||
].forEach(evt => process.on(evt, onExit)); | ||
} |
{ | ||
"name": "@rettersoft/retter-log", | ||
"version": "1.2.5", | ||
"version": "2.0.0", | ||
"description": "Streams logs with certain format", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.js", |
3974
-19.33%99
-28.78%