Skip to content

A simple, cross environment logger written in TypeScript.

Notifications You must be signed in to change notification settings

hackforplay/log

Folders and files

NameName
Last commit message
Last commit date
Sep 19, 2019
Oct 4, 2019
Sep 6, 2019
Sep 6, 2019
Sep 6, 2019
Sep 6, 2019
Oct 3, 2019
Jun 25, 2022
Sep 19, 2019
Sep 19, 2019

Repository files navigation

@hackforplay/log

Actions Status npm latest version

This is a simple logger written in TypeScript. Created for @hackforplay/sandbox and @hackforplay/common.

Type definition is ready to import! d.ts is included in this package.

Install

npm install @hackforplay/log

How to use

import { createLogger } from '@hackforplay/log';

const logger = createLogger();

// Show all logs
logger.subscribe(console.info);

// Add a new line
const log = logger.log;
log('Hello World!');

Sharing loggers between independent libraries

As you use createLogger(), the shared reference will be injected in global. e.g. window in browsers or self in the Node.js.

import { createLogger } from '@hackforplay/log';

const loggerA = createLogger();
const loggerB = createLogger();
console.log(loggerA === loggerB); // true

const loggerC = createLogger('You can use different reference with key string');
console.log(loggerA === loggerC); // false

// Yes, This is global injection :P
console.log(
  loggerC === window['You can use different reference with key string']
); // true

If you hate this way, you can use constructor. This way DO NOT global injection.

import { Logger } from '@hackforplay/log';

const logger = new Logger();

// Show all logs
logger.subscribe(console.info);

// Add a new line
const log = logger.log;
log('Hello World!');