http-system-logger 1.0.5 → 1.0.7

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.
@@ -1,5 +1,6 @@
1
1
  export declare class Logger {
2
2
  private readonly context;
3
+ private readonly logger;
3
4
  constructor(context: string);
4
5
  info(message: string, context?: string): void;
5
6
  error(message: string, trace?: string, context?: string): void;
@@ -4,22 +4,23 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
4
4
  };
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
6
  exports.httpLogger = exports.Logger = void 0;
7
- const winston_config_1 = __importDefault(require("./winston.config"));
7
+ const winston_config_1 = require("./winston.config");
8
8
  class Logger {
9
9
  constructor(context) {
10
10
  this.context = context;
11
+ this.logger = (0, winston_config_1.getLogger)();
11
12
  }
12
13
  info(message, context) {
13
- winston_config_1.default.info(message, { context: context || this.context });
14
+ this.logger.info(message, { context: context || this.context });
14
15
  }
15
16
  error(message, trace = '', context) {
16
- winston_config_1.default.error(message, { context: context || this.context, trace });
17
+ this.logger.error(message, { context: context || this.context, trace });
17
18
  }
18
19
  warn(message, context) {
19
- winston_config_1.default.warn(message, { context: context || this.context });
20
+ this.logger.warn(message, { context: context || this.context });
20
21
  }
21
22
  debug(message, context) {
22
- winston_config_1.default.debug(message, { context: context || this.context });
23
+ this.logger.debug(message, { context: context || this.context });
23
24
  }
24
25
  }
25
26
  exports.Logger = Logger;
@@ -27,7 +27,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
27
27
  };
28
28
  Object.defineProperty(exports, "__esModule", { value: true });
29
29
  const morgan_1 = __importDefault(require("morgan"));
30
- const winston_config_1 = __importDefault(require("./winston.config"));
30
+ const winston_config_1 = require("./winston.config");
31
31
  const uuid = __importStar(require("uuid"));
32
32
  const ignoredRoutes = (process.env.IGNORED_ROUTES || '/api/health')
33
33
  .split(',')
