26
GitHub - crylate/deaf: simple logging library for node.js
source link: https://github.com/crylate/deaf
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
deaf.js
Installation
npm install deaf
Usage
Importing
const deaf = require("deaf");
Configuration
use this if you want to save your logs to files automatically
- the logs property is meant for basic logging
- the reqs property is meant for http requests
deaf.config ({ logs: "./logs.txt", reqs: "./requests.txt" });
Basic Logging
deaf.success("successfully fetched data"); deaf.error("user is not defined"); deaf.warning("this method is deprecated and will be removed in future versions"); deaf.info("u/spez logged in");
HTTP Requests
deaf.request(path, status, source); deaf.request("/", 418, "192.0.2.0"); deaf.request("/login", 100, "192.0.2.255"); deaf.request("/profile", 200, "192.0.2.104"); deaf.request("/news", 502, "192.0.2.13"); // you can also use the shorter way instead deaf.req(path, status, source);
Reading Logs
you can either read the logs file or the reqs file and either use promises or await
deaf.read("logs") .then(data => console.log(data)) .catch(err => console.log(err)); let data = await deaf.read("reqs"); console.log(data);
Express Example
here's an example how you could use this library with the express.js framework
const express = require("express"); const deaf = require("deaf"); const app = express(); app.listen(3000, () => deaf.info("server is waiting for requests")); deaf.config ({ logs: "./logs.txt", reqs: "./requests.txt" }); app.get("/", (req, res) => { deaf.request(req.url, 200, req.connection.remoteAddress); res.sendStatus(200); });
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK