@emartech/json-logger 7.2.0 → 7.2.1

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,27 +1,15 @@
1
1
  import { Timer } from '../timer/timer';
2
- interface AxiosError extends Error {
3
- isAxiosError: boolean;
4
- config: {
5
- method: string;
6
- url: string;
7
- };
8
- response?: {
9
- status: number;
10
- statusText: string;
11
- data: string;
12
- };
13
- }
14
2
  export interface LoggerConfig {
15
3
  formatter: Function;
16
4
  output: Function;
17
5
  transformers: Function[];
18
6
  }
19
7
  export declare class Logger {
20
- _namespace: string;
21
- _enabled: boolean;
8
+ private readonly namespace;
9
+ private readonly enabled;
22
10
  constructor(namespace: string, enabled: boolean);
23
11
  static configure(options: Partial<LoggerConfig>): void;
24
- static _validate(options: Partial<LoggerConfig>): void;
12
+ private static validate;
25
13
  static config: LoggerConfig;
26
14
  isEnabled(): boolean;
27
15
  trace(action: string, data?: unknown): void;
@@ -30,26 +18,13 @@ export declare class Logger {
30
18
  warn(action: string, data?: unknown): void;
31
19
  error(action: string, data?: unknown): void;
32
20
  fatal(action: string, data?: unknown): void;
33
- _log(level: string, action: string, data: unknown): void;
34
21
  customError(severity: string, action: string, error: Error, data?: unknown): void;
35
22
  fromError(action: string, error: unknown, data?: unknown): void;
36
23
  warnFromError(action: string, error: unknown, data?: unknown): void;
37
24
  timer(): Timer;
38
- _shortenStackTrace(stack: string): string | undefined;
39
- _shortenData(data: unknown): string | undefined;
40
- _getErrorDetails(error: Error): {};
41
- _getAxiosErrorDetails(error: AxiosError): {
42
- request_method?: undefined;
43
- request_url?: undefined;
44
- response_status?: undefined;
45
- response_status_text?: undefined;
46
- response_data?: undefined;
47
- } | {
48
- request_method: string;
49
- request_url: string;
50
- response_status: number | undefined;
51
- response_status_text: string | undefined;
52
- response_data: string | undefined;
53
- };
25
+ private log;
26
+ private shortenStackTrace;
27
+ private shortenData;
28
+ private getErrorDetails;
29
+ private getAxiosErrorDetails;
54
30
  }
55
- export {};
@@ -10,14 +10,14 @@ const console_1 = require("../output/console");
10
10
  const allowedKeys = ['output', 'formatter', 'transformers'];
11
11
  class Logger {
12
12
  constructor(namespace, enabled) {
13
- this._namespace = namespace;
14
- this._enabled = enabled;
13
+ this.namespace = namespace;
14
+ this.enabled = enabled;
15
15
  }
16
16
  static configure(options) {
17
- this._validate(options);
17
+ this.validate(options);
18
18
  Object.assign(Logger.config, options);
19
19
  }
20
- static _validate(options) {
20
+ static validate(options) {
21
21
  Object.keys(options).forEach((key) => {
22
22
  if (!allowedKeys.includes(key)) {
23
23
  throw new Error('Only the following keys are allowed: formatter, output');
@@ -25,32 +25,44 @@ class Logger {
25
25
  });
26
26
  }
27
27
  isEnabled() {
28
- return this._enabled;
28
+ return this.enabled;
29
29
  }
30
30
  trace(action, data = {}) {
31
- this._log('trace', action, data);
31
+ this.log('trace', action, data);
32
32
  }
33
33
  debug(action, data = {}) {
34
- this._log('debug', action, data);
34
+ this.log('debug', action, data);
35
35
  }
36
36
  info(action, data = {}) {
37
- this._log('info', action, data);
37
+ this.log('info', action, data);
38
38
  }
39
39
  warn(action, data = {}) {
40
- this._log('warn', action, data);
40
+ this.log('warn', action, data);
41
41
  }
42
42
  error(action, data = {}) {
43
- this._log('error', action, data);
43
+ this.log('error', action, data);
44
44
  }
45
45
  fatal(action, data = {}) {
46
- this._log('fatal', action, data);
46
+ this.log('fatal', action, data);
47
47
  }
48
- _log(level, action, data) {
49
- if (!this._enabled) {
48
+ customError(severity, action, error, data = {}) {
49
+ this.log(severity, action, Object.assign(this.getErrorDetails(error), data));
50
+ }
51
+ fromError(action, error, data = {}) {
52
+ this.customError('error', action, error, data);
53
+ }
54
+ warnFromError(action, error, data = {}) {
55
+ this.customError('warn', action, error, data);
56
+ }
57
+ timer() {
58
+ return new timer_1.Timer(this);
59
+ }
60
+ log(level, action, data) {
61
+ if (!this.enabled) {
50
62
  return;
51
63
  }
52
64
  let dataToLog = Object.assign({
53
- name: this._namespace,
65
+ name: this.namespace,
54
66
  action: action,
55
67
  level: config_1.config.levels[level].number,
56
68
  time: new Date().toISOString(),
@@ -60,44 +72,32 @@ class Logger {
60
72
  });
61
73
  Logger.config.output(Logger.config.formatter(dataToLog));
62
74
  }
63
- customError(severity, action, error, data = {}) {
64
- this._log(severity, action, Object.assign(this._getErrorDetails(error), data));
65
- }
66
- fromError(action, error, data = {}) {
67
- this.customError('error', action, error, data);
68
- }
69
- warnFromError(action, error, data = {}) {
70
- this.customError('warn', action, error, data);
71
- }
72
- timer() {
73
- return new timer_1.Timer(this);
74
- }
75
- _shortenStackTrace(stack) {
75
+ shortenStackTrace(stack) {
76
76
  if (!stack) {
77
77
  return;
78
78
  }
79
79
  return stack.length > STACK_TRACE_LIMIT ? stack.substring(0, STACK_TRACE_LIMIT) + ' ...' : stack;
80
80
  }
81
- _shortenData(data) {
81
+ shortenData(data) {
82
82
  if (typeof data === 'undefined') {
83
83
  return;
84
84
  }
85
85
  const stringifiedData = typeof data === 'object' ? JSON.stringify(data) : data;
86
86
  return stringifiedData.length > DATA_LIMIT ? stringifiedData.substring(0, DATA_LIMIT) + ' ...' : stringifiedData;
87
87
  }
88
- _getErrorDetails(error) {
88
+ getErrorDetails(error) {
89
89
  if (!(error instanceof Object)) {
90
90
  return {};
91
91
  }
92
92
  const baseDetails = {
93
93
  error_name: error.name,
94
- error_stack: this._shortenStackTrace(error.stack || ''),
94
+ error_stack: this.shortenStackTrace(error.stack || ''),
95
95
  error_message: error.message,
96
- error_data: this._shortenData(error.data),
96
+ error_data: this.shortenData(error.data),
97
97
  };
98
- return Object.assign(baseDetails, this._getAxiosErrorDetails(error));
98
+ return Object.assign(baseDetails, this.getAxiosErrorDetails(error));
99
99
  }
100
- _getAxiosErrorDetails(error) {
100
+ getAxiosErrorDetails(error) {
101
101
  if (!error.isAxiosError) {
102
102
  return {};
103
103
  }
@@ -106,7 +106,7 @@ class Logger {
106
106
  request_url: error.config.url,
107
107
  response_status: error.response ? error.response.status : undefined,
108
108
  response_status_text: error.response ? error.response.statusText : undefined,
109
- response_data: error.response ? this._shortenData(error.response.data) : undefined,
109
+ response_data: error.response ? this.shortenData(error.response.data) : undefined,
110
110
  };
111
111
  }
112
112
  }
@@ -1,7 +1,7 @@
1
1
  import { Logger } from '../logger/logger';
2
2
  export declare class Timer {
3
- _logger: Logger;
4
- _start: number;
3
+ private readonly start;
4
+ private readonly logger;
5
5
  constructor(logger: Logger);
6
6
  trace(action: string, data?: unknown): void;
7
7
  debug(action: string, data?: unknown): void;
@@ -11,5 +11,5 @@ export declare class Timer {
11
11
  fatal(action: string, data?: unknown): void;
12
12
  fromError(action: string, error: unknown, data?: unknown): void;
13
13
  warnFromError(action: string, error: unknown, data?: unknown): void;
14
- _duration(): number;
14
+ private duration;
15
15
  }
@@ -3,36 +3,36 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.Timer = void 0;
4
4
  class Timer {
5
5
  constructor(logger) {
6
- this._logger = logger;
7
- this._start = new Date().getTime();
6
+ this.logger = logger;
7
+ this.start = new Date().getTime();
8
8
  }
9
9
  trace(action, data = {}) {
10
- this._logger.trace(action, Object.assign({ duration: this._duration() }, data));
10
+ this.logger.trace(action, Object.assign({ duration: this.duration() }, data));
11
11
  }
12
12
  debug(action, data = {}) {
13
- this._logger.debug(action, Object.assign({ duration: this._duration() }, data));
13
+ this.logger.debug(action, Object.assign({ duration: this.duration() }, data));
14
14
  }
15
15
  info(action, data = {}) {
16
- this._logger.info(action, Object.assign({ duration: this._duration() }, data));
16
+ this.logger.info(action, Object.assign({ duration: this.duration() }, data));
17
17
  }
18
18
  warn(action, data = {}) {
19
- this._logger.warn(action, Object.assign({ duration: this._duration() }, data));
19
+ this.logger.warn(action, Object.assign({ duration: this.duration() }, data));
20
20
  }
21
21
  error(action, data = {}) {
22
- this._logger.error(action, Object.assign({ duration: this._duration() }, data));
22
+ this.logger.error(action, Object.assign({ duration: this.duration() }, data));
23
23
  }
24
24
  fatal(action, data = {}) {
25
- this._logger.fatal(action, Object.assign({ duration: this._duration() }, data));
25
+ this.logger.fatal(action, Object.assign({ duration: this.duration() }, data));
26
26
  }
27
27
  fromError(action, error, data = {}) {
28
- this._logger.fromError(action, error, Object.assign({ duration: this._duration() }, data));
28
+ this.logger.fromError(action, error, Object.assign({ duration: this.duration() }, data));
29
29
  }
30
30
  warnFromError(action, error, data = {}) {
31
- this._logger.warnFromError(action, error, Object.assign({ duration: this._duration() }, data));
31
+ this.logger.warnFromError(action, error, Object.assign({ duration: this.duration() }, data));
32
32
  }
33
- _duration() {
33
+ duration() {
34
34
  const end = new Date().getTime();
35
- return end - this._start;
35
+ return end - this.start;
36
36
  }
37
37
  }
38
38
  exports.Timer = Timer;
package/package.json CHANGED
@@ -60,5 +60,5 @@
60
60
  "url": "https://github.com/emartech/json-logger-js/issues"
61
61
  },
62
62
  "homepage": "https://github.com/emartech/json-logger-js#readme",
63
- "version": "7.2.0"
63
+ "version": "7.2.1"
64
64
  }