Security News
Fluent Assertions Faces Backlash After Abandoning Open Source Licensing
Fluent Assertions is facing backlash after dropping the Apache license for a commercial model, leaving users blindsided and questioning contributor rights.
timer-logs
Advanced tools
A minimalist NodeJS library, written in TypeScript, for logging code execution time and errors.
A minimalist NodeJS library, written in TypeScript, for logging code execution time and errors.
Designed to work well with Google Cloud logging, but useful for any application.
Fully documented with JSDoc(in .d.ts
) and examples. Includes full typescript support.
npm install timer-logs
import Timer from 'timer-logs'
const webservice = async () => {
const timer = new Timer({filename: 'webservice.ts'})
timer.start('operation 1')
await sleep(1000) // perform operation 1
timer.next('operation 2')
await sleep(2000) // perform operation 2
timer.next('operation 3')
await sleep(3000) // perform operation 2
timer.flush()
}
Output:
{
"severity": "INFO",
"message": "Timer: 6025ms",
"filename": "webservice.ts",
"operation 1": 1010,
"operation 2": 2000,
"operation 3": 3014
}
You can specify more options when constructing the Timer
object to customise the log output.
const timer = new Timer({
filename: 'webservice.ts',
label: 'Demo timer',
details: {id: 'c69adf0e7ff8fddf8a93'},
severity: 'ERROR'
})
{
"severity": "ERROR",
"message": "Demo timer: 61ms",
"filename": "webservice.ts",
"Demo timer": 61,
"id": "c69adf0e7ff8fddf8a93"
}
The most important function is timer.flush()
which prints the log to the console. It uses information saved from all
the other function calls including the constructor. This function call should go at the end of the function you are
profiling.
Other functions
Start a new timer
timer.start('label for timer')
Start another timer, after the first one. (Stops the most recently started timer, and begins a new one)
timer.next('the next label you want to time')
End the most recently started timer. Is automatically called when flush is called if the most recently started timer has not been ended already.
timer.end()
To stop a specific timer by name, there are two options
const timer1 = timer.start('timer 1')
const timer2 = timer.start('timer 2')
// perform operations
timer.stop('timer 1') // similar to console.time()
timer2.stop() //doesn't require the label
Custom error messages
timer.customError('Zero length list returned. Expecting at least 1 item')
Output:
{
"severity": "ERROR",
"message": "Zero length list returned. Expecting at least 1 item",
"filename": "webservice.ts"
}
Postgres error handling
const queryResult = await pool.query(`SELECT NOW()`).catch(e => timer.postgresError(e))
Generic error handling
try {
JSON.parse('asdfghjkl;')
} catch (e) {
timer.genericError(e)
}
Output
{
"severity": "ERROR",
"errorMessage": "Unexpected token a in JSON at position 0",
"errorName": "SyntaxError",
"stackTrace": "SyntaxError: Unexpected token a in JSON at position 0\n at JSON.parse (<anonymous>)\n at Object.<anonymous> (C:\\Users\\webservice.js:6:10)\n at Module._compile (node:internal/modules/cjs/loader:1109:14)\n atObject.Module._extensions..js (node:internal/modules/cjs/loader:1138:10)\n at Module.load (node:internal/modules/cjs/loader:989:32)\n at Function.Module._load (node:internal/modules/cjs/loader:829:14)\n at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:76:12)\n at node:internal/main/run_main_module:17:47",
"filename": "webservice.ts"
}
FAQs
Nodejs logging library to time function execution
The npm package timer-logs receives a total of 23 weekly downloads. As such, timer-logs popularity was classified as not popular.
We found that timer-logs demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Fluent Assertions is facing backlash after dropping the Apache license for a commercial model, leaving users blindsided and questioning contributor rights.
Research
Security News
Socket researchers uncover the risks of a malicious Python package targeting Discord developers.
Security News
The UK is proposing a bold ban on ransomware payments by public entities to disrupt cybercrime, protect critical services, and lead global cybersecurity efforts.