Skip to content

SHD logging module: an abstraction on console.log for both frontend as backend (Node.js).

License

Notifications You must be signed in to change notification settings

studiohyperdrive/logger

Repository files navigation

@studiohyperdrive/logger

One logger to rule them all  🚀
Supports browser console and Node.js, configurable and MIT licensed


Table of Contents


@studiohyperdrive/logger is a logging library that supports both the browser and Node.js by exporting different bundles for both targets.

Installation

Install through npm:

$ npm install @studiohyperdrive/logger

Install through yarn:

$ yarn add @studiohyperdrive/logger

Browser

For usage with the browser a UMD and ESModule bundle are exported. It provides a Logger class which is an abstraction on console.log based on high-console.

Usage

Create an instance of the logger and export it for usage in your application:

logger.js

import { Logger } from "@studiohyperdrive/logger";

export const logger = new Logger();

app.js

import { logger } from "./logger";

logger.debug("some debug message", { key: "value" });
logger.info("some info message", { key: "value" });
logger.success("some success message", { key: "value" });
logger.warn("some warn message", { key: "value" });
logger.error("some error message", { key: "value" });

Output

Overloads

As documented in the typings, each method has several overloads:

logger.info("some info message", { key: "value" }); // Message and object
logger.info("some info message"); // Message
logger.info({ key: "value" }); // Object
logger.info("some info message", { key: "value" }, true); // Force a log when loglevel is disabled

Configuration

When creating an instance of the Logger class configuration can be provided by passing an options object to the constructor.

import { Logger } from "@studiohyperdrive/logger";

export const logger = new Logger({
    enabled: [
        "error",
    ],
});

Default configuration

The default configuration is the following:

{
    enabled: [ // Enabled loglevels
        "debug",
        "info",
        "success",
        "warn",
        "error"
    ],
    timestamp: true
}

Node.js

For usage with Node.js a CommonJS bundle is exported. It provides a Logger class which is an abstraction on console.log based on signale.

Other features:

  • Logging to the filesystem using daily rotating files
    • all
    • warn
    • error

Usage

Create an instance of the logger and export it for usage in your application:

logger.js

const Logger = require("@studiohyperdrive/logger");

const logger = new Logger();

module.exports = logger;

app.js

const logger = require("./logger");

logger.debug("some debug message", { key: "value" });
logger.info("some info message", { key: "value" });
logger.cron("some cron message", { key: "value" });
logger.db("some db message", { key: "value" });
logger.success("some success message", { key: "value" });
logger.warn("some warn message", { key: "value" });
logger.error("some error message", { key: "value" });

Output

Overloads

As documented in the typings, each method has several overloads:

logger.info("some info message", { key: "value" }); // Message and object
logger.info("some info message"); // Message
logger.info({ key: "value" }); // Object
logger.info("some info message", { key: "value" }, true); // Force a log when loglevel is disabled

Configuration

When creating an instance of the Logger class configuration can be provided by passing an options object to the constructor.

const Logger = require("@studiohyperdrive/logger");

const logger = new Logger({
    enabled: [
        "error",
    ],
    filesystem: {
        enabled: true,
        path: "logs",
    },
});

module.exports = logger;

Default configuration

The default configuration is the following:

{
    enabled: [ // Enabled loglevels
        "debug",
        "info",
        "cron",
        "db",
        "success",
        "warn",
        "error"
    ],
    filesystem: {
        enabled: true, // Enable logging to filesystem
        path: "logs" // Path for the filesystem logs (from root)
    },
    timestamp: true,
}

Snippets

Snippets for JavaScript and TypeScript can be found in the snippets folder.

Roadmap

We are continuously looking to improve this package. If you have any feedback or ideas, please let us know.

The roadmap as is today:

  • ...

Contributing

Feel free to provide feedback, open issues or create pull-requests to this repository.

License

@studiohyperdrive/logger is MIT licensed by Studio Hyperdrive.

About us

Studio Hyperdrive is an experienced digital development studio focussed on all things JavaScript. Already 18 strong. Based in Antwerp & Ghent! With a handpicked set of skills we build anything from websites to chatbots and immersive cross reality experiences. Feel free to contact us through our website.