Using the Logger Module
The Logger module provides a flexible and configurable logging system for both browser and Node.js environments. This guide will demonstrate how to use the module to log messages with different log levels and styling.
Installation
To use the Logger module, first ensure you have the necessary files in your project:
deno add jsr:@hilmarch/logger
for the package Logger functionality.
Usage
Importing the Logger
To get started, import the logger
instance from jsr
:
import { logger } from "jsr:@hilmarch/logger";
Configuring the Logger
You can configure the logger by specifying log levels, transports, and default metadata:
import { logger } from "jsr:@hilmarch/logger"; logger.configure({ level: "debug", transports: { console: { level: "info" }, }, defaultMeta: { service: "user-service" }, });
Logging Messages
The Logger provides several methods for logging messages at different levels:
logger.info(...args: any[])
: Logs informational messages.logger.success(...args: any[])
: Logs success messages.logger.warn(...args: any[])
: Logs warning messages.logger.error(...args: any[])
: Logs error messages.logger.debug(...args: any[])
: Logs debug messages.logger.verbose(...args: any[])
: Logs verbose messages.
Example:
logger.info("This is an info message"); logger.error("This is an error message", { errorCode: 123 });
Environment Detection
The detectEnvironment()
method determines if the code is running in a browser or Node.js environment, which influences how messages are styled.
Conclusion
The Logger module offers a powerful and flexible way to manage logging across different environments, with support for colorized output and different log levels. Customize the configuration and color settings as needed to fit your application's requirements.
Add Package
deno add jsr:@hilmarch/logger
Import symbol
import * as logger from "@hilmarch/logger";
---- OR ----
Import directly with a jsr specifier
import * as logger from "jsr:@hilmarch/logger";
Add Package
npx jsr add @hilmarch/logger
Import symbol
import * as logger from "@hilmarch/logger";
Add Package
yarn dlx jsr add @hilmarch/logger
Import symbol
import * as logger from "@hilmarch/logger";
Add Package
pnpm dlx jsr add @hilmarch/logger
Import symbol
import * as logger from "@hilmarch/logger";
Add Package
bunx jsr add @hilmarch/logger
Import symbol
import * as logger from "@hilmarch/logger";