json-log-line 1.0.0 → 1.0.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,19 @@
1
+ export type Options = {
2
+ exclude?: string | string[];
3
+ include?: string | string[];
4
+ format?: Record<string, (value: any, parsedLogObject?: any, ...arguments_: any[]) => string>;
5
+ };
6
+ export declare function logLineFactory({
7
+ /**
8
+ * include and exclude both take keys with dot notation
9
+ */
10
+ exclude,
11
+ /**
12
+ * include always overrides exclude
13
+ */
14
+ include,
15
+ /**
16
+ * Format functions for any given key, keys of the object are automatically included and cannot be excluded
17
+ */
18
+ format, }?: Options): (inputData: string | Record<string, unknown>) => string;
19
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAQA,MAAM,MAAM,OAAO,GAAG;IACpB,OAAO,CAAC,EAAE,MAAM,GAAG,MAAM,EAAE,CAAC;IAC5B,OAAO,CAAC,EAAE,MAAM,GAAG,MAAM,EAAE,CAAC;IAC5B,MAAM,CAAC,EAAE,MAAM,CACb,MAAM,EACN,CAAC,KAAK,EAAE,GAAG,EAAE,eAAe,CAAC,EAAE,GAAG,EAAE,GAAG,UAAU,EAAE,GAAG,EAAE,KAAK,MAAM,CACpE,CAAC;CACH,CAAC;AAMF,wBAAgB,cAAc,CAAC;AAC7B;;GAEG;AACH,OAAY;AACZ;;GAEG;AACH,OAAY;AACZ;;GAEG;AACH,MAAW,GACZ,GAAE,OAAY,IASI,WAAW,MAAM,GAAG,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,KAAG,MAAM,CA0FtE"}
package/dist/index.js ADDED
@@ -0,0 +1,103 @@
1
+ import jsonParse from 'fast-json-parse';
2
+ import unset from 'unset-value';
3
+ import get from 'get-value';
4
+ import set from 'set-value';
5
+ import deepMerge from '@fastify/deepmerge';
6
+ import isObject from "./utils/is-object.js";
7
+ import isEmpty from "./utils/is-empty.js";
8
+ const nl = '\n';
9
+ export function logLineFactory({
10
+ /**
11
+ * include and exclude both take keys with dot notation
12
+ */
13
+ exclude = [],
14
+ /**
15
+ * include always overrides exclude
16
+ */
17
+ include = [],
18
+ /**
19
+ * Format functions for any given key, keys of the object are automatically included and cannot be excluded
20
+ */
21
+ format = {}, } = {}) {
22
+ const logLineKeys = Object.keys(format);
23
+ const splitLogLineKeys = logLineKeys.flatMap((key) => key.split('|'));
24
+ format.extraFields ||= (object) => JSON.stringify(object) + nl;
25
+ /**
26
+ * @param inputData - The input data to be formatted can be a JSON stringified object or a plain object
27
+ */
28
+ return function (inputData) {
29
+ try {
30
+ let object = {};
31
+ if (typeof inputData === 'string') {
32
+ const parsedData = jsonParse(inputData);
33
+ if (!parsedData.value || parsedData.err) {
34
+ return inputData + nl;
35
+ }
36
+ object = parsedData.value;
37
+ }
38
+ else if (isObject(inputData)) {
39
+ object = inputData;
40
+ }
41
+ else {
42
+ return nl;
43
+ }
44
+ // cache the whitelist
45
+ const whiteListObject = {};
46
+ for (const key of [...splitLogLineKeys, ...include]) {
47
+ const value = get(object, key);
48
+ if (value) {
49
+ set(whiteListObject, key, value);
50
+ }
51
+ }
52
+ // remove the blacklist
53
+ for (const key of exclude) {
54
+ unset(object, key);
55
+ }
56
+ // add back in the whitelist
57
+ object = deepMerge()(object, whiteListObject);
58
+ const output = [];
59
+ for (const _key of logLineKeys) {
60
+ const keys = _key.split('|');
61
+ for (const key of keys) {
62
+ // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
63
+ const value = get(object, key);
64
+ if (!value) {
65
+ continue;
66
+ }
67
+ const formatter = format[_key];
68
+ if (formatter) {
69
+ output.push(formatter(value, object));
70
+ }
71
+ }
72
+ }
73
+ // remove the properties that were used to create the log-line
74
+ for (const key of splitLogLineKeys) {
75
+ unset(object, key);
76
+ }
77
+ // remove empty blacklist that may have had whitelisted properties
78
+ for (const key of exclude) {
79
+ if (isEmpty(get(object, key))) {
80
+ unset(object, key);
81
+ }
82
+ }
83
+ let outputString = output.filter(Boolean).join(' ');
84
+ // after processing the rest of the object contains
85
+ // extra fields that were not in the logLine nor in the log line nor blacklisted
86
+ // so these are the ones we want to prettify and highlight
87
+ if (isObject(object) && !isEmpty(object) && format.extraFields) {
88
+ outputString = outputString.concat(format.extraFields(object)); // eslint-disable-line unicorn/prefer-spread
89
+ }
90
+ if (!outputString.endsWith(nl)) {
91
+ outputString += nl;
92
+ }
93
+ if (outputString === nl) {
94
+ return JSON.stringify(object) + nl;
95
+ }
96
+ return outputString;
97
+ }
98
+ catch (error) {
99
+ console.log(error);
100
+ return '';
101
+ }
102
+ };
103
+ }
@@ -0,0 +1,3 @@
1
+ declare function isEmpty(object: unknown): object is Record<string, never>;
2
+ export default isEmpty;
3
+ //# sourceMappingURL=is-empty.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"is-empty.d.ts","sourceRoot":"","sources":["../../src/utils/is-empty.ts"],"names":[],"mappings":"AAEA,iBAAS,OAAO,CAAC,MAAM,EAAE,OAAO,GAAG,MAAM,IAAI,MAAM,CAAC,MAAM,EAAE,KAAK,CAAC,CAOjE;AAED,eAAe,OAAO,CAAC"}
@@ -0,0 +1,8 @@
1
+ import isObject from "./is-object.js";
2
+ function isEmpty(object) {
3
+ return Boolean(isObject(object) &&
4
+ (object === undefined ||
5
+ object === null ||
6
+ Object.keys(object).length === 0));
7
+ }
8
+ export default isEmpty;
@@ -0,0 +1,3 @@
1
+ declare function isObject(input: unknown): input is object;
2
+ export default isObject;
3
+ //# sourceMappingURL=is-object.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"is-object.d.ts","sourceRoot":"","sources":["../../src/utils/is-object.ts"],"names":[],"mappings":"AACA,iBAAS,QAAQ,CAAC,KAAK,EAAE,OAAO,GAAG,KAAK,IAAI,MAAM,CAIjD;AAED,eAAe,QAAQ,CAAC"}
@@ -0,0 +1,5 @@
1
+ // eslint-disable-next-line @typescript-eslint/no-restricted-types
2
+ function isObject(input) {
3
+ return Boolean(input && Object.prototype.toString.apply(input) === '[object Object]');
4
+ }
5
+ export default isObject;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "json-log-line",
3
- "version": "1.0.0",
3
+ "version": "1.0.1",
4
4
  "description": "A utility for building awesome log lines from JSON",
5
5
  "homepage": "https://github.com/spence-s/json-log-line",
6
6
  "bugs": {
@@ -36,6 +36,7 @@
36
36
  "clean": "rimraf dist",
37
37
  "lint": "npm run check && xo",
38
38
  "prepare": "husky",
39
+ "prepublish": "npm run build",
39
40
  "release": "np",
40
41
  "test": "c8 ava",
41
42
  "update": "ncu -i"