@@ -52,7 +52,7 @@ const morganMiddleware = (0, morgan_1.default)(function (tokens, req, res) {
52
52
  // Configure Morgan to use our custom logger with the http severity
53
53
  write: (message) => {
54
54
  if (message) {
55
- winston_config_1.default.http(message, { context: 'HttpContext' });
55
+ (0, winston_config_1.getLogger)().http(message, { context: 'HttpContext' });
56
56
  }
57
57
  },
58
58
  },
@@ -1,4 +1,3 @@
1
1
  import * as winston from 'winston';
2
2
  import 'winston-daily-rotate-file';
3
- declare const _default: winston.Logger;
4
- export default _default;
3
+ export declare const getLogger: () => winston.Logger;
@@ -0,0 +1,76 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || function (mod) {
19
+ if (mod && mod.__esModule) return mod;
20
+ var result = {};
21
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
+ __setModuleDefault(result, mod);
23
+ return result;
24
+ };
25
+ Object.defineProperty(exports, "__esModule", { value: true });
26
+ exports.getLogger = void 0;
27
+ const winston = __importStar(require("winston"));
28
+ require("winston-daily-rotate-file");
29
+ let loggerInstance = null;
30
+ const createLoggerInstance = () => {
31
+ const logDir = process.env.LOG_DIR || 'logs';
32
+ const maxSize = process.env.LOG_MAX_SIZE || '4m';
33
+ const maxFiles = process.env.LOG_MAX_FILES || '7d';
34
+ const fileName = process.env.LOG_FILE_NAME || 'application';
35
+ const transports = [
36
+ new winston.transports.Console({
37
+ level: 'http',
38
+ format: winston.format.combine(winston.format.timestamp({ format: 'YYYY-MM-DD hh:mm:ss A' }), winston.format.colorize(), winston.format.printf(({ timestamp, level, message, context, trace, service, stack }) => {
39
+ return `[\x1b[34m${timestamp}\x1b[0m] [\x1b[36m${service}\x1b[0m] [\x1b[33m${context}\x1b[0m] [${level}]: ${message}${stack ? `\n${stack}` : ''}${trace ? `\n${trace}` : ''}`;
40
+ })),
41
+ }),
42
+ new winston.transports.DailyRotateFile({
43
+ level: 'error',
44
+ filename: `${logDir}/${fileName}-error-%DATE%`,
45
+ extension: '.log',
46
+ datePattern: 'YYYY-MM-DD',
47
+ maxSize,
48
+ maxFiles,
49
+ format: winston.format.combine(winston.format.timestamp(), winston.format.json()),
50
+ }),
51
+ new winston.transports.DailyRotateFile({
52
+ level: 'http',
53
+ filename: `${logDir}/${fileName}-info-%DATE%`,
54
+ extension: '.log',
55
+ datePattern: 'YYYY-MM-DD',
56
+ maxSize,
57
+ maxFiles,
58
+ format: winston.format.combine(winston.format.timestamp(), winston.format.json()),
59
+ }),
60
+ ];
61
+ return winston.createLogger({
62
+ level: 'http',
63
+ format: winston.format.combine(winston.format.json(), winston.format.errors({ stack: true })),
64
+ transports,
65
+ defaultMeta: {
66
+ service: process.env.SERVICE_NAME || 'logger',
67
+ },
68
+ });
69
+ };
70
+ const getLogger = () => {
71
+ if (!loggerInstance) {
72
+ loggerInstance = createLoggerInstance();
73
+ }
74
+ return loggerInstance;
75
+ };
76
+ exports.getLogger = getLogger;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "http-system-logger",
3
- "version": "1.0.5",
3
+ "version": "1.0.7",
4
4
  "description": "A lightweight and configurable logging library for Node.js applications, built with winston and morgan.",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -1,71 +0,0 @@
1
- "use strict";
2
- var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
- if (k2 === undefined) k2 = k;
4
- var desc = Object.getOwnPropertyDescriptor(m, k);
5
- if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
- desc = { enumerable: true, get: function() { return m[k]; } };
7
- }
8
- Object.defineProperty(o, k2, desc);
9
- }) : (function(o, m, k, k2) {
10
- if (k2 === undefined) k2 = k;
11
- o[k2] = m[k];
12
- }));
13
- var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
- Object.defineProperty(o, "default", { enumerable: true, value: v });
15
- }) : function(o, v) {
16
- o["default"] = v;
17
- });
18
- var __importStar = (this && this.__importStar) || function (mod) {
19
- if (mod && mod.__esModule) return mod;
20
- var result = {};
21
- if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
- __setModuleDefault(result, mod);
23
- return result;
24
- };
25
- Object.defineProperty(exports, "__esModule", { value: true });
26
- const winston = __importStar(require("winston"));
27
- require("winston-daily-rotate-file");
28
- const logDir = process.env.LOG_DIR || 'logs';
29
- const maxSize = process.env.LOG_MAX_SIZE || '4m';
30
- const maxFiles = process.env.LOG_MAX_FILES || '7d';
31
- const fileName = process.env.LOG_FILE_NAME || 'application';
32
- const transports = [
33
- new winston.transports.Console({
34
- level: 'http',
35
- format: winston.format.combine(
36
- // Add a timestamp to the console logs
37
- winston.format.timestamp({ format: 'YYYY-MM-DD hh:mm:ss A' }),
38
- // Add colors to you logs
39
- winston.format.colorize(),
40
- // What the details you need as logs
41
- winston.format.printf(({ timestamp, level, message, context, trace, service, stack }) => {
42
- return `[\x1b[34m${timestamp}\x1b[0m] [\x1b[36m${service}\x1b[0m] [\x1b[33m${context}\x1b[0m] [${level}]: ${message}${stack ? `\n${stack}` : ''}${trace ? `\n${trace}` : ''}`;
43
- })),
44
- }),
45
- new winston.transports.DailyRotateFile({
46
- level: 'error',
47
- filename: `${logDir}/${fileName}-error-%DATE%`,
48
- extension: '.log',
49
- datePattern: 'YYYY-MM-DD',
50
- maxSize,
51
- maxFiles,
52
- format: winston.format.combine(winston.format.timestamp(), winston.format.json()),
53
- }),
54
- new winston.transports.DailyRotateFile({
55
- level: 'http',
56
- filename: `${logDir}/${fileName}-info-%DATE%`,
57
- extension: '.log',
58
- datePattern: 'YYYY-MM-DD',
59
- maxSize,
60
- maxFiles,
61
- format: winston.format.combine(winston.format.timestamp(), winston.format.json()),
62
- }),
63
- ];
64
- exports.default = winston.createLogger({
65
- level: 'http',
66
- format: winston.format.combine(winston.format.json(), winston.format.errors({ stack: true })),
67
- transports,
68
- defaultMeta: {
69
- service: process.env.SERVICE_NAME || 'logger',
70
- },
71
- });
@@ -1 +0,0 @@
1
- export {};
@@ -1,27 +0,0 @@
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
- });