Skip to content

๐Ÿ“ tslog - Universal Logger for TypeScript and JavaScript

License

Notifications You must be signed in to change notification settings

fullstack-build/tslog

Repository files navigation

๐Ÿ“ tslog: Beautiful logging experience for TypeScript and JavaScript

lang: Typescript License: MIT npm version CI: GitHub codecov.io code style: prettier 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

import { Logger, ILogObj } from "tslog";

const log: Logger<ILogObj> = new Logger();
log.silly("I am a silly log.");

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

Install

npm install tslog

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

{
  "name": "NAME",
  "version": "1.0.0",
  "main": "index.js",
  // here:
  "type": "module",
  "scripts": {
    "build": "tsc -p .",
    // and here:
    "start": "node --enable-source-maps --experimental-specifier-resolution=node index.js"
  },
  "dependencies": {
    "tslog": "^4"
  },
  "devDependencies": {
    "typescript": "^4"
  },
  "engines": {
    "node": ">=16"
  }
}

With this package.json you can simply build and run it:

npm run build
npm start

Otherwise:

ESM: Node.js with JavaScript:

node --enable-source-maps --experimental-specifier-resolution=node

CJS: Node.js with JavaScript:

node --enable-source-maps

ESM: Node.js with TypeScript and ts-node:

node --enable-source-maps --experimental-specifier-resolution=node --no-warnings --loader ts-node/esm

CJS: Node.js with TypeScript and ts-node:

node --enable-source-maps --no-warnings --loader ts-node/cjs

Browser:

<!doctype html>
<html lang="en">
<head>
<title>tslog example</title>
</head>
<body>
<h1>Example</h1>

<script src="tslog.js"></script>

<script>
  const logger = new tslog.Logger();
  logger.silly("I am a silly log.");
</script>

</body>
</html>

Enable TypeScript source map support:

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