Security News
tea.xyz Spam Plagues npm and RubyGems Package Registries
Tea.xyz, a crypto project aimed at rewarding open source contributions, is once again facing backlash due to an influx of spam packages flooding public package registries.
kayvee
Advanced tools
Readme
Package kayvee provides methods to output human and machine parseable strings.
Read the Kayvee spec to learn more about the goals of Kayvee logging.
Initialization:
var kayvee = require("kayvee");
var log = new kayvee.logger("logger-source");
Use it to write metrics:
log.gauge("gauge-simple", 18)
log.gaugeD("gauge-with-extra-data", 3, {user_id: "value", scope: "scope_system"})
and structured logs:
log.infoD("non-metric-log", {"msg": "this is my info", user: "user-id", group: "group-id"})
log.error("this is an error with no extra structured metadata")
Here's are two examples snippets that log a kayvee formatted string:
console.error(kayvee.format({"hello":"world"}));
# {"hello":"world"}
console.error(kayvee.formatLog("test_source", kayvee.INFO, "title", {"foo" : 1, "bar" : "baz"}));
# {"foo":1,"bar":"baz","source":"test_source","level":"info","title":"title"}
Log routing is a mechanism for defining where log lines should go once they've entered Clever's logging pipeline. Routes are defined in a yaml file called kvconfig.yml. Here's an example of a log routing rule that sends a slack message:
// main.js
const kv = require("../kayvee-js");
kv.setGlobalRouting("./kvconfig.yml");
const log = new kv.logger("myApp");
module.exports = (cb) => {
// Simple debugging
log.debug("Service has started");
// Do something async
setImmediate(() => {
// Output structured data
log.infoD("DataResults", {"key": "value"}); // Sends slack message
// You can use an object to send arbitrary key value pairs
log.infoD("DataResults", {"shorter": "line"}); // will NOT send a slack message
cb(null);
});
};
# kvconfig.yml
routes:
key-val:
matchers:
title: [ "DataResults", "QueryResults" ]
key: [ "value" ]
output:
type: "notifications"
channel: "#distribution"
icon: ":rocket:"
message: "%{key}"
user: "Flight Tracker"
To ensure that your log-routing rules are correct, use mockRouting
to temporarily mock out kayvee. The mock kayvee will record which rules and how often they were matched.
// main-test.js
const assert = require("assert");
const kv = require("../kayvee-js");
kv.setGlobalRouting("./kvconfig.yml");
const main = require("./main");
kv.mockRouting(kvdone => { // Don't nest kv.mockRouting calls!!
main(err => {
assert.ifError(err);
let ruleMatches = kvdone();
assert.equal(ruleMatches["key-val"].length, 1);
});
});
For more information on log routing see https://clever.atlassian.net/wiki/spaces/ENG/pages/90570917/Application+Log+Routing
Run make test
to execute the tests
logger
functionality along with support for gauge
and counter
metrics# only source is required
var log = new kayvee.Logger(source, logLvl = process.env.KAYVEE_LOG_LEVEL, formatter = kv.format, output = console.error)
An environment variable named KAYVEE_LOG_LEVEL
can be used instead of setting logLvl
in the application.
log.setConfig(source, logLvl, formatter, output)
You can also individually set the config
using:
setLogLevel
: defaults to LOG_LEVELS.Debug
setFormatter
: defaults to kv.format
setOutput
: defaults to console.error
Titles only:
log.debug("title")
log.info("title")
log.warn("title")
log.error("title")
log.critical("title")
Title + Metadata:
log.debugD("title" {key1: "value", key2: "val"})
log.infoD("title" {key1: "value", key2: "val"})
log.warnD("title" {key1: "value", key2: "val"})
log.errorD("title" {key1: "value", key2: "val"})
log.criticalD("title" {key1: "value", key2: "val"})
log.counter("counter-name")
defaults to value of 1
log.gauge("gauge-name", 100)
log.counterD("counter-with-data", 2, {extra: "info"})
log.gaugeD("gauge-with-data", 2, {extra: "info"})
kayvee.format(data)
Format converts a map to stringified json output
kayvee.formatLog(source, level, title, data)
formatLog
is similar to format
, but takes additional reserved params to promote
logging best-practices
source
(string) - locality of the log; an application name or part of an applicationlevel
(string) - available levels are
title
(string) - the event that occurreddata
(object) - other parameters describing the eventKayvee includes logging middleware, compatible with expressJS.
The middleware can be added most simply via
var kayvee = require('kayvee');
var app = express();
app.use(kayvee.middleware({"source":"my-app"}));
Note that source
is a required field, since it clarifies which application is emitting the logs.
The middleware also supports further user configuration via the options
object.
It prints the values of headers
or the results of handlers
.
If a value is undefined
, the key will not be printed.
headers
X-Request-Id
handlers
(request, response) => { "key": "val" }
.ignore_dir
directory
and path
directory
is the absolute file path of the directory that contains static files. This is the path passed to express.static
path
is the express mount point for these files. Defaults to /
.
This will ignore all requests with statusCode < 400
to path
/file/path/in/dir
For example, the below snippet causes the X-Request-Id
request header and a param called some_id
to be logged.
var kayvee = require('kayvee');
var app = express();
var options = {
source: "my-app",
headers: ["x-request-id"],
handlers: [
(req, res) => { return {"some_id": req.params.some_id}; }
],
};
app.use(kayvee.middleware(options));
You can also log with the request context using req.log
. For example:
myRouteHandler(req, res) {
doTheThing((err, data) => {
if (err) {
req.log.errorD("do_the_thing_error", {error: err.message});
res.send(500);
}
req.log.infoD("do_the_thing_success", {response: data});
res.send(200);
});
}
FAQs
Write data to key=val pairs, for human and machine readability
The npm package kayvee receives a total of 242 weekly downloads. As such, kayvee popularity was classified as not popular.
We found that kayvee demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 5 open source maintainers 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
Tea.xyz, a crypto project aimed at rewarding open source contributions, is once again facing backlash due to an influx of spam packages flooding public package registries.
Security News
As cyber threats become more autonomous, AI-powered defenses are crucial for businesses to stay ahead of attackers who can exploit software vulnerabilities at scale.
Security News
UnitedHealth Group disclosed that the ransomware attack on Change Healthcare compromised protected health information for millions in the U.S., with estimated costs to the company expected to reach $1 billion.