devdad-express-utils 1.2.0 → 1.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +36 -1
- package/dist/errorHandler.js +3 -2
- package/dist/index.d.ts +2 -2
- package/dist/index.js +2 -2
- package/dist/logger.d.ts +2 -0
- package/dist/logger.js +15 -0
- package/package.json +3 -2
package/README.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# Express Utils
|
|
2
2
|
|
|
3
|
-
A collection of reusable utilities for Express.js applications, including error handling, async route wrapping, custom error classes,
|
|
3
|
+
A collection of reusable utilities for Express.js applications, including error handling, async route wrapping, custom error classes, MongoDB connection management, and Winston-based logging.
|
|
4
4
|
|
|
5
5
|
## Installation
|
|
6
6
|
|
|
@@ -98,6 +98,33 @@ const status = getDBStatus();
|
|
|
98
98
|
console.log(status); // { isConnected: true, readyState: 1, host: '...', name: '...' }
|
|
99
99
|
```
|
|
100
100
|
|
|
101
|
+
### Logging
|
|
102
|
+
|
|
103
|
+
Winston-based logger with configurable service name and environment-aware transports.
|
|
104
|
+
|
|
105
|
+
```typescript
|
|
106
|
+
import { logger } from "devdad-express-utils";
|
|
107
|
+
|
|
108
|
+
// Log messages at different levels
|
|
109
|
+
logger.info("User logged in", { userId: 123 });
|
|
110
|
+
logger.error("Database connection failed", { error: err.message });
|
|
111
|
+
logger.debug("Processing request", { requestId: "abc-123" });
|
|
112
|
+
```
|
|
113
|
+
|
|
114
|
+
#### Configuration
|
|
115
|
+
|
|
116
|
+
- **Service Name**: Set `SERVICE_NAME` environment variable to customize the service name in logs (defaults to "express-utils")
|
|
117
|
+
- **Log Level**: "debug" in development, "info" in production
|
|
118
|
+
- **Transports**:
|
|
119
|
+
- **Development**: Console (colored) + error.log + combined.log files
|
|
120
|
+
- **Production**: Console only (suitable for platforms like Railway)
|
|
121
|
+
|
|
122
|
+
#### Log Files
|
|
123
|
+
|
|
124
|
+
In development, logs are written to:
|
|
125
|
+
- `error.log`: Error level and above
|
|
126
|
+
- `combined.log`: All log levels
|
|
127
|
+
|
|
101
128
|
## Error Handling Patterns
|
|
102
129
|
|
|
103
130
|
### Using AppError
|
|
@@ -221,6 +248,14 @@ Gets the current MongoDB connection status.
|
|
|
221
248
|
getDBStatus() => { isConnected: boolean; readyState: number; host: string; name: string; }
|
|
222
249
|
```
|
|
223
250
|
|
|
251
|
+
### logger
|
|
252
|
+
|
|
253
|
+
Winston logger instance with JSON formatting, timestamps, and error stack traces.
|
|
254
|
+
|
|
255
|
+
```typescript
|
|
256
|
+
logger: winston.Logger
|
|
257
|
+
```
|
|
258
|
+
|
|
224
259
|
## Development
|
|
225
260
|
|
|
226
261
|
```bash
|
package/dist/errorHandler.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { AppError } from "./AppError.js";
|
|
2
|
+
import { logger } from "./logger.js";
|
|
2
3
|
/**
|
|
3
4
|
* Sends detailed error information in development mode.
|
|
4
5
|
* @param {any} err - The error object.
|
|
@@ -29,7 +30,7 @@ const sendErrorProd = (err, res) => {
|
|
|
29
30
|
}
|
|
30
31
|
else {
|
|
31
32
|
// Unknown error → hide details
|
|
32
|
-
|
|
33
|
+
logger.error("Unknown error occurred", err);
|
|
33
34
|
res.status(500).json({
|
|
34
35
|
status: "error",
|
|
35
36
|
message: "Something went wrong!",
|
|
@@ -47,7 +48,7 @@ const errorHandler = (err, req, res, next) => {
|
|
|
47
48
|
err.statusCode = err.statusCode || 500;
|
|
48
49
|
err.status = err.status || "error";
|
|
49
50
|
// Add this for Extra In-Depth Error Logging
|
|
50
|
-
|
|
51
|
+
logger.error("Error details", {
|
|
51
52
|
message: err.message,
|
|
52
53
|
statusCode: err.statusCode,
|
|
53
54
|
errors: err.errors,
|
package/dist/index.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
export { AppError } from "./AppError.js";
|
|
2
2
|
export { catchAsync } from "./catchAsync.js";
|
|
3
3
|
export { errorHandler } from "./errorHandler.js";
|
|
4
|
+
export { logger } from "./logger.js";
|
|
4
5
|
export { sendSuccess, sendError } from "./responseFormatter.js";
|
|
5
|
-
export {
|
|
6
|
-
export { default as connectDB, getDBStatus, DatabaseConnection } from "./DatabaseConnection.js";
|
|
6
|
+
export { default as connectDB, getDBStatus, DatabaseConnection, } from "./DatabaseConnection.js";
|
package/dist/index.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
export { AppError } from "./AppError.js";
|
|
2
2
|
export { catchAsync } from "./catchAsync.js";
|
|
3
3
|
export { errorHandler } from "./errorHandler.js";
|
|
4
|
+
export { logger } from "./logger.js";
|
|
4
5
|
export { sendSuccess, sendError } from "./responseFormatter.js";
|
|
5
|
-
export {
|
|
6
|
-
export { default as connectDB, getDBStatus, DatabaseConnection } from "./DatabaseConnection.js";
|
|
6
|
+
export { default as connectDB, getDBStatus, DatabaseConnection, } from "./DatabaseConnection.js";
|
package/dist/logger.d.ts
ADDED
package/dist/logger.js
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import winston from "winston";
|
|
2
|
+
export const logger = winston.createLogger({
|
|
3
|
+
level: process.env.NODE_ENV === "production" ? "info" : "debug",
|
|
4
|
+
format: winston.format.combine(winston.format.timestamp(), winston.format.errors({ stack: true }), winston.format.splat(), winston.format.json()),
|
|
5
|
+
defaultMeta: { service: process.env.SERVICE_NAME || "express-utils" },
|
|
6
|
+
transports: [
|
|
7
|
+
new winston.transports.Console({
|
|
8
|
+
format: winston.format.combine(winston.format.colorize(), winston.format.simple()),
|
|
9
|
+
}),
|
|
10
|
+
...(process.env.NODE_ENV !== "production" ? [
|
|
11
|
+
new winston.transports.File({ filename: "error.log", level: "error" }),
|
|
12
|
+
new winston.transports.File({ filename: "combined.log" }),
|
|
13
|
+
] : []),
|
|
14
|
+
],
|
|
15
|
+
});
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "devdad-express-utils",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.4.0",
|
|
4
4
|
"description": "Reusable Express.js utilities for error handling, async wrapping, and more",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"types": "dist/index.d.ts",
|
|
@@ -29,7 +29,8 @@
|
|
|
29
29
|
"@types/mongoose": "^5.11.96",
|
|
30
30
|
"express": "^5.1.0",
|
|
31
31
|
"jsonwebtoken": "^9.0.2",
|
|
32
|
-
"mongoose": "^9.0.0"
|
|
32
|
+
"mongoose": "^9.0.0",
|
|
33
|
+
"winston": "^3.17.0"
|
|
33
34
|
},
|
|
34
35
|
"devDependencies": {
|
|
35
36
|
"@types/express": "^5.0.5",
|