Zaplog is a minimalist logging library designed to work out of the box with zero
configurations. It provides four log levels (info, error, warn, debug)
and writes log messages to files (except in production).
Install Zaplog using npm or yarn:
npm install zaplogHere’s how to get started with Zaplog:
import { createLogger } from "zaplog";
// Default loggerEnvironment = 'node'
const logger = createLogger();
// Log messages
logger.info("This is an info message");
logger.error("An error occurred");
logger.warn("This is a warning");
logger.debug("Debugging information");You can provide additional context to log messages for more clarity. Use the optional context parameter in the logging methods:
logger.info("User logged in", "AuthService");
logger.error("Database connection failed", "DBService");In the above example, AuthService and DBService are contexts that help identify the source of the log messages.
- Minimalist Design: Works out of the box with sensible defaults.
- Browser support: Works in browser and node.
- Flexible Log Levels: Supports
info,error,warn, anddebuglevels. - Environment-Sensitive Defaults: Adjusts logging behavior based on the
environment (
development,test,production). - File Writing: Automatically writes logs to categorized files.
- Error Stack Traces: Includes error stack traces for deeper insights.
- Zero Configuration: No need for initial setup; just import and start logging.
Zaplog supports four log levels with varying levels of severity:
| Level | Code | Description |
|---|---|---|
error |
0 | Critical issues requiring immediate attention |
warn |
1 | Non-critical issues that might need attention |
info |
2 | General informational messages |
debug |
3 | Detailed information useful for debugging |
Logs with a severity higher than the current environment level will not be
recorded. For example, if the level is set to warn, only warn and error
logs will be processed.
Default log level based on the environment:
development:infotest:errorproduction:warn- Default :
info
You can override these defaults by configuring the library (see below).
Zaplog is designed to work out of the box, but you can customize its behavior by
passing configuration options to the Logger constructor.
If no configuration is provided, following default options are used:
{
level: 'info', // Default log level based on the environment
errorStack: true, // Includes stack traces in error logs
logFiles: false,
}Default options if logFiles is set to true:
logFiles: {
error: "${projectRoot}/logs/errors.log",
warn: "${projectRoot}/logs/warnings.log",
info: "${projectRoot}/logs/combined.log",
debug: "${projectRoot}/logs/combined.log",
combined: "${projectRoot}/logs/combined.log",
}Where projectRoot is equal to absolute path to you project root.
{
level: 'info', // Default log level based on the environment
errorStack: true, // Includes stack traces in error logs
}To customize the logging behavior, pass an options object as second argument
import {createLogger} from "zaplog";
const logger = createLogger("node",{
level: "debug",
errorStack: false,
logFiles: {
error: "/custom-logs/errors.log",
warn: "/custom-logs/warnings.log",
info: "/custom-logs/info.log",
debug: "/custom-logs/debug.log",
combined: "/custom-logs/combined.log",
},
});
logger.info("Custom configuration applied!");
File path should be absolute e.g
`path.join(import.meta.dirname,"logs/custom.log")`In Browser Environment:
import { createLogger } from "zaplog";
const logger = createLogger("browser", {
level: "debug",
errorStack: false,
});
logger.info("Custom configuration applied!");We welcome contributions to improve this project! If you'd like to contribute, follow these steps:
- Fork the repository.
- Create a new branch (
git checkout -b feature-name). - Make your changes and commit them (
git commit -am 'Add new feature'). - Push to your branch (
git push origin feature-name). - Create a pull request.
Please ensure your code adheres to the existing coding standards and includes tests where applicable.
This project is licensed under the MIT License - see the LICENSE file for details.
- My previous project Microservices Backend
