@emartech/json-logger 7.1.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.
@@ -0,0 +1,20 @@
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,4 +1,4 @@
1
- name: Test and Release
1
+ name: Release
2
2
 
3
3
  on:
4
4
  push:
package/README.md CHANGED
@@ -10,6 +10,7 @@ npm install @emartech/json-logger
10
10
  ```
11
11
 
12
12
 
13
+
13
14
  ### Usage
14
15
 
15
16
  #### Script
@@ -1 +1 @@
1
- export declare function isNamespaceEnabled(availableNamespace: string, namespace: string): boolean;
1
+ export declare const isNamespaceEnabled: (availableNamespace: string, namespace: string) => boolean;
@@ -1,12 +1,12 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.isNamespaceEnabled = void 0;
4
- function isNamespaceEnabled(availableNamespace, namespace) {
4
+ const isNamespaceEnabled = (availableNamespace, namespace) => {
5
5
  const availableNamespaces = availableNamespace.split(/[\s,]+/);
6
6
  let enabled = false;
7
7
  const adds = [];
8
8
  const skips = [];
9
- availableNamespaces.forEach(function (name) {
9
+ availableNamespaces.forEach((name) => {
10
10
  if (!name) {
11
11
  return;
12
12
  }
@@ -18,16 +18,16 @@ function isNamespaceEnabled(availableNamespace, namespace) {
18
18
  adds.push(new RegExp('^' + name + '$'));
19
19
  }
20
20
  });
21
- adds.forEach(function (addRegexp) {
21
+ adds.forEach((addRegexp) => {
22
22
  if (addRegexp.test(namespace)) {
23
23
  enabled = true;
24
24
  }
25
25
  });
26
- skips.forEach(function (addRegexp) {
26
+ skips.forEach((addRegexp) => {
27
27
  if (addRegexp.test(namespace)) {
28
28
  enabled = false;
29
29
  }
30
30
  });
31
31
  return enabled;
32
- }
32
+ };
33
33
  exports.isNamespaceEnabled = isNamespaceEnabled;
@@ -1,6 +1,4 @@
1
- import { jsonFormatter } from './json';
2
- import { logentriesFormatter } from './logentries';
3
1
  export declare const formatter: {
4
- json: typeof jsonFormatter;
5
- logentries: typeof logentriesFormatter;
2
+ json: (log: unknown) => string;
3
+ logentries: (data: Record<string, unknown>) => string;
6
4
  };
@@ -5,5 +5,5 @@ const json_1 = require("./json");
5
5
  const logentries_1 = require("./logentries");
6
6
  exports.formatter = {
7
7
  json: json_1.jsonFormatter,
8
- logentries: logentries_1.logentriesFormatter
8
+ logentries: logentries_1.logentriesFormatter,
9
9
  };
@@ -1 +1 @@
1
- export declare function jsonFormatter(log: unknown): string;
1
+ export declare const jsonFormatter: (log: unknown) => string;
@@ -1,7 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.jsonFormatter = void 0;
4
- function jsonFormatter(log) {
4
+ const jsonFormatter = (log) => {
5
5
  return JSON.stringify(log);
6
- }
6
+ };
7
7
  exports.jsonFormatter = jsonFormatter;
@@ -1 +1 @@
1
- export declare function logentriesFormatter(data: Record<string, unknown>): string;
1
+ export declare const logentriesFormatter: (data: Record<string, unknown>) => string;
@@ -15,10 +15,9 @@ const convertToTag = (value, key) => {
15
15
  }
16
16
  return key + '=' + value;
17
17
  };
18
- function logentriesFormatter(data) {
19
- return Object
20
- .keys(data)
21
- .map(key => convertToTag(data[key], key))
18
+ const logentriesFormatter = (data) => {
19
+ return Object.keys(data)
20
+ .map((key) => convertToTag(data[key], key))
22
21
  .join(' ');
23
- }
22
+ };
24
23
  exports.logentriesFormatter = logentriesFormatter;
package/dist/index.d.ts CHANGED
@@ -6,7 +6,7 @@ export declare namespace createLogger {
6
6
  var getNamespaces: () => string;
7
7
  var configure: (options: LoggerConfig) => void;
8
8
  var formatter: {
9
- json: typeof import("./formatter/json").jsonFormatter;
10
- logentries: typeof import("./formatter/logentries").logentriesFormatter;
9
+ json: (log: unknown) => string;
10
+ logentries: (data: Record<string, unknown>) => string;
11
11
  };
12
12
  }
@@ -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
- static configure(options: LoggerConfig): void;
24
- static _validate(options: LoggerConfig): void;
11
+ static configure(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,58 +10,43 @@ 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) {
21
- Object.keys(options).forEach(key => {
20
+ static validate(options) {
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');
24
24
  }
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);
47
- }
48
- _log(level, action, data) {
49
- if (!this._enabled) {
50
- return;
51
- }
52
- let dataToLog = Object.assign({
53
- name: this._namespace,
54
- action: action,
55
- level: config_1.config.levels[level].number,
56
- time: new Date().toISOString()
57
- }, data);
58
- Logger.config.transformers.forEach((transform) => {
59
- dataToLog = transform(dataToLog);
60
- });
61
- Logger.config.output(Logger.config.formatter(dataToLog));
46
+ this.log('fatal', action, data);
62
47
  }
63
48
  customError(severity, action, error, data = {}) {
64
- this._log(severity, action, Object.assign(this._getErrorDetails(error), data));
49
+ this.log(severity, action, Object.assign(this.getErrorDetails(error), data));
65
50
  }
66
51
  fromError(action, error, data = {}) {
67
52
  this.customError('error', action, error, data);
@@ -72,36 +57,47 @@ class Logger {
72
57
  timer() {
73
58
  return new timer_1.Timer(this);
74
59
  }
75
- _shortenStackTrace(stack) {
60
+ log(level, action, data) {
61
+ if (!this.enabled) {
62
+ return;
63
+ }
64
+ let dataToLog = Object.assign({
65
+ name: this.namespace,
66
+ action: action,
67
+ level: config_1.config.levels[level].number,
68
+ time: new Date().toISOString(),
69
+ }, data);
70
+ Logger.config.transformers.forEach((transform) => {
71
+ dataToLog = transform(dataToLog);
72
+ });
73
+ Logger.config.output(Logger.config.formatter(dataToLog));
74
+ }
75
+ shortenStackTrace(stack) {
76
76
  if (!stack) {
77
77
  return;
78
78
  }
79
- return stack.length > STACK_TRACE_LIMIT ?
80
- stack.substring(0, STACK_TRACE_LIMIT) + ' ...' :
81
- stack;
79
+ return stack.length > STACK_TRACE_LIMIT ? stack.substring(0, STACK_TRACE_LIMIT) + ' ...' : stack;
82
80
  }
83
- _shortenData(data) {
81
+ shortenData(data) {
84
82
  if (typeof data === 'undefined') {
85
83
  return;
86
84
  }
87
85
  const stringifiedData = typeof data === 'object' ? JSON.stringify(data) : data;
88
- return stringifiedData.length > DATA_LIMIT ?
89
- stringifiedData.substring(0, DATA_LIMIT) + ' ...' :
90
- stringifiedData;
86
+ return stringifiedData.length > DATA_LIMIT ? stringifiedData.substring(0, DATA_LIMIT) + ' ...' : stringifiedData;
91
87
  }
92
- _getErrorDetails(error) {
88
+ getErrorDetails(error) {
93
89
  if (!(error instanceof Object)) {
94
90
  return {};
95
91
  }
96
92
  const baseDetails = {
97
93
  error_name: error.name,
98
- error_stack: this._shortenStackTrace(error.stack || ''),
94
+ error_stack: this.shortenStackTrace(error.stack || ''),
99
95
  error_message: error.message,
100
- error_data: this._shortenData(error.data)
96
+ error_data: this.shortenData(error.data),
101
97
  };
102
- return Object.assign(baseDetails, this._getAxiosErrorDetails(error));
98
+ return Object.assign(baseDetails, this.getAxiosErrorDetails(error));
103
99
  }
104
- _getAxiosErrorDetails(error) {
100
+ getAxiosErrorDetails(error) {
105
101
  if (!error.isAxiosError) {
106
102
  return {};
107
103
  }
@@ -110,7 +106,7 @@ class Logger {
110
106
  request_url: error.config.url,
111
107
  response_status: error.response ? error.response.status : undefined,
112
108
  response_status_text: error.response ? error.response.statusText : undefined,
113
- response_data: error.response ? this._shortenData(error.response.data) : undefined
109
+ response_data: error.response ? this.shortenData(error.response.data) : undefined,
114
110
  };
115
111
  }
116
112
  }
@@ -118,5 +114,5 @@ exports.Logger = Logger;
118
114
  Logger.config = {
119
115
  formatter: json_1.jsonFormatter,
120
116
  output: console_1.consoleOutput,
121
- transformers: []
117
+ transformers: [],
122
118
  };
@@ -1 +1 @@
1
- export declare function consoleOutput(formattedLog: unknown): void;
1
+ export declare const consoleOutput: (formattedLog: unknown) => void;
@@ -1,7 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.consoleOutput = void 0;
4
- function consoleOutput(formattedLog) {
4
+ const consoleOutput = (formattedLog) => {
5
5
  console.log(formattedLog);
6
- }
6
+ };
7
7
  exports.consoleOutput = consoleOutput;
@@ -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
@@ -3,9 +3,10 @@
3
3
  "description": "Tiny and fast json logger with namespace support",
4
4
  "main": "dist/index.js",
5
5
  "scripts": {
6
- "test": "mocha --require ts-node/register ./src --recursive",
7
- "test:watch": "mocha --require ts-node/register ./src --recursive --watch",
8
- "lint": "eslint ./src/**/*.{ts,js}",
6
+ "test": "mocha --require ts-node/register --extension ts ./src --recursive",
7
+ "test:watch": "mocha --require ts-node/register --extension ts ./src --recursive --watch",
8
+ "lint": "eslint ./src/**/*.ts",
9
+ "lint:fix": "eslint ./src/**/*.ts --fix",
9
10
  "build": "rm -rf dist && tsc --project ./tsconfig.json",
10
11
  "release": "CI=true semantic-release",
11
12
  "example-js": "DEBUG=* node examples/index-js.js",
@@ -25,21 +26,31 @@
25
26
  "debug",
26
27
  "json"
27
28
  ],
28
- "dependencies": {},
29
29
  "devDependencies": {
30
- "@types/node": "18.7.14",
31
- "@typescript-eslint/parser": "5.36.1",
30
+ "@types/chai": "4.3.3",
31
+ "@types/mocha": "10.0.0",
32
+ "@types/node": "18.7.23",
33
+ "@types/sinon": "10.0.13",
34
+ "@types/sinon-chai": "3.2.8",
35
+ "@typescript-eslint/eslint-plugin": "5.38.1",
36
+ "@typescript-eslint/parser": "5.38.1",
37
+ "axios": "0.27.2",
32
38
  "chai": "4.3.6",
33
- "eslint": "8.23.0",
39
+ "eslint": "8.24.0",
34
40
  "eslint-config-emarsys": "5.1.0",
41
+ "eslint-config-prettier": "8.5.0",
35
42
  "eslint-plugin-no-only-tests": "3.0.0",
43
+ "eslint-plugin-prettier": "4.2.1",
36
44
  "eslint-plugin-security": "1.5.0",
45
+ "install": "0.13.0",
37
46
  "mocha": "10.0.0",
47
+ "npm": "8.19.2",
48
+ "prettier": "2.7.1",
38
49
  "semantic-release": "19.0.5",
39
50
  "sinon": "14.0.0",
40
51
  "sinon-chai": "3.7.0",
41
52
  "ts-node": "10.9.1",
42
- "typescript": "4.8.3"
53
+ "typescript": "4.8.4"
43
54
  },
44
55
  "repository": {
45
56
  "type": "git",
@@ -49,5 +60,5 @@
49
60
  "url": "https://github.com/emartech/json-logger-js/issues"
50
61
  },
51
62
  "homepage": "https://github.com/emartech/json-logger-js#readme",
52
- "version": "7.1.0"
63
+ "version": "7.2.1"
53
64
  }
package/tsconfig.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
- "exclude": ["dist", "node_modules", "src/**/*.js"],
3
- "include": ["src/**/*.ts", "src/**/*.js"],
2
+ "exclude": ["dist", "node_modules"],
3
+ "include": ["src/**/*.ts"],
4
4
  "compilerOptions": {
5
5
  /* Visit https://aka.ms/tsconfig to read more about this file */
6
6
 
@@ -41,7 +41,7 @@
41
41
  // "noResolve": true, /* Disallow 'import's, 'require's or '<reference>'s from expanding the number of files TypeScript should add to a project. */
42
42
 
43
43
  /* JavaScript Support */
44
- "allowJs": true, /* Allow JavaScript files to be a part of your program. Use the 'checkJS' option to get errors from these files. */
44
+ "allowJs": false, /* Allow JavaScript files to be a part of your program. Use the 'checkJS' option to get errors from these files. */
45
45
  // "checkJs": true, /* Enable error reporting in type-checked JavaScript files. */
46
46
  // "maxNodeModuleJsDepth": 1, /* Specify the maximum folder depth used for checking JavaScript files from 'node_modules'. Only applicable with 'allowJs'. */
47
47
 
package/.eslintrc DELETED
@@ -1,25 +0,0 @@
1
- {
2
- "env": {
3
- "node": true,
4
- "mocha": true,
5
- "es6": true
6
- },
7
- "parser": "@typescript-eslint/parser",
8
- "parserOptions": {
9
- "ecmaFeatures": {
10
- "impliedStrict": true
11
- }
12
- },
13
- "globals": {
14
- "expect": true
15
- },
16
- "rules": {
17
- "security/detect-object-injection": 0,
18
- "security/detect-non-literal-regexp": 0,
19
- "no-unused-expressions": 0,
20
- "func-style": 0
21
- },
22
- "extends": [
23
- "emarsys"
24
- ]
25
- }