Research
Security News
Malicious npm Package Targets Solana Developers and Hijacks Funds
A malicious npm package targets Solana developers, rerouting funds in 2% of transactions to a hardcoded address.
winston-timer
Advanced tools
Extend winston to measure time intervals
An important use of logging is to measure performance of certain parts of the codebase. To make this easier, this module extends the winston logging framework for NodeJS with named timers that are started and return the high-resolution time difference when they were stopped.
npm install --save winston-timer
The module exports a single function that expects a winston logger instance and an optional configuration object.
In the simplest case (using winston
as a global logger):
var winston = require('winston');
require('winston-timer')(winston);
You can also pass a stand-alone Winston.Logger
object:
var winston = require('winston');
var log = new Winston.Logger(/* ... */);
require('winston-timer')(logger, {
"useColors": false
});
Then you can use the methods provided by the API to start and stop timers and log their results:
winston.start_log('long-running-task', 'info');
/* ... */
winston.stop_log('long-running-task', 'warn');
Output:
info: Starting timer 'long-running-task'
debug Finished timer 'long-running-task' in 1007.194189 ms
See test/example.js for a short example script.
Time difference is measured in a second-nanosecond-tuple.
To get the time difference in milliseconds, use the stop_ms
method or log in milliseconds directly using stop_log
method.
starting a timer that has already been started or stopping a timer that has already been stopped will cause an exception.
start(timer)
Starts a timer.
Arguments:
Returns:
stop(timer)
Stops a timer.
Arguments:
Returns:
stop_ms(timer)
Stops a timer and returns the time passed in milliseconds.
Arguments:
Returns:
start_log(timer, level, prefix, suffix)
Starts a timer and logs a message with the timer name. By default it looks like this:
debug: Starting timer "<name of the timer>"
Arguments:
silly
, debug
, info
,
warn
, error
. Default: config.level
.config.start_prefix
.config.start_suffix
.stop_log(timer, level, prefix, suffix)
Stops a timer and logs a message with the timer name and the time elapsed in milliseconds. By default, it looks like this:
debug: Finished timer "<name of the timer>" in 1007.449218 ms
As an additional convenience, the elapsed time is highlighted according to how
long it took. This behavior can be disabled by setting
config.use_colors
to false
and the threshold times to change
colors and the colors to use can be changed with the
config.thresholds
and config.colors
arrays.
By default times will be highlighted
bold green
green
yellow
red
1000 ms:
bold red
Arguments:
silly
, debug
, info
,
warn
, error
. Default: config.level
.config.stop_prefix
.config.stop_suffix
.The configuration object can be passed as the second argument to the exported method to change the defaults, e.g.
require('winston-timer')(logger, {
"level": "info",
"use_colors": false
});
Logging level to use for start_log
and stop_log
.
Default: debug
Whether to use colors for highlighting the elapsed time.
Default: true
Thresholds at which to change the color highlighting of the elapsed time.
Default: [10, 100, 500, 999, Number.MAX_VALUE]
Colors used for highlighting elapsed times.
See the chalk module for possible values.
Default: [chalk.green.bold, chalk.green, chalk.yellow, chalk.red, chalk.red.bold]
Text before the timer name.
Default: 'Starting timer "'
Text after the timer name.
Default: '"'
Text before the timer name.
Default: 'Finished timer "'
Text after the timer name.
Default: '" in '
FAQs
Add simple time measurement to winston
We found that winston-timer 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.
Research
Security News
A malicious npm package targets Solana developers, rerouting funds in 2% of transactions to a hardcoded address.
Security News
Research
Socket researchers have discovered malicious npm packages targeting crypto developers, stealing credentials and wallet data using spyware delivered through typosquats of popular cryptographic libraries.
Security News
Socket's package search now displays weekly downloads for npm packages, helping developers quickly assess popularity and make more informed decisions.