One logger to rule them all 🚀
Supports browser console and Node.js, configurable and MIT licensed
@studiohyperdrive/logger is a logging library that supports both the browser and Node.js by exporting different bundles for both targets.
Install through npm:
$ npm install @studiohyperdrive/logger
Install through yarn:
$ yarn add @studiohyperdrive/logger
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
.
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
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
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",
],
});
The default configuration is the following:
{
enabled: [ // Enabled loglevels
"debug",
"info",
"success",
"warn",
"error"
],
timestamp: true
}
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
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
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
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;
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 for JavaScript and TypeScript can be found in the snippets folder.
We are continuously looking to improve this package. If you have any feedback or ideas, please let us know.
The roadmap as is today:
- ...
Feel free to provide feedback, open issues or create pull-requests to this repository.
@studiohyperdrive/logger is MIT licensed by Studio Hyperdrive.
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.