@emartech/json-logger 7.2.0 → 7.2.2

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.2"
64
64
  }
@@ -1,55 +0,0 @@
1
- name: Initialize build
2
-
3
- on:
4
- workflow_call:
5
-
6
- outputs:
7
- NODE_VERSION:
8
- value: ${{ jobs.init.outputs.NODE_VERSION }}
9
- NODE_CACHE_KEY:
10
- value: ${{ jobs.init.outputs.NODE_CACHE_KEY }}
11
-
12
- jobs:
13
- init:
14
- name: Initialize build
15
- runs-on: ubuntu-latest
16
-
17
- outputs:
18
- NODE_VERSION: ${{ steps.node-version.outputs.NODE_VERSION }}
19
- NODE_CACHE_KEY: ${{ steps.node-cache-key.outputs.NODE_CACHE_KEY }}
20
-
21
- steps:
22
- - name: Checkout code
23
- uses: actions/checkout@v3
24
- with:
25
- fetch-depth: 0
26
-
27
- - name: Get node version
28
- id: node-version
29
- run: echo ::set-output name=NODE_VERSION::$(cat .nvmrc)
30
-
31
- - name: Get node cache key
32
- id: node-cache-key
33
- run: echo ::set-output name=NODE_CACHE_KEY::npm-${{ steps.node-version.outputs.NODE_VERSION }}-${{ hashFiles('**/package.json') }}
34
-
35
- - name: Cache dependencies
36
- id: cache-node-modules
37
- uses: actions/cache@v3
38
- with:
39
- path: node_modules
40
- key: ${{ steps.node-cache-key.outputs.NODE_CACHE_KEY }}
41
-
42
- - name: Setup node
43
- if: steps.cache-node-modules.outputs.cache-hit != 'true'
44
- uses: actions/setup-node@v3
45
- with:
46
- node-version: '${{ steps.node-version.outputs.NODE_VERSION }}'
47
-
48
- - name: Install dependencies
49
- if: steps.cache-node-modules.outputs.cache-hit != 'true'
50
- run: npm install
51
-
52
- - name: Log results
53
- run: |
54
- echo "NODE_VERSION: ${{ steps.node-version.outputs.NODE_VERSION }}"
55
- echo "NODE_CACHE_KEY: ${{ steps.node-cache-key.outputs.NODE_CACHE_KEY }}"
@@ -1,46 +0,0 @@
1
- name: Release package
2
-
3
- on:
4
- workflow_call:
5
- inputs:
6
- NODE_VERSION:
7
- type: string
8
- required: true
9
- NODE_CACHE_KEY:
10
- type: string
11
- required: true
12
-
13
- secrets:
14
- SEMANTIC_RELEASE_NPM_TOKEN:
15
- required: true
16
- SEMANTIC_RELEASE_GH_TOKEN:
17
- required: true
18
-
19
- jobs:
20
- test:
21
- name: Release package
22
- runs-on: ubuntu-latest
23
-
24
- steps:
25
- - name: Checkout code
26
- uses: actions/checkout@v3
27
-
28
- - name: Load dependencies from cache
29
- uses: actions/cache@v3
30
- with:
31
- path: node_modules
32
- key: ${{ inputs.NODE_CACHE_KEY }}
33
-
34
- - name: Setup node
35
- uses: actions/setup-node@v3
36
- with:
37
- node-version: '${{ inputs.NODE_VERSION }}'
38
-
39
- - name: Run build
40
- run: npm run build
41
-
42
- - name: Run release
43
- run: npm run release
44
- env:
45
- NPM_TOKEN: ${{ secrets.SEMANTIC_RELEASE_NPM_TOKEN }}
46
- GH_TOKEN: ${{ secrets.SEMANTIC_RELEASE_GH_TOKEN }}
@@ -1,39 +0,0 @@
1
- name: Run tests
2
-
3
- on:
4
- workflow_call:
5
- inputs:
6
- NODE_VERSION:
7
- type: string
8
- required: true
9
- NODE_CACHE_KEY:
10
- type: string
11
- required: true
12
-
13
- jobs:
14
- test:
15
- name: Run tests
16
- runs-on: ubuntu-latest
17
-
18
- strategy:
19
- fail-fast: false
20
- matrix:
21
- test: [lint, test]
22
-
23
- steps:
24
- - name: Checkout code
25
- uses: actions/checkout@v3
26
-
27
- - name: Load dependencies from cache
28
- uses: actions/cache@v3
29
- with:
30
- path: node_modules
31
- key: ${{ inputs.NODE_CACHE_KEY }}
32
-
33
- - name: Setup node
34
- uses: actions/setup-node@v3
35
- with:
36
- node-version: '${{ inputs.NODE_VERSION }}'
37
-
38
- - name: Run ${{ matrix.test }} test
39
- run: npm run ${{ matrix.test }}
@@ -1,20 +0,0 @@
1
- name: Development
2
-
3
- on:
4
- push:
5
- branches: [ '*', '*/*', '**', '!master', '!main' ]
6
- pull_request:
7
- branches: [ master, main ]
8
-
9
- jobs:
10
- init:
11
- name: Init
12
- uses: ./.github/workflows/_init.yml
13
-
14
- test:
15
- name: Test
16
- uses: ./.github/workflows/_test.yml
17
- needs: [ init ]
18
- with:
19
- NODE_VERSION: ${{ needs.init.outputs.NODE_VERSION }}
20
- NODE_CACHE_KEY: ${{ needs.init.outputs.NODE_CACHE_KEY }}
@@ -1,29 +0,0 @@
1
- name: Release
2
-
3
- on:
4
- push:
5
- branches: [ master, main ]
6
-
7
- jobs:
8
- init:
9
- name: Init
10
- uses: ./.github/workflows/_init.yml
11
-
12
- test:
13
- name: Test
14
- uses: ./.github/workflows/_test.yml
15
- needs: [ init ]
16
- with:
17
- NODE_VERSION: ${{ needs.init.outputs.NODE_VERSION }}
18
- NODE_CACHE_KEY: ${{ needs.init.outputs.NODE_CACHE_KEY }}
19
-
20
- release:
21
- name: Release
22
- uses: ./.github/workflows/_release.yml
23
- needs: [ init, test ]
24
- with:
25
- NODE_VERSION: ${{ needs.init.outputs.NODE_VERSION }}
26
- NODE_CACHE_KEY: ${{ needs.init.outputs.NODE_CACHE_KEY }}
27
- secrets:
28
- SEMANTIC_RELEASE_NPM_TOKEN: ${{ secrets.SEMANTIC_RELEASE_NPM_TOKEN }}
29
- SEMANTIC_RELEASE_GH_TOKEN: ${{ secrets.SEMANTIC_RELEASE_GH_TOKEN }}
package/main.yml DELETED
@@ -1,30 +0,0 @@
1
- name: Test and Release
2
-
3
- on:
4
- push:
5
- branches: [ master, main ]
6
-
7
- env:
8
- NPM_TOKEN: ${{ secrets.SEMANTIC_RELEASE_NPM_TOKEN }}
9
- GH_TOKEN: ${{ secrets.SEMANTIC_RELEASE_GH_TOKEN }}
10
-
11
- jobs:
12
- init:
13
- name: Init
14
- uses: ./.github/workflows/_init.yml
15
-
16
- test:
17
- name: Test
18
- uses: ./.github/workflows/_test.yml
19
- needs: [ init ]
20
- with:
21
- NODE_VERSION: ${{ needs.init.outputs.NODE_VERSION }}
22
- NODE_CACHE_KEY: ${{ needs.init.outputs.NODE_CACHE_KEY }}
23
-
24
- deploy:
25
- name: Test
26
- uses: ./.github/workflows/_deploy.yml
27
- needs: [ init, test ]
28
- with:
29
- NODE_VERSION: ${{ needs.init.outputs.NODE_VERSION }}
30
- NODE_CACHE_KEY: ${{ needs.init.outputs.NODE_CACHE_KEY }}