Skip to content

Commit 46a602a

Browse files
authored
Introduce multi-logging (#60)
1 parent b945168 commit 46a602a

14 files changed

+288
-240
lines changed

.eslintrc.json

+3-2
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,14 @@
44
"ecmaVersion": 2021
55
},
66
"extends": [
7-
"plugin:radar/recommended",
7+
"plugin:sonarjs/recommended",
88
"eslint:recommended",
99
"eslint:recommended"
1010
],
1111
"ignorePatterns": ["!**/*"],
1212
"plugins": [
1313
"simple-import-sort",
14-
"radar",
14+
"sonarjs",
1515
"unicorn",
1616
"jest"
1717
],
@@ -28,6 +28,7 @@
2828
"simple-import-sort/imports": "error",
2929
"simple-import-sort/exports": "error",
3030
"no-unused-vars": "off",
31+
"sonarjs/cognitive-complexity": "off",
3132
"padding-line-between-statements": [
3233
"error",
3334
{

package.json

+43-43
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,45 @@
11
{
2-
"name": "@lvksh/logger",
3-
"description": "Zero dependency, light-weight, blazing fast customizable logging library.",
4-
"repository": "https://github.com/lvkdotsh/logger",
5-
"author": "lvksh",
6-
"license": "LGPL-3.0-or-later",
7-
"types": "./lib/index.d.ts",
8-
"main": "./lib/index.js",
9-
"files": [
10-
"lib"
11-
],
12-
"keywords": [
13-
"logger",
14-
"logging",
15-
"chalk",
16-
"color",
17-
"padleft"
18-
],
19-
"scripts": {
20-
"build": "yarn ts && yarn ts:defs",
21-
"ts": "tsc",
22-
"ts:defs": "tsc --declaration --outDir lib --emitDeclarationOnly",
23-
"debug": "yarn build && node ./test/index.js",
24-
"pub": "yarn build && yarn publish --access public",
25-
"test": "jest --verbose --coverage",
26-
"lint": "eslint -c .eslintrc.json --ext .ts ./src"
27-
},
28-
"devDependencies": {
29-
"@types/jest": "^27.0.3",
30-
"@types/node": "^16.11.11",
31-
"@typescript-eslint/parser": "^5.2.0",
32-
"eslint": "^8.4.0",
33-
"eslint-plugin-jest": "^25.2.2",
34-
"eslint-plugin-radar": "^0.2.1",
35-
"eslint-plugin-simple-import-sort": "^7.0.0",
36-
"eslint-plugin-unicorn": "^37.0.1",
37-
"chalk": "4.0.0",
38-
"jest": "^27.4.4",
39-
"ts-jest": "^27.1.1",
40-
"ts-node": "^10.4.0",
41-
"typescript": "^4.5.2"
42-
},
43-
"dependencies": {},
44-
"version": "1.0.12"
2+
"name": "@lvksh/logger",
3+
"description": "Zero dependency, light-weight, blazing fast customizable logging library.",
4+
"repository": "https://github.com/lvkdotsh/logger",
5+
"author": "lvksh",
6+
"license": "LGPL-3.0-or-later",
7+
"types": "./lib/index.d.ts",
8+
"main": "./lib/index.js",
9+
"files": [
10+
"lib"
11+
],
12+
"keywords": [
13+
"logger",
14+
"logging",
15+
"chalk",
16+
"color",
17+
"padleft"
18+
],
19+
"scripts": {
20+
"build": "yarn ts && yarn ts:defs",
21+
"ts": "tsc",
22+
"ts:defs": "tsc --declaration --outDir lib --emitDeclarationOnly",
23+
"debug": "yarn build && node ./test/index.js",
24+
"pub": "yarn build && yarn publish --access public",
25+
"test": "jest --verbose --coverage",
26+
"lint": "eslint -c .eslintrc.json --ext .ts ./src"
27+
},
28+
"devDependencies": {
29+
"@types/jest": "^27.0.3",
30+
"@types/node": "^16.11.11",
31+
"@typescript-eslint/parser": "^5.2.0",
32+
"chalk": "4.0.0",
33+
"eslint": "^8.4.0",
34+
"eslint-plugin-jest": "^25.2.2",
35+
"eslint-plugin-sonarjs": "^0.11.0",
36+
"eslint-plugin-simple-import-sort": "^7.0.0",
37+
"eslint-plugin-unicorn": "^37.0.1",
38+
"jest": "^27.4.4",
39+
"ts-jest": "^27.1.1",
40+
"ts-node": "^10.4.0",
41+
"typescript": "^4.5.2"
42+
},
43+
"dependencies": {},
44+
"version": "1.0.12"
4545
}

src/index.ts

+39-36
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,8 @@ const pad = (
9898
if (paddingStrategy === 'PREPEND') return calculatedPadding + text;
9999
};
100100

101+
type GenericLogFunction = (input: string) => void;
102+
101103
/**
102104
* @name createLogger
103105
* Creates a logger with the specified methods and config
@@ -109,8 +111,10 @@ const pad = (
109111
export const createLogger = <A extends string>(
110112
methods: { [k in A]: string | MethodConfig },
111113
config: Partial<LogConfig> = {},
112-
func: (payload: string) => void = console.log
114+
func: GenericLogFunction | GenericLogFunction[] = console.log
113115
) => {
116+
let functions: GenericLogFunction[] = Array.isArray(func) ? func : [func];
117+
114118
// Fill default values incase not overriden by arg
115119
const completeConfig: LogConfig = {
116120
...{
@@ -199,40 +203,39 @@ export const createLogger = <A extends string>(
199203

200204
return {
201205
[methodHandle]: (...s: LogMethodInput[]) => {
202-
func(
203-
s
204-
.map((value) => {
205-
if (typeof value !== 'string') {
206-
value = inspect(
207-
value,
208-
false,
209-
3,
210-
completeConfig.color
211-
);
212-
}
213-
214-
return value;
215-
})
216-
.join('\n')
217-
.split('\n')
218-
.map(
219-
(value, index, array) =>
220-
(index == 0
221-
? (typeof method.label === 'string'
222-
? paddedText
223-
: pad(
224-
method.label.calculate(),
225-
maxLength,
226-
completeConfig.padding,
227-
method.paddingChar
228-
)) + method.divider
229-
: (array.length - 1 == index
230-
? newLineEndPadding
231-
: newLinePadding) +
232-
method.divider) + value
233-
)
234-
.join('\n')
235-
);
206+
const value = s
207+
.map((value) => {
208+
if (typeof value !== 'string') {
209+
value = inspect(
210+
value,
211+
false,
212+
3,
213+
completeConfig.color
214+
);
215+
}
216+
217+
return value;
218+
})
219+
.join('\n')
220+
.split('\n')
221+
.map(
222+
(value, index, array) =>
223+
(index == 0
224+
? (typeof method.label === 'string'
225+
? paddedText
226+
: pad(
227+
method.label.calculate(),
228+
maxLength,
229+
completeConfig.padding,
230+
method.paddingChar
231+
)) + method.divider
232+
: (array.length - 1 == index
233+
? newLineEndPadding
234+
: newLinePadding) + method.divider) +
235+
value
236+
)
237+
.join('\n');
238+
functions.forEach((a) => a(value));
236239
},
237240
};
238241
})
@@ -243,4 +246,4 @@ export const shimLog = <A extends string>(logger: Logger<A>, func: A) => {
243246
Object.defineProperty(console, 'log', {
244247
value: logger[func],
245248
});
246-
};
249+
};

tests/appendprepend.test.ts

+47-50
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,46 @@
1-
import { createLogger, Logger } from "../src";
1+
import { createLogger, Logger } from '../src';
22
const logFn = jest.fn();
33

4-
5-
describe("Prepend vs Append vs None", () => {
6-
let loggerPrepend: Logger<"short" | "longer" | "custom_short">;
7-
let loggerAppend: Logger<"short" | "longer" | "custom_short">;
8-
let loggerNone: Logger<"short" | "longer" | "custom_short">;
4+
describe('Prepend vs Append vs None', () => {
5+
let loggerPrepend: Logger<'short' | 'longer' | 'custom_short'>;
6+
let loggerAppend: Logger<'short' | 'longer' | 'custom_short'>;
7+
let loggerNone: Logger<'short' | 'longer' | 'custom_short'>;
98

109
beforeAll(() => {
1110
loggerPrepend = createLogger(
1211
{
13-
short: "SHORT",
14-
longer: "LONGER",
12+
short: 'SHORT',
13+
longer: 'LONGER',
1514
custom_short: {
16-
label: "SHORT",
17-
paddingChar: "-",
15+
label: 'SHORT',
16+
paddingChar: '-',
1817
},
1918
},
20-
{ padding: "PREPEND", color: false },
19+
{ padding: 'PREPEND', color: false },
2120
logFn
2221
);
2322
loggerAppend = createLogger(
2423
{
25-
short: "SHORT",
26-
longer: "LONGER",
24+
short: 'SHORT',
25+
longer: 'LONGER',
2726
custom_short: {
28-
label: "SHORT",
29-
paddingChar: "-",
27+
label: 'SHORT',
28+
paddingChar: '-',
3029
},
3130
},
32-
{ padding: "APPEND", color: false },
31+
{ padding: 'APPEND', color: false },
3332
logFn
3433
);
3534
loggerNone = createLogger(
3635
{
37-
short: "SHORT",
38-
longer: "LONGER",
36+
short: 'SHORT',
37+
longer: 'LONGER',
3938
custom_short: {
40-
label: "SHORT",
41-
paddingChar: "-",
39+
label: 'SHORT',
40+
paddingChar: '-',
4241
},
4342
},
44-
{ padding: "NONE", color: false },
43+
{ padding: 'NONE', color: false },
4544
logFn
4645
);
4746
});
@@ -50,42 +49,40 @@ describe("Prepend vs Append vs None", () => {
5049
jest.clearAllMocks();
5150
});
5251

53-
it("should prepend short", () => {
54-
loggerPrepend.short("Hello world");
55-
expect(logFn).toBeCalledWith(" SHORT Hello world");
52+
it('should prepend short', () => {
53+
loggerPrepend.short('Hello world');
54+
expect(logFn).toBeCalledWith(' SHORT Hello world');
5655
});
57-
it("should append short", () => {
58-
loggerAppend.short("Hello world");
59-
expect(logFn).toBeCalledWith("SHORT Hello world");
56+
it('should append short', () => {
57+
loggerAppend.short('Hello world');
58+
expect(logFn).toBeCalledWith('SHORT Hello world');
6059
});
61-
it("should none short", () => {
62-
loggerNone.short("Hello world");
63-
expect(logFn).toBeCalledWith("SHORT Hello world");
60+
it('should none short', () => {
61+
loggerNone.short('Hello world');
62+
expect(logFn).toBeCalledWith('SHORT Hello world');
6463
});
65-
it("should prepend short", () => {
66-
loggerPrepend.short("Hello world");
67-
expect(logFn).toBeCalledWith(" SHORT Hello world");
64+
it('should prepend short', () => {
65+
loggerPrepend.short('Hello world');
66+
expect(logFn).toBeCalledWith(' SHORT Hello world');
6867
});
69-
it("should append short", () => {
70-
loggerAppend.short("Hello world");
71-
expect(logFn).toBeCalledWith("SHORT Hello world");
68+
it('should append short', () => {
69+
loggerAppend.short('Hello world');
70+
expect(logFn).toBeCalledWith('SHORT Hello world');
7271
});
73-
it("should none short", () => {
74-
loggerNone.short("Hello world");
75-
expect(logFn).toBeCalledWith("SHORT Hello world");
72+
it('should none short', () => {
73+
loggerNone.short('Hello world');
74+
expect(logFn).toBeCalledWith('SHORT Hello world');
7675
});
77-
it("should prepend custom padding short", () => {
78-
loggerPrepend.custom_short("Hello world");
79-
expect(logFn).toBeCalledWith("-SHORT Hello world");
76+
it('should prepend custom padding short', () => {
77+
loggerPrepend.custom_short('Hello world');
78+
expect(logFn).toBeCalledWith('-SHORT Hello world');
8079
});
81-
it("should append custom padding short", () => {
82-
loggerAppend.custom_short("Hello world");
83-
expect(logFn).toBeCalledWith("SHORT- Hello world");
80+
it('should append custom padding short', () => {
81+
loggerAppend.custom_short('Hello world');
82+
expect(logFn).toBeCalledWith('SHORT- Hello world');
8483
});
85-
it("should none custom padding short", () => {
86-
loggerNone.custom_short("Hello world");
87-
expect(logFn).toBeCalledWith("SHORT Hello world");
84+
it('should none custom padding short', () => {
85+
loggerNone.custom_short('Hello world');
86+
expect(logFn).toBeCalledWith('SHORT Hello world');
8887
});
8988
});
90-
91-

tests/defaultvalues.test.ts

+8-9
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,22 @@
1-
import { createLogger, Logger } from "../src";
1+
import { createLogger, Logger } from '../src';
22

3-
describe("Default values", () => {
3+
describe('Default values', () => {
44
let consoleLog: jest.SpyInstance;
5-
let logger: Logger<"default">;
5+
let logger: Logger<'default'>;
66

77
beforeAll(() => {
8-
consoleLog = jest.spyOn(console, "log").mockImplementation(() => { });
8+
consoleLog = jest.spyOn(console, 'log').mockImplementation(() => { });
99
logger = createLogger({
10-
default: "OK",
10+
default: 'OK',
1111
});
1212
});
1313

1414
afterEach(() => {
1515
jest.clearAllMocks();
1616
});
1717

18-
it("should log", () => {
19-
logger.default("Hello world");
20-
expect(consoleLog).toBeCalledWith("OK Hello world");
18+
it('should log', () => {
19+
logger.default('Hello world');
20+
expect(consoleLog).toBeCalledWith('OK Hello world');
2121
});
2222
});
23-

0 commit comments

Comments
 (0)