http-system-logger 1.0.3 → 1.0.5

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 ADDED
@@ -0,0 +1,96 @@
1
+ # HTTP System Logger
2
+
3
+ A lightweight and configurable logging library for Node.js applications, built with `winston` and `morgan`.
4
+
5
+ ## Features
6
+
7
+ - Console and file-based logging with daily rotation.
8
+ - HTTP request logging using `morgan`.
9
+ - Customizable log levels and formats.
10
+ - Supports structured JSON logs for better integration with log management systems.
11
+ - Ability to ignore logs from specific routes.
12
+
13
+ ## Installation
14
+
15
+ Install the library and its dependencies:
16
+
17
+ ```bash
18
+ npm install http-system-logger
19
+ ```
20
+
21
+ ## Usage
22
+
23
+ ### Logger Class
24
+
25
+ The `Logger` class provides methods for logging messages at different levels (`info`, `error`, `warn`, `debug`).
26
+
27
+ #### Example:
28
+
29
+ ```typescript
30
+ import { Logger } from 'http-system-logger';
31
+
32
+ const logger = new Logger('MyAppContext');
33
+
34
+ // Log an informational message
35
+ logger.info('This is an info message');
36
+
37
+ // Log an error with a stack trace
38
+ logger.error('An error occurred', 'Error stack trace');
39
+
40
+ // Log a warning
41
+ logger.warn('This is a warning');
42
+
43
+ // Log a debug message
44
+ logger.debug('Debugging details');
45
+ ```
46
+
47
+ ### HTTP Request Logging
48
+
49
+ The library includes a `httpLogger` middleware for logging HTTP requests in Express applications.
50
+
51
+ #### Example:
52
+
53
+ ```typescript
54
+ import express from 'express';
55
+ import { httpLogger } from 'http-system-logger';
56
+
57
+ const app = express();
58
+
59
+ // Use the HTTP logger middleware
60
+ app.use(httpLogger);
61
+
62
+ app.get('/', (req, res) => {
63
+ res.send('Hello, world!');
64
+ });
65
+
66
+ app.listen(3000, () => {
67
+ console.log('Server is running on port 3000');
68
+ });
69
+ ```
70
+
71
+ ### Ignoring Specific Routes
72
+
73
+ You can configure the logger to ignore specific routes by setting the `IGNORED_ROUTES` environment variable. Provide a comma-separated list of routes to ignore.
74
+
75
+ #### Example:
76
+
77
+ ```bash
78
+ export IGNORED_ROUTES=/api/health,/api/skip
79
+ ```
80
+
81
+ In this example, requests to `/api/health` and `/api/skip` will not be logged.
82
+
83
+ ### Environment Variables
84
+
85
+ You can configure the logger using the following environment variables:
86
+
87
+ | Variable | Default Value | Description |
88
+ |--------------------|---------------|--------------------------------------------------|
89
+ | `LOG_DIR` | `logs` | Directory where log files are stored. |
90
+ | `LOG_MAX_SIZE` | `4m` | Maximum size of a log file before rotation. |
91
+ | `LOG_MAX_FILES` | `7d` | Maximum number of log files to retain. |
92
+ | `LOG_FILE_NAME` | `application` | Base name for log files. |
93
+ | `SERVICE_NAME` | `logger` | Default service name included in log metadata. |
94
+ | `IGNORED_ROUTES` | `/api/health` | Comma-separated list of routes to ignore. |
95
+
96
+
@@ -29,7 +29,13 @@ Object.defineProperty(exports, "__esModule", { value: true });
29
29
  const morgan_1 = __importDefault(require("morgan"));
30
30
  const winston_config_1 = __importDefault(require("./winston.config"));
31
31
  const uuid = __importStar(require("uuid"));
