3.0.2 • Published 7 months ago

logtown v3.0.2

Weekly downloads
20
License
Apache-2.0
Repository
github
Last release
7 months ago

Logtown

npm

Lightweight logging wrapper with only one dependency. Logtown is not a logger and does not depend on any loggers.

The reasoning behind this package is to provide a simple and easy-to-use logging interface that can be used with any logger within any environment.

Nowadays, application can be run in many different environments, such as Cloudflare Workers, Deno, AWS Lambda, Node.js, Bun.js, Browsers and many others. Each of these environments has its own logging capabilities and requirements. Logtown is designed to be a single logging interface that can be used in any of these environments.

For example, for local development you can enable console logging that would include colors, while in production you would use a logger that sends logs to a remote server, such as AWS CloudWatch, and/or you can configure it to send logs in a specific format, like JSON.

Log levels

Logtown supports the following log levels:

  • verbose
  • debug
  • info
  • warn
  • error

No other log levels are supported and never will be. The decision to keep the number of log levels to a minimum and never change them, so that the code that uses Logtown will be more predictable and easier to maintain.

Installation

npm install logtown
yarn add logtown
pnpm add logtown
bun add logtown

Usage

import { createLogger, registerWrapper, ConsoleWrapper } from "logtown";

const logger = new createLogger("my-logger");
logger.info("Hello World");
// ^ nothing happens, because no wrappers were added

registerWrapper(ConsoleWrapper);
logger.info("Hello World");
// ^ prints "Hello World" to the console

Wrappers

Wrappers are the actual loggers that will be used to log messages. You should create your own wrapper. The wrapper can be either a function or an object that has a log method(that would be called for all log levels) or any of log level methods.

import { createLogger, registerWrapper, type LoggerPayload } from "logtown";

// the following wrapper will log all messages to the console
registerWrapper({
  log: ({ level, message }: LoggerPayload) => {
    console.log(`[${level}] ${message}`);
  },
});
import { createLogger, registerWrapper, type LoggerPayload } from "logtown";
import debug from "debug";
const debugLog = debug("my-logger");

if (process.env.NODE_ENV === "development") {
  // the following wrapper will log debug messages only to the debug logger
  registerWrapper({
    debug: ({ level, timestamp, id, message, ...rest }: LoggerPayload) => {
      debugLog(message, ...rest);
    },
  });
}

License

logtown released under the Apache 2.0 license

Donate

npm.io npm.io

3.0.2

7 months ago

3.0.1

7 months ago

3.0.0

7 months ago

2.4.5

7 years ago

2.4.4

7 years ago

2.4.0

7 years ago

2.3.1

7 years ago

2.3.0

7 years ago

2.2.0

7 years ago

2.1.3

8 years ago

2.1.2

8 years ago

2.1.1

8 years ago

2.1.0

8 years ago

2.0.2

9 years ago

2.0.1

9 years ago

2.0.0

9 years ago

1.0.5

9 years ago

1.0.4

9 years ago

1.0.3

9 years ago

1.0.2

9 years ago

1.0.1

9 years ago

1.0.0

9 years ago