npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@hidori/logger

v0.1.2

Published

Simple and flexible logger

Downloads

1

Readme

@hidori/logger

Simple and flexible logger

INSTALL

npm i --save @hidori/logger

USAGE

TEXT OUTPUT

CODE:

import * as Logger from "@hidori/logger";

const logger = new Logger.Logger();

logger.debugSync("hello");
logger.infoSync("hello");
logger.warnSync("hello");
logger.errorSync("hello");
logger.fatalSync("hello");

(async () => {
  await logger.debug("hello");
  await logger.info("hello");
  await logger.warn("hello");
  await logger.error("hello");
  await logger.fatal("hello");
})();

OUTPUT:

2024-02-18T23:15:57.767+09:00 [DEBUG] hello
2024-02-18T23:15:57.774+09:00 [INFO] hello
2024-02-18T23:15:57.775+09:00 [WARN] hello
2024-02-18T23:15:57.775+09:00 [ERROR] hello
2024-02-18T23:15:57.775+09:00 [FATAL] hello
2024-02-18T23:15:57.775+09:00 [DEBUG] hello
2024-02-18T23:15:57.775+09:00 [INFO] hello
2024-02-18T23:15:57.775+09:00 [WARN] hello
2024-02-18T23:15:57.776+09:00 [ERROR] hello
2024-02-18T23:15:57.776+09:00 [FATAL] hello

JSON OUTPUT

CODE:

import * as Logger from "@hidori/logger";

const logger = new Logger.Logger({
  formatter: new Logger.JSONFormatter(),
});

logger.debugSync("hello");
logger.infoSync("hello");
logger.warnSync("hello");
logger.errorSync("hello");
logger.fatalSync("hello");

(async () => {
  await logger.debug("hello");
  await logger.info("hello");
  await logger.warn("hello");
  await logger.error("hello");
  await logger.fatal("hello");
})();

OUTPUT:

{"timestamp":"2024-02-18T23:15:57.722+09:00","level":"debug","data":"hello"}
{"timestamp":"2024-02-18T23:15:57.727+09:00","level":"info","data":"hello"}
{"timestamp":"2024-02-18T23:15:57.727+09:00","level":"warn","data":"hello"}
{"timestamp":"2024-02-18T23:15:57.727+09:00","level":"error","data":"hello"}
{"timestamp":"2024-02-18T23:15:57.727+09:00","level":"fatal","data":"hello"}
{"timestamp":"2024-02-18T23:15:57.728+09:00","level":"debug","data":"hello"}
{"timestamp":"2024-02-18T23:15:57.728+09:00","level":"info","data":"hello"}
{"timestamp":"2024-02-18T23:15:57.728+09:00","level":"warn","data":"hello"}
{"timestamp":"2024-02-18T23:15:57.728+09:00","level":"error","data":"hello"}
{"timestamp":"2024-02-18T23:15:57.728+09:00","level":"fatal","data":"hello"}

API

ASYNC

SIGNATURE:

logger.xxx(data)

xxx is one of the LEVELS

SYNC

SIGNATURE:

logger.xxxSync(data)

xxx is one of the LEVELS

LEVELS

  • debug
  • info
  • warn
  • error
  • fatal

CONFIGURATION

LEVEL

CODE:

import * as Logger from "@hidori/logger";

const logger = new Logger.Logger({
  level: Logger.levelError,
});

logger.debugSync("hello");
logger.infoSync("hello");
logger.warnSync("hello");
logger.errorSync("hello");
logger.fatalSync("hello");

(async () => {
  await logger.debug("hello");
  await logger.info("hello");
  await logger.warn("hello");
  await logger.error("hello");
  await logger.fatal("hello");
})();

OUTPUT:

2024-02-18T23:52:35.574+09:00 [ERROR] hello
2024-02-18T23:52:35.579+09:00 [FATAL] hello
2024-02-18T23:52:35.579+09:00 [ERROR] hello
2024-02-18T23:52:35.579+09:00 [FATAL] hello

