You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

65 lines
3.5 KiB

2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
  1. # Usage
  2. ```js
  3. const { log, logt } = require("./tlog");
  4. ```
  5. ## log
  6. send log data to targets tagged with 'all'
  7. ```js
  8. log("use", ["exactly", "like"], `console.log ${17 * 35}`)
  9. ```
  10. ## logt
  11. send log data to only targets tagged with one of the tags in the array passed as a first argument.
  12. first argument can be a string or array of strings.
  13. ```js
  14. // send only to targets with the 'verbose' tag
  15. logt("verbose", "Download progress:", downloadProgress / totalDownloads);
  16. // send to targets with the 'info' and 'error' tags
  17. logt(["error", "info"], "failed to parse json at: ", filePath)
  18. ```
  19. # Configuration
  20. tlog supports 3 types of targets for your log data:
  21. - file
  22. - console
  23. - http
  24. to determine what logs go to which target, edit the file `tlog.conf.js`.
  25. there are three arrays defined in that file, associated with a key of either 'file', 'console' or 'http'.
  26. 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.
  27. # Details
  28. ## 'file' object details
  29. - 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.
  30. ## 'console' object details
  31. - 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.
  32. ## 'http' object details
  33. 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.
  34. Whichever method you choose, the data of the log will be sent in the request body.
  35. 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.
  36. 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'.
  37. The configuration file is a .js file to enable using environment variables, as you may have sensitive information in your url(s).
  38. ### Handling Http Errors
  39. Network requests can fail at runtime, and often those failures are also something you want to log.
  40. Specify the `errorTags` property on the http object, and errors will attempt to be written to the target(s) specified when they occur.
  41. If the target that failed is tagged with one of the tags on its own `errorTags` property, it will skip itself.
  42. This feature is pretty limited and needs elaboration.
  43. # Other Configuration Options/Settings
  44. ## timestamps
  45. 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.
  46. ## output formatting
  47. 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.