Browse Source

readme, add tests, fix mistake

main
churchianity 3 years ago
parent
commit
9d7bc9ee53
  1. 1
      .gitignore
  2. 5
      README.md
  3. 10
      log4jesus.js
  4. 16
      test.js

1
.gitignore

@ -0,0 +1 @@
*.log

5
README.md

@ -2,7 +2,7 @@
# Usage # Usage
```js ```js
const { log, logt } = require("log4jesus");
const { log, logt } = require("./log4jesus");
``` ```
## log ## log
@ -48,3 +48,6 @@ You may commonly have to set the 'headers' object, and the 'content-type' header
## timestamps ## 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. 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.

10
log4jesus.js

@ -17,7 +17,7 @@ function parseStreams(config) {
if (config.file) { if (config.file) {
for (let i = 0; i < config.file.length; i++) { for (let i = 0; i < config.file.length; i++) {
const file = config.file[i]; const file = config.file[i];
let path = file.path || `${__dirname}/${timestamp()}-log.txt`;
let path = file.path || `${__dirname}/${timestamp()}.log`;
try { try {
file.stream = fs.createWriteStream(path, { flags: "a", autoClose: true }); file.stream = fs.createWriteStream(path, { flags: "a", autoClose: true });
@ -152,10 +152,6 @@ function getStreamsOfTypeAndTags(tags, type) {
} }
function getStreamsByTags(tags) { function getStreamsByTags(tags) {
if (!Array.isArray(tags)) {
tags = [ tags ];
}
return [].concat( return [].concat(
getStreamsOfTypeAndTags(tags, "file"), getStreamsOfTypeAndTags(tags, "file"),
getStreamsOfTypeAndTags(tags, "console"), 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 // 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) { function httpRequest(options, payload) {
const url = new URL(options.url); const url = new URL(options.url);
@ -212,10 +208,12 @@ function logt(tags, ...args) {
tags = [ tags ]; tags = [ tags ];
} }
const output = formatOutput(...args);
const streams = getStreamsByTags(tags); const streams = getStreamsByTags(tags);
for (let i = 0; i < streams.length; i++) { for (let i = 0; i < streams.length; i++) {
const s = streams[i]; const s = streams[i];
if (s.stream) { if (s.stream) {
s.stream.write(output); s.stream.write(output);

16
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)
Loading…
Cancel
Save