Security News
RubyGems.org Adds New Maintainer Role
RubyGems.org has added a new "maintainer" role that allows for publishing new versions of gems. This new permission type is aimed at improving security for gem owners and the service overall.
Roarr is a lightweight and fast logger for Node.js applications. It is designed to be simple, performant, and easy to use, providing structured logging capabilities with minimal overhead.
Basic Logging
Roarr allows you to create a logger instance and log messages with different severity levels. In this example, an info message is logged.
const Roarr = require('roarr');
const log = Roarr.child({ application: 'my-app' });
log.info('This is an info message');
Child Loggers
Roarr supports creating child loggers that inherit context from their parent loggers. This is useful for adding additional context to logs in different parts of your application.
const Roarr = require('roarr');
const parentLog = Roarr.child({ application: 'my-app' });
const childLog = parentLog.child({ module: 'my-module' });
childLog.debug('This is a debug message from my-module');
Structured Logging
Roarr supports structured logging, allowing you to log additional context as JSON objects. This makes it easier to analyze and search logs.
const Roarr = require('roarr');
const log = Roarr.child({ application: 'my-app' });
log.info({ userId: 123, action: 'login' }, 'User login event');
Log Level Filtering
Roarr allows you to filter logs by severity level using the ROARR_LOG environment variable. In this example, only debug and higher severity logs will be output.
process.env.ROARR_LOG = 'debug';
const Roarr = require('roarr');
const log = Roarr.child({ application: 'my-app' });
log.debug('This is a debug message');
log.info('This is an info message');
Winston is a versatile logging library for Node.js with support for multiple transports, log levels, and formats. It is more feature-rich compared to Roarr but also comes with more complexity and overhead.
Pino is a fast and low-overhead logging library for Node.js, similar to Roarr in terms of performance. Pino offers more features out of the box, such as log rotation and serializers, but may be slightly more complex to set up.
Bunyan is a JSON logging library for Node.js that provides structured logging and log levels. It is similar to Roarr in its focus on structured logging but includes additional features like log streams and serializers.
Roarr.
Roarr logging is disabled by default. To enable logging, you must start program with an environment variable ROARR_LOG
set to true
, e.g.
ROARR_LOG=true node ./index.js
roarr
package exports a function that accepts the following API:
export type LoggerType =
(
context: MessageContextType,
message: string,
c?: SprintfArgumentType,
d?: SprintfArgumentType,
e?: SprintfArgumentType,
f?: SprintfArgumentType,
g?: SprintfArgumentType,
h?: SprintfArgumentType,
i?: SprintfArgumentType,
k?: SprintfArgumentType
) => void |
(
message: string,
b?: SprintfArgumentType,
c?: SprintfArgumentType,
d?: SprintfArgumentType,
e?: SprintfArgumentType,
f?: SprintfArgumentType,
g?: SprintfArgumentType,
h?: SprintfArgumentType,
i?: SprintfArgumentType,
k?: SprintfArgumentType
) => void;
To put it in words:
string | number | boolean | null
).import log from 'roarr';
log('foo');
log('bar %s', 'baz');
// Creates a child logger appending the provided `context` object
// to the previous logger context.
const debug = log.child({
level: 'debug'
});
debug('qux');
debug({
quuz: 'corge'
}, 'quux');
Produces output:
{"context":{},"message":"foo","sequence":0,"time":1506776210001,"version":"1.0.0"}
{"context":{},"message":"bar baz","sequence":1,"time":1506776210002,"version":"1.0.0"}
{"context":{"level":"debug"},"message":"qux","sequence":2,"time":1506776210003,"version":"1.0.0"}
{"context":{"level":"debug","quuz":"corge"},"sequence":3,"message":"quux","time":1506776210004,"version":"1.0.0"}
Prepending context using the global state will affect all roarr
logs.
import log from 'roarr';
log('foo');
global.ROARR.prepend = {
taskId: 1
};
log('bar');
global.ROARR.prepend = {};
log('baz');
Produces output:
{"context":{},"message":"foo","sequence":0,"time":1506776210001,"version":"1.0.0"}
{"context":{"taskId":1},"message":"bar","sequence":1,"time":1506776210002,"version":"1.0.0"}
{"context":{},"message":"baz","sequence":2,"time":1506776210003,"version":"1.0.0"}
Prepending context using the global state is useful when the desired result is to associate all logs with a specific context for a duration of an operation, e.g. to correlate the main process logs with the dependency logs.
import log from 'roarr';
import foo from 'foo';
const taskIds = [
1,
2,
3
];
for (const taskId of taskIds) {
global.ROARR = global.ROARR || {};
global.ROARR.prepend = {
taskId
};
log('starting task ID %d', taskId);
// In this example, `foo` is an arbitrary third-party dependency that is using
// roarr logger.
foo(taskId);
log('successfully completed task ID %d', taskId);
global.ROARR.prepend = {};
}
Produces output:
{"context":{"taskId":1},"message":"starting task ID 1","sequence":0,"time":1506776210001,"version":"1.0.0"}
{"context":{"taskId":1},"message":"foo","sequence":1,"time":1506776210002,"version":"1.0.0"}
{"context":{"taskId":1},"message":"successfully completed task ID 1","sequence":2,"time":1506776210003,"version":"1.0.0"}
[...]
Roarr is designed to print all or none logs (refer to the ROARR_LOG
environment variable documentation).
To filter logs you need to use a JSON processor, e.g. jq.
jq
allows you to filter JSON messages using select(boolean_expression)
, e.g.
ROARR_LOG=true node ./index.js | jq 'select(.context.level == "warning" or .context.level == "error")'
A transport in most logging libraries is something that runs in-process to perform some operation with the finalised log line. For example, a transport might send the log line to a standard syslog server after processing the log line and reformatting it.
Roarr does not support in-process transports.
Roarr does not support in-process transports because Node processes are single threaded processes (ignoring some technical details). Given this restriction, Roarr purposefully offloads handling of the logs to external processes so that the threading capabilities of the OS can be used (or other CPUs).
Depending on your configuration, consider one of the following log transports:
When running the script in a Node.js environment, use environment variables to control roarr
behaviour.
Name | Type | Function | Default |
---|---|---|---|
ROARR_LOG | Boolean | Enables/ disables logging. | false |
FAQs
JSON logger for Node.js and browser.
The npm package roarr receives a total of 1,041,615 weekly downloads. As such, roarr popularity was classified as popular.
We found that roarr demonstrated a healthy version release cadence and project activity because the last version was released less than 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
RubyGems.org has added a new "maintainer" role that allows for publishing new versions of gems. This new permission type is aimed at improving security for gem owners and the service overall.
Security News
Node.js will be enforcing stricter semver-major PR policies a month before major releases to enhance stability and ensure reliable release candidates.
Security News
Research
Socket's threat research team has detected five malicious npm packages targeting Roblox developers, deploying malware to steal credentials and personal data.