reactjs-logger
v1.0.1
Published
A lightweight and versatile logger for structured and consistent logging in Node.js and browser environments. Includes human-readable timestamps and supports multiple log levels.
Downloads
134
Readme
Logger Package
A lightweight and versatile logger for structured and consistent logging in Node.js and browser environments. Includes human-readable timestamps and supports multiple log levels.
Features
- Human-Readable Timestamps: Logs include precise timestamps for better traceability.
- Flexible Log Levels: Supports
info
,warn
, anderror
log levels. - Environment-Based Control: Adjusts logging behavior for development and production environments.
Installation
Install the package via npm:
npm install reactjs-logger
Usage
Importing the Logger
import log from "reactjs-logger";
Logging Examples
1. Log with Explicit Level
log("info", "This is an informational message.");
log("warn", "This is a warning message.", { usage: "85%" });
log("error", "An error occurred!", new Error("Sample error"));
2. Log with Default Level (info
)
If the log level is omitted, the default is info
:
log("Task completed successfully.", { taskId: 123 });
API Documentation
log(level: LogLevel | unknown, ...messages: unknown[]): void
Logs messages with a specified log level. If no valid log level is provided, the function defaults to info
.
Parameters
level
: (LogLevel | unknown
)
The log level ("info"
,"warn"
, or"error"
). If this doesn't match a valid log level, it is treated as part of the message....messages
: (unknown[]
)
The messages to log. Can include strings, objects, or any data type.
Example
log("info", "Application started.");
log("warn", "High memory usage detected.", { usage: "95%" });
log("error", "Unhandled exception occurred.", new Error("Sample error"));
Log Levels
| Level | Description | Example Usage |
| ------- | -------------------------------- | ------------------------------------- |
| info
| General informational messages. | log("info", "Server started.");
|
| warn
| Warnings about potential issues. | log("warn", "Disk usage high.");
|
| error
| Errors or critical issues. | log("error", "Failed to connect.");
|
Environment Configuration
Control logging behavior using environment variables:
NEXT_PUBLIC_LOGGER
: Enables or disables logging. Set totrue
to enable logging.NODE_ENV
: Adjusts log level filtering based on the environment.development
: Logs all levels (info
,warn
,error
).production
: Logs onlywarn
anderror
levels.
Example .env
File
NEXT_PUBLIC_LOGGER=true
NODE_ENV=production
Production Considerations
- Minimize Logs: In production, only log necessary levels (
warn
anderror
) to avoid performance degradation. - Use Centralized Logging: Route logs to tools like AWS CloudWatch, Datadog, or Logstash for better observability.
- Avoid Sensitive Data: Ensure no sensitive information (like API keys or passwords) is logged.