http-system-logger 1.0.6 → 1.0.8

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/dist/index.d.ts CHANGED
@@ -1,6 +1,8 @@
1
1
  export declare class Logger {
2
- private readonly context;
2
+ private context;
3
+ private readonly logger;
3
4
  constructor(context: string);
5
+ setContext(context: string): void;
4
6
  info(message: string, context?: string): void;
5
7
  error(message: string, trace?: string, context?: string): void;
6
8
  warn(message: string, context?: string): void;
package/dist/index.js CHANGED
@@ -4,22 +4,26 @@ 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)();
12
+ }
13
+ setContext(context) {
14
+ this.context = context;
11
15
  }
12
16
  info(message, context) {
13
- winston_config_1.default.info(message, { context: context || this.context });
17
+ this.logger.info(message, { context: context || this.context });
14
18
  }
15
19
  error(message, trace = '', context) {
16
- winston_config_1.default.error(message, { context: context || this.context, trace });
20
+ this.logger.error(message, { context: context || this.context, trace });
17
21
  }
18
22
  warn(message, context) {
19
- winston_config_1.default.warn(message, { context: context || this.context });
23
+ this.logger.warn(message, { context: context || this.context });
20
24
  }
21
25
  debug(message, context) {
22
- winston_config_1.default.debug(message, { context: context || this.context });
26
+ this.logger.debug(message, { context: context || this.context });
23
27
  }
24
28
  }
25
29
  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;
@@ -23,49 +23,54 @@ var __importStar = (this && this.__importStar) || function (mod) {
23
23
  return result;
24
24
  };
25
25
  Object.defineProperty(exports, "__esModule", { value: true });
26
+ exports.getLogger = void 0;
26
27
  const winston = __importStar(require("winston"));
27
28
  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({
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({
34
62
  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
- });
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.6",
3
+ "version": "1.0.8",
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",