From 9d7bc9ee530bb3fbd05ca093f0ff5fbdb70072e1 Mon Sep 17 00:00:00 2001 From: churchianity Date: Sun, 8 May 2022 23:32:10 -0400 Subject: [PATCH] readme, add tests, fix mistake --- .gitignore | 1 + README.md | 5 ++++- log4jesus.js | 10 ++++------ test.js | 16 ++++++++++++++++ 4 files changed, 25 insertions(+), 7 deletions(-) create mode 100644 .gitignore create mode 100644 test.js diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..397b4a7 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +*.log diff --git a/README.md b/README.md index a707d3a..67510fa 100644 --- a/README.md +++ b/README.md @@ -2,7 +2,7 @@ # Usage ```js - const { log, logt } = require("log4jesus"); + const { log, logt } = require("./log4jesus"); ``` ## log @@ -48,3 +48,6 @@ You may commonly have to set the 'headers' object, and the 'content-type' header ## timestamps all logs are prefixed with an ISO 8601 timestamp. You can change this easily by editing the `timestamp` function, but for now it is not configurable from the config file. +## output formatting +by default, we format output using `util.format`, the same way console output is normally formatted. You can change this if you want by editing the `formatOutput` function, but for now it is not configurable from the config file. + diff --git a/log4jesus.js b/log4jesus.js index d8015cf..cc0c436 100644 --- a/log4jesus.js +++ b/log4jesus.js @@ -17,7 +17,7 @@ function parseStreams(config) { if (config.file) { for (let i = 0; i < config.file.length; i++) { const file = config.file[i]; - let path = file.path || `${__dirname}/${timestamp()}-log.txt`; + let path = file.path || `${__dirname}/${timestamp()}.log`; try { file.stream = fs.createWriteStream(path, { flags: "a", autoClose: true }); @@ -152,10 +152,6 @@ function getStreamsOfTypeAndTags(tags, type) { } function getStreamsByTags(tags) { - if (!Array.isArray(tags)) { - tags = [ tags ]; - } - return [].concat( getStreamsOfTypeAndTags(tags, "file"), getStreamsOfTypeAndTags(tags, "console"), @@ -164,7 +160,7 @@ function getStreamsByTags(tags) { } // convienent list of streams that we always want to send our logs to, regardless of tags -const alwaysStreams = getStreamsByTags("all"); +const alwaysStreams = getStreamsByTags(["all"]); function httpRequest(options, payload) { const url = new URL(options.url); @@ -212,10 +208,12 @@ function logt(tags, ...args) { tags = [ tags ]; } + const output = formatOutput(...args); const streams = getStreamsByTags(tags); for (let i = 0; i < streams.length; i++) { const s = streams[i]; + if (s.stream) { s.stream.write(output); diff --git a/test.js b/test.js new file mode 100644 index 0000000..30cc3d7 --- /dev/null +++ b/test.js @@ -0,0 +1,16 @@ + +const { log, logt } = require("./log4jesus"); + + +log("use", ["exactly", "like"], `console.log ${420 * 69}`) + + +const downloadProgress = 51; +const totalDownloads = 106; +// send only to targets with the 'verbose' tag +logt("verbose", "Download progress:", downloadProgress / totalDownloads); + +const filePath = "/example-non-existant-file.json"; +// send to targets with the 'info' and 'error' tags +logt(["error", "info"], "failed to parse json at: ", filePath) +