send log data to only targets tagged with one of the tags in the array passed as a first arguement.
send log data to only targets tagged with one of the tags in the array passed as a first argument.
first argument can be a string or array of strings.
```js
// send only to targets with the 'verbose' tag
@ -23,28 +23,40 @@ first argument can be a string or array of strings.
```
# Configuration
log4jesus supports 3 types of targets for your log data:
tlog supports 3 types of targets for your log data:
- file
- console
- http
to determine what logs go to which target, edit the file `log4jesus.json`.
there are three arrays defined in that file, associated with a key of either 'file', 'console' or 'http.
to determine what logs go to which target, edit the file `tlog.conf.js`.
there are three arrays defined in that file, associated with a key of either 'file', 'console' or 'http'.
in each case, the array should be an array of objects with a `tags` property (which should be a string or array of strings), as well as some other details about how this target should operate.
## 'file' details
- path: optional string to the file to create
# Details
## 'file' object details
- path: optional string path to the file to create. defaults to the `data` directory, as a `.log` file named a ISO 8601 timestamp at time of creation.
## 'console' details
- stdstream: required value of 'stdout', 'stderr', or 'stdin'. It's not usually desired to make it 'stdin'.
## 'console' object details
- stdstream: required value of 'stdout', 'stderr', or 'stdin'. It's not usually desired to make it 'stdin'. outputs sent to stdstreams are automatically de-duplicated. even if there are multiple console outputs with the tag relevant to your logs that share the same stdstream, the output will only happen once.
## 'http' details
our http object is an extension of the [nodejs http/https options object](https://nodejs.org/api/http.html#httprequesturl-options-callback). The object is passed faithfully to http/s.request, so anything you add that is valid for http/s.request will be valid here.
## 'http' object details
our http object is an extension of the [nodejs http/https options object](https://nodejs.org/api/http.html#httprequesturl-options-callback). The object is passed faithfully to http(s).request, so anything you add that is valid for http(s).request will be valid here.
Add a 'url' field to the object to specify the url for the request, which should include both the port and protocol - you usually won't need to set 'hostname' or 'host', or 'path' or 'port' or 'protocol'.
Whichever method you choose, the data of the log will be sent in the request body.
Add a 'url' field to the object to specify the url for the request, which should include both the port and protocol - you usually won't need to set 'hostname' or 'host', or 'path' or 'port' or 'protocol'. Certain combinations of port/protocol/path/host/hostname can behave strangely - it's recommended to specify as much information as possible in the 'url' field.
You may commonly have to set the 'headers' object, and the 'content-type' header inside of that object, depending on your server. If it's not provided it's set to 'text/plain'.
The configuration file is a .js file to enable using environment variables, as you may have sensitive information in your url(s).
### Handling Http Errors
Network requests can fail at runtime, and often those failures are also something you want to log.
Specify the `errorTags` property on the http object, and errors will attempt to be written to the target(s) specified when they occur.
If the target that failed is tagged with one of the tags on its own `errorTags` property, it will skip itself.
This feature is pretty limited and needs elaboration.
# Other Configuration Options/Settings
## 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.
@ -50,11 +54,13 @@ function parseStreams(config) {
for(leti=0;i<config.http.length;i++){
consth=config.http[i];
h.id=crypto.randomBytes(8).toString("hex");
if(!h.method){
h.method="POST";
}elseif(h.method==="GET"){
console.warn(`using the http method 'GET' to send your logs is not recommended - in particular if you are intending to send a request body. Some system libraries are known to strip the payload completely off of GET requests, making this unreliable, despite the http 1.1 standard technically allowing it. See: https://github.com/whatwg/fetch/issues/551#issue-235203784`);
console.warn(`using the http method 'GET' to send your logs is not recommended. Unfortunately, sending payloads with a GET request can be unreliable.\nSee:\nhttps://github.com/whatwg/fetch/issues/551#issue-235203784\nRemove this line in tlog.js to suppress this warning.`);
}
if(!h.url){
@ -67,68 +73,20 @@ function parseStreams(config) {
@ -173,26 +132,41 @@ function httpRequest(options, payload) {
});
request.on("error",error=>{
// @TODO, if you care about errors on the request
conststreams=getStreamsByTags(options.errorTags);
constusedStreams={};
for(leti=0;i<streams.length;i++){
conststream=streams[i];
if(usedStreams[stream.id]){
continue;
}
usedStreams[stream.id]=true;
if(stream.id!==options.id){
write(stream,"error writing to http: ",...args);
}
}
});
request.write(payload);
request.write(formatOutput(...args));
request.end();
}
functionwrite(s,...args){
if(s.stream){
s.stream.write(formatOutput(...args));
}elseif(s.type==="http"){
httpRequest(s,...args);
}
}
// log by itself will only log to streams tagged 'all'. if you want to specifiy tags, call 'logt' instead
functionlog(...args){
constoutput=formatOutput(...args);
for(leti=0;i<alwaysStreams.length;i++){
consts=alwaysStreams[i];
if(s.stream){
s.stream.write(output);
}elseif(s.type==="http"){
httpRequest(s,output);
}
write(alwaysStreams[i],...args);
}
}
@ -200,7 +174,6 @@ function log(...args) {
// which informs the logger 'where' to output the log
functionlogt(tags,...args){
if(!args){
log(tags);
return;
}
@ -208,18 +181,29 @@ function logt(tags, ...args) {
tags=[tags];
}
constoutput=formatOutput(...args);
constusedStreams={};
conststreams=getStreamsByTags(tags);
for(leti=0;i<streams.length;i++){
consts=streams[i];
if(usedStreams[s.id]){
continue;
}
if(s.stream){
s.stream.write(output);
write(s,...args);
usedStreams[s.id]=true;
}
}elseif(s.type==="http"){
httpRequest(s,output);
// now send it to the 'all' streams. we need to check if we've already done the job, because in the case a stream is tagged with both 'all' and another tag, we'd double send if we didn't de-dupe.