Relationship between level and output: | level | debug()/debugSync() | info()/infoSync() | warn()/warnSync() | error()/errorSync() | fatal()/fatalSync() | | :-- | :-- | :-- | :-- | :-- | :-- | | levelDebug | O | O | O | O | O | | levelInfo | - | O | O | O | O | | levelWarn | - | - | O | O | O | | levelError | - | - | - | O | O | | levelFatal | - | - | - | - | O | | levelNone | - | - | - | - | - |

FORMATTER

BUILTIN TEXT FORMATTER (DEFAULT)

CODE:

import * as Logger from "../src/index.js";

const logger = new Logger.Logger({
  formatter: new Logger.TextFormatter(),
});

logger.infoSync("hello");

(async () => {
  await logger.info({
    key: "ABC",
    value: "123",
  });
})();

OUTPUT:

2024-02-19T00:20:33.075+09:00 [INFO] hello
2024-02-19T00:20:33.075+09:00 [INFO] hello

BUILTIN JSON FORMATTER

CODE:

import * as Logger from "@hidori/logger";

const logger = new Logger.Logger({
  formatter: new Logger.TextFormatter(),
});

logger.infoSync({
  key: "ABC",
  value: "123",
});

(async () => {
  await logger.info({
    key: "ABC",
    value: "123",
  });
})();

OUTPUT:

{"timestamp":"2024-02-19T00:20:33.037+09:00","level":"info","data":{"key":"ABC","value":"123"}}
{"timestamp":"2024-02-19T00:20:33.037+09:00","level":"info","data":{"key":"ABC","value":"123"}}

CUSTOM FORMATTER

CODE:

const logger = new Logger.Logger({
  formatter: {
    format: (timestamp, level, data) => {
      return `${Logger.toISOStringWithTimezone(timestamp)} [${level.toUpperCase()}] ${JSON.stringify(data)}`;
    }
  },
});

logger.infoSync({
  key: "ABC",
  value: "123",
});

(async () => {
  await logger.info({
    key: "ABC",
    value: "123",
  });
})();

OUTPUT:

2024-02-19T00:34:44.174+09:00 [INFO] {"key":"ABC","value":"123"}
2024-02-19T00:34:44.179+09:00 [INFO] {"key":"ABC","value":"123"}

WRITER

BUILTIN CONSOLE WRITER (DEFAULT)

CODE:

import * as Logger from "@hidori/logger";

const writer = new Logger.ConsoleWriter();
const logger = new Logger.Logger({
  writer: writer,
});

logger.infoSync("hello");

(async () => {
  await logger.info("hello");
})();

OUTPUT:

2024-02-19T00:56:08.305+09:00 [INFO] hello
2024-02-19T00:56:08.309+09:00 [INFO] hello

BUILTIN STRING WRITER

CODE:

import * as Logger from "@hidori/logger";

const writer = new Logger.StringWriter();
const logger = new Logger.Logger({
  writer: writer,
});

logger.infoSync("hello");

(async () => {
  await logger.info("hello");
})();

console.log(writer.toString());

OUTPUT:

2024-02-19T00:56:08.398+09:00 [INFO] hello
2024-02-19T00:56:08.398+09:00 [INFO] hello

CUSTOM WRITER

CODE:

import fs from "fs";
import path from "path";

import * as Logger from "@hidori/logger";

const logger = new Logger.Logger({
  writer: {
    write: async (text) => {
      const fileName = path.join(
        process.env.HOME,
        "./example_config_writer_custom.txt",
      );
      fs.appendFileSync(fileName, text + "\n");
    },

    writeSync: (text) => {
      const fileName = path.join(
        process.env.HOME,
        "./example_config_writer_custom.txt",
      );
      fs.appendFileSync(fileName, text + "\n");
    },
  },
});

logger.infoSync("hello");

(async () => {
  await logger.info("hello");
})();

OUTPUT:

$ cat ~/example_config_writer_custom.txt
2024-02-19T00:48:08.529+09:00 [INFO] hello
2024-02-19T00:48:08.529+09:00 [INFO] hello

LICENSE

MIT

Copyright (c) 2024 Hiroaki SHIBUKI