32
+ const ignoredRoutes = (process.env.IGNORED_ROUTES || '/api/health')
33
+ .split(',')
34
+ .map((route) => route.trim()); // Load ignored routes from environment variables
32
35
  const morganMiddleware = (0, morgan_1.default)(function (tokens, req, res) {
36
+ if (ignoredRoutes.includes(req.path)) {
37
+ return null; // Skip logging for ignored routes
38
+ }
33
39
  return JSON.stringify({
34
40
  method: tokens.method(req, res),
35
41
  url: tokens.url(req, res),
@@ -45,7 +51,9 @@ const morganMiddleware = (0, morgan_1.default)(function (tokens, req, res) {
45
51
  stream: {
46
52
  // Configure Morgan to use our custom logger with the http severity
47
53
  write: (message) => {
48
- winston_config_1.default.http(message, { context: 'HttpContext' });
54
+ if (message) {
55
+ winston_config_1.default.http(message, { context: 'HttpContext' });
56
+ }
49
57
  },
50
58
  },
51
59
  });
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,27 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ const express_1 = __importDefault(require("express"));
7
+ const src_1 = require("../src");
8
+ const app = (0, express_1.default)();
9
+ const logger = new src_1.Logger('TestApp');
10
+ // Middleware to parse JSON requests
11
+ app.use(express_1.default.json());
12
+ // Use the HTTP logger middleware
13
+ app.use(src_1.httpLogger);
14
+ // Test route
15
+ app.get('/api/test', (req, res) => {
16
+ logger.info('Test route accessed');
17
+ res.send('Test route is working!');
18
+ });
19
+ // Ignored route
20
+ app.get('/api/health', (req, res) => {
21
+ res.send('Health check route');
22
+ });
23
+ // Start the server
24
+ const PORT = 3000;
25
+ app.listen(PORT, () => {
26
+ logger.info(`Test server is running on http://localhost:${PORT}`);
27
+ });
package/package.json CHANGED
@@ -1,35 +1,49 @@
1
- {
2
- "name": "http-system-logger",
3
- "version": "1.0.3",
4
- "description": "",
5
- "main": "dist/index.js",
6
- "types": "dist/index.d.ts",
7
- "files": [
8
- "dist"
9
- ],
10
- "scripts": {
11
- "test": "echo \"Error: no test specified\" && exit 1",
12
- "build": "npx tsc"
13
- },
14
- "keywords": [
15
- "node-logger",
16
- "logger",
17
- "http-logger"
18
- ],
19
- "author": "erik",
20
- "license": "ISC",
21
- "devDependencies": {
22
- "@types/express": "^4.17.21",
23
- "@types/morgan": "^1.9.9",
24
- "@types/node": "^20.12.5",
25
- "@types/uuid": "^9.0.8",
26
- "typescript": "^5.4.4"
27
- },
28
- "dependencies": {
29
- "morgan": "^1.10.0",
30
- "uuid": "^9.0.1",
31
- "winston": "^3.13.0",
32
- "winston-daily-rotate-file": "^5.0.0",
33
- "winston-transport": "^4.7.0"
34
- }
35
- }
1
+ {
2
+ "name": "http-system-logger",
3
+ "version": "1.0.5",
4
+ "description": "A lightweight and configurable logging library for Node.js applications, built with winston and morgan.",
5
+ "main": "dist/index.js",
6
+ "types": "dist/index.d.ts",
7
+ "files": [
8
+ "dist"
9
+ ],
10
+ "scripts": {
11
+ "test": "npx ts-node test/index.ts",
12
+ "build": "npx tsc",
13
+ "prepare": "npm run build"
14
+ },
15
+ "keywords": [
16
+ "node-logger",
17
+ "logger",
18
+ "http-logger",
19
+ "winston",
20
+ "morgan",
21
+ "typescript"
22
+ ],
23
+ "author": "erik",
24
+ "license": "ISC",
25
+ "repository": {
26
+ "type": "git",
27
+ "url": "https://github.com/buicuong1303/http-system-logger.git"
28
+ },
29
+ "bugs": {
30
+ "url": "https://github.com/buicuong1303/http-system-logger/issues"
31
+ },
32
+ "homepage": "https://github.com/buicuong1303/http-system-logger#README",
33
+ "devDependencies": {
34
+ "@types/express": "^4.17.21",
35
+ "@types/morgan": "^1.9.9",
36
+ "@types/node": "^20.12.5",
37
+ "@types/uuid": "^9.0.8",
38
+ "ts-node": "^10.9.2",
39
+ "typescript": "^5.4.4",
40
+ "express": "^4.18.2"
41
+ },
42
+ "dependencies": {
43
+ "morgan": "^1.10.0",
44
+ "uuid": "^9.0.1",
45
+ "winston": "^3.13.0",
46
+ "winston-daily-rotate-file": "^5.0.0",
47
+ "winston-transport": "^4.7.0"
48
+ }
49
+ }
File without changes
File without changes