tslog

Beautiful logging experience for TypeScript and JavaScript

README

📝 tslog: Beautiful logging experience for TypeScript and JavaScript

lang: Typescript
License: MIT npm version
CI: GitHub codecov.io code style: prettier undefined GitHub stars

Powerful, fast and expressive logging for TypeScript and JavaScript


tslog pretty output

Highlights


⚡ **Fast and powerful**
🪶 **Lightweight and flexible**
🏗 **Universal: Works in Browsers and Node.js**
👮‍️ **Fully typed with TypeScript support (native source maps)**
🗃 **_Pretty_ or `JSON` output**
📝 **Customizable log level**
⭕️ **Supports _circular_ structures**
🦸 **Custom pluggable loggers**
💅 **Object and error interpolation**
🤓 **Stack trace and pretty errors**
👨‍👧‍👦 **Sub-logger with inheritance**
🙊 **Mask/hide secrets and keys**
📦 **CJS & ESM with tree shaking support**
✍️ **Well documented and tested**

Example


  1. ```typescript
  2. import { Logger } from "tslog";

  3. const log: Logger = new Logger();
  4. log.silly("I am a silly log.");
  5. ```

Donations help me allocate more time for my open source work.
undefined

Install


  1. ``` sh
  2. npm install tslog
  3. ```

In order to run a native ES module in Node.js, you have to do two things:

1) Set "type": "module" in package.json.
2) For now, start with --experimental-specifier-resolution=node

Example package.json
  1. ``` json5
  2. {
  3.   "name": "NAME",
  4.   "version": "1.0.0",
  5.   "main": "index.js",
  6.   // here:
  7.   "type": "module",
  8.   "scripts": {
  9.     "build": "tsc -p .",
  10.     // and here:
  11.     "start": "node --enable-source-maps --experimental-specifier-resolution=node index.js"
  12.   },
  13.   "dependencies": {
  14.     "tslog": "^4"
  15.   },
  16.   "devDependencies": {
  17.     "typescript": "^4"
  18.   },
  19.   "engines": {
  20.     "node": ">=16"
  21.   }
  22. }
  23. ```

With this package.json you can simply build and run it:
  1. ``` sh
  2. npm run build
  3. npm start
  4. ```

Otherwise:

ESM: Node.js with JavaScript:
  1. ``` sh
  2. node --enable-source-maps --experimental-specifier-resolution=node
  3. ```

CJS: Node.js with JavaScript:
  1. ``` sh
  2. node --enable-source-maps
  3. ```

ESM: Node.js with TypeScript and ts-node:
  1. ``` sh
  2. node --enable-source-maps --experimental-specifier-resolution=node --no-warnings --loader ts-node/esm
  3. ```

CJS: Node.js with TypeScript and ts-node:
  1. ``` sh
  2. node --enable-source-maps --no-warnings --loader ts-node/cjs
  3. ```

Browser:
  1. ``` html
  2. <!doctype html>
  3. <html lang="en">
  4. <head>
  5. <title>tslog example</title>
  6. </head>
  7. <body>
  8. <h1>Example</h1>
  9. <script src="tslog.js"></script>
  10. <script>
  11.   const logger = new tslog.Logger();
  12.   logger.silly("I am a silly log.");
  13. </script>
  14. </body>
  15. </html>
  16. ```

Enable TypeScript source map support:

This feature enables tslog to reference a correct line number in your TypeScript source code.

  1. ``` json5
  2. // tsconfig.json
  3. {
  4.   // ...
  5.   compilerOptions: {
  6.     // ...
  7.     "inlineSourceMap": true,  //