timer-logs
Advanced tools
Comparing version
{ | ||
"name": "timer-logs", | ||
"version": "1.0.0", | ||
"version": "1.0.1", | ||
"devDependencies": { | ||
@@ -12,3 +12,4 @@ "@types/node": "^" | ||
}, | ||
"license": "MIT" | ||
"license": "MIT", | ||
"types": "./Timer.d.ts" | ||
} |
88
Timer.js
"use strict"; | ||
//todo: | ||
// - add the option of adding metadata to the log after its been started (details setter) | ||
// - make 'class' a built-in, required param in the constructor (force) | ||
// - add error handling methods, such as handlePostgresError(e) | ||
// this should really be in every single interface method, from the top to the return | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.Timer = void 0; | ||
class Timer { | ||
/** | ||
* Create a new Timer object. Can have multiple timers within this object. | ||
* Should only have one of these per file | ||
* @param config optional configuration object with message and severity | ||
*/ | ||
constructor(config) { | ||
@@ -20,4 +10,2 @@ var _a, _b; | ||
this.config.details = (_a = config === null || config === void 0 ? void 0 : config.details) !== null && _a !== void 0 ? _a : {}; | ||
// TODO: split filename by '/' and save each item as its own key:value in the log. | ||
// - such as ./interface/getCompanyProfile.ts becomes {level1:'interface', level2:'getCompanyProfile.ts'} | ||
this.savedTimes = {}; | ||
@@ -27,7 +15,2 @@ if (((_b = this.config) === null || _b === void 0 ? void 0 : _b.label) !== undefined) | ||
} | ||
/** | ||
* Start a new timer | ||
* @param label the label of the timer. this will be console logged on flush() | ||
* @return object which can be used to stop the timer without its label | ||
*/ | ||
start(label) { | ||
@@ -39,5 +22,2 @@ console.assert(!this.savedTimes.hasOwnProperty(label), 'Timer started more than once for same label'); | ||
this.savedTimes[label] = { startTime: Date.now() }; | ||
/** | ||
* Stops the timer and saves the time taken | ||
*/ | ||
const _stop = () => { | ||
@@ -50,6 +30,2 @@ return this.stop(label); | ||
} | ||
/** | ||
* Stops a timer and saves the time taken | ||
* @param label the label of the timer you wish to stop | ||
*/ | ||
stop(label) { | ||
@@ -62,14 +38,2 @@ console.assert(this.savedTimes.hasOwnProperty(label), 'Timer stopped for unstarted label. Missing timer.start()'); | ||
} | ||
/** | ||
* Stops the most recently started timer, and starts a new one | ||
* @param label for new timer started | ||
* @example | ||
* timer.start('label1') | ||
* await new Promise() | ||
* timer.next('label2') | ||
* await new Promise() | ||
* timer.next('label3') | ||
* await new Promise() | ||
* timer.end() | ||
*/ | ||
next(label) { | ||
@@ -79,5 +43,2 @@ this.stop(this.mostRecentlyStartedLabel); | ||
} | ||
/** | ||
* stops the most recently started timer | ||
*/ | ||
end() { | ||
@@ -87,7 +48,2 @@ if (this.mostRecentlyStartedLabel) | ||
} | ||
/** | ||
* prints times to the console in JSON format for Google Cloud Logging. | ||
* | ||
* Will end the most recently started timer if not already ended | ||
*/ | ||
flush() { | ||
@@ -113,8 +69,2 @@ var _a, _b, _c, _d, _e, _f; | ||
} | ||
/** | ||
* Adds a detail to the JSON of the log. | ||
* | ||
* @param key the key to log in the JSON | ||
* @param value (optional) value for the key. Defaults to true | ||
*/ | ||
addDetail(key, value = true) { | ||
@@ -124,7 +74,2 @@ var _a; | ||
} | ||
/** | ||
* Adds multiple details to the JSON of the log. | ||
* | ||
* @param details an object of key value pairs to log | ||
*/ | ||
addDetails(details) { | ||
@@ -134,12 +79,5 @@ var _a; | ||
} | ||
/** | ||
* Returns the time elapsed since creating this timer in milliseconds | ||
*/ | ||
getTimeUntilNow() { | ||
return Date.now() - this.startTime; | ||
} | ||
/** | ||
* Logs a custom error message in a separate log to the main Timer | ||
* @param message the string to log | ||
*/ | ||
customError(message) { | ||
@@ -154,11 +92,2 @@ var _a; | ||
} | ||
/** | ||
* Logs a postgres error message in a separate log to the main Timer. | ||
* | ||
* @param e the error object returned by postgres client | ||
* | ||
* @example | ||
* const { rows } = await pool.query('SELECT NOW()',[]) | ||
* .catch(e=>timer.postgresError(e)) | ||
*/ | ||
postgresError(e) { | ||
@@ -176,19 +105,2 @@ var _a; | ||
} | ||
/** | ||
* Logs a generic error in a separate log to the main Timer. | ||
* | ||
* @param e the error that has been thrown | ||
* @param message an optional custom message giving context to the error | ||
* This can be called after any catching any error, like this: | ||
* @example | ||
* try{ | ||
* // code that could throw an error | ||
* }catch(e){ | ||
* timer.genericError(e) | ||
* } | ||
* @example | ||
* await asynchronousFunction() | ||
* .then() | ||
* .catch(timer.genericError) | ||
*/ | ||
genericError(e, message) { | ||
@@ -195,0 +107,0 @@ var _a; |
@@ -23,3 +23,3 @@ //todo: | ||
} | ||
export class Timer { | ||
export default class Timer { | ||
private readonly startTime: number | ||
@@ -26,0 +26,0 @@ private finishTime: number |
@@ -6,3 +6,3 @@ { | ||
"sourceMap": false, | ||
"removeComments": false | ||
"removeComments": true | ||
}, | ||
@@ -9,0 +9,0 @@ "exclude": [ |
18307
6.47%7
16.67%482
12.35%