
Security News
Attackers Are Hunting High-Impact Node.js Maintainers in a Coordinated Social Engineering Campaign
Multiple high-impact npm maintainers confirm they have been targeted in the same social engineering campaign that compromised Axios.
log-stream
Advanced tools
Very simple take on logging. This package doesn't concern itself with transports, persistence, or anything of the sort. Instead, it exposes streams which you can .pipe() around as you see fit.
npm install --save log-stream
var log = require('log-stream')({name: 'myApp'})
log.stream.pipe(process.stdout)
log('The sky is falling!')
log.fatal('This message should be %s.', 'fatal')
LogStream accepts the following options.
name: required - Will be included in message as name.levels: default { trace': 10, 'debug': 20,'info': 30,'warn': 40,'error': 50,'fatal': 60 } - An array of logging levels to expose.defaultLevel: default "info" - When using log(message) instead of explicitly specifying the level, this option specifies which
level the message is sent to.data: An optional object containing properties/values that will be passed as part of the log entry.message: A string containing the message to be logged. Messages support util.format style
formatting, and any argument after the message will be substituted into the message in the same manner
as util.format. The data argument is expected to be last.Options are the same as above, but the level is explicitly stated instead of allowing the message to go to the default log level. (ex: log.error('This is an error message') )
log.errorHandler will generate a function that can be supplied as a callback to a function honoring
the error-first callback convention. If that function executes the generated callback with an error,
then a log message will be recorded. If a level is provided, it will be used, otherwise error will be
used. If a msg is provided, it will be the log message, otherwise the error's message property will
be the message (or the string passed as 1st argument if Error object is not used). If the 1st argument
is an Error object, it will be serialized and passed as part of the log message in the err property.
Often you may want to log an error, and execute an error-first style callback with the error you just
logged. The log-stream module makes this easier by returning an object on log calls that exposes the
andCallWithError function which accepts a callback. The callback will be executed and
passed an Error() object as the 1st argument. This error object will expose: message, and log
properties where log contains all the log properties.
The stream property of the log instance is a duplex stream. It will emit all log entries recorded to any log level within the log instance, and writes to it will be re-emitted. Writes should adhere to the protocol described below.
The stream property of each log level is a duplex stream. It will emit log entries recorded to that log level, and writes to it will be emitted to itself, and the root log stream. Writes should adhere to the protocol described below.
log.createStream is used to create a filtered stream of selected level and above only, useful for outputting and
persisting.
Example that displays only errors and fatals to the console:
var log = require('log-stream')({prefix:'custom-example'})
log.createStream('error').pipe(process.stderr)
log.info('This will not appear on stderr.')
log.error('But this will.')
Here we log an error in a function and stop execution, supplying error to callback.
function doSomething (cb) {
// Do something
if (err) return log.error('An error occured.').andCallWithError(cb)
}
// Here's another way of doing the same thing, perhaps a little less readable
function doSomething (cb) {
// Do something
if (err) return cb( log.error('An error occured.').errorObject )
}
To persist log messages, simply pipe the log.stream to a persistent writable stream.
Log entries are streamed in this format:
{"name":"myApp","hostname":"localhost","pid":55455,"level":30,"msg":"The sky is falling!","time":"2014-12-05T05:08:44.016Z","v":1}
time is automatically set at the time the event is recorded from Date.toISOString().
hostname contains the hostname of the node process that the log message originated from, as output from
os.hostname()
name will be the string specified when the logger was created.
level will contain the numeric level in which the message was recorded.
message contains the message recorded.
If any data properties were passed as part of the message, they will be present in the JSON chunk as well.
var log = require('log-stream')({name:"App"})
log.stream.pipe(process.stdout)
log.debug("stream all the things", {why:"because"})
results in a stream chunk like:
{"name":"myApp","hostname":"localhost","pid":55736,"level":20,"msg":"stream all the things","time":"2014-12-05T05:12:08.814Z","v":1,"why":"because"}
FAQs
A pure streaming take on logging
We found that log-stream 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
Multiple high-impact npm maintainers confirm they have been targeted in the same social engineering campaign that compromised Axios.

Security News
Axios compromise traced to social engineering, showing how attacks on maintainers can bypass controls and expose the broader software supply chain.

Security News
Node.js has paused its bug bounty program after funding ended, removing payouts for vulnerability reports but keeping its security process unchanged.