@unchainedshop/logger 1.1.3 → 1.2.0
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.
- package/jest.config.js +5 -0
- package/package.json +13 -17
- package/src/createLogger.ts +60 -0
- package/src/log.ts +10 -0
- package/src/logger-index.ts +5 -0
- package/src/logger.types.ts +14 -0
- package/tests/logger-index.test.ts +23 -0
- package/{tsconfig.build.json → tsconfig.json} +11 -5
- package/babel.config.js +0 -3
- package/package.js +0 -21
package/jest.config.js
ADDED
package/package.json
CHANGED
|
@@ -1,14 +1,18 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@unchainedshop/logger",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.2.0",
|
|
4
4
|
"main": "lib/logger-index.js",
|
|
5
|
+
"exports": {
|
|
6
|
+
".": "./lib/logger-index.js",
|
|
7
|
+
"./*": "./lib/*"
|
|
8
|
+
},
|
|
5
9
|
"types": "lib/logger-index.d.ts",
|
|
6
10
|
"type": "module",
|
|
7
11
|
"scripts": {
|
|
12
|
+
"prepublishOnly": "npm install && npm run build || :",
|
|
8
13
|
"clean": "rm -rf lib",
|
|
9
|
-
"build": "npm run clean && tsc
|
|
10
|
-
"watch": "tsc
|
|
11
|
-
"link:core": "npm link @unchainedshop/types",
|
|
14
|
+
"build": "npm run clean && tsc",
|
|
15
|
+
"watch": "tsc -w",
|
|
12
16
|
"test": "jest --watch"
|
|
13
17
|
},
|
|
14
18
|
"repository": {
|
|
@@ -28,24 +32,16 @@
|
|
|
28
32
|
"homepage": "https://github.com/unchainedshop/unchained#readme",
|
|
29
33
|
"dependencies": {
|
|
30
34
|
"safe-stable-stringify": "^2.3.1",
|
|
31
|
-
"winston": "^3.
|
|
35
|
+
"winston": "^3.8.0",
|
|
32
36
|
"winston-transport": "^4.5.0"
|
|
33
37
|
},
|
|
34
38
|
"devDependencies": {
|
|
35
|
-
"@
|
|
36
|
-
"@
|
|
37
|
-
"@babel/preset-typescript": "^7.17.12",
|
|
38
|
-
"@types/chai": "^4.3.1",
|
|
39
|
-
"@types/jest": "^28.1.1",
|
|
40
|
-
"@types/mocha": "^9.1.1",
|
|
39
|
+
"@types/jest": "^28.1.3",
|
|
40
|
+
"@types/node": "^16.11.44",
|
|
41
41
|
"@types/winston": "^2.4.4",
|
|
42
|
-
"@unchainedshop/types": "
|
|
43
|
-
"babel-jest": "^28.1.1",
|
|
44
|
-
"chai": "^4.3.6",
|
|
45
|
-
"cross-env": "^7.0.3",
|
|
42
|
+
"@unchainedshop/types": "^1.1.9",
|
|
46
43
|
"jest": "^28.1.1",
|
|
47
44
|
"ts-jest": "^28.0.5",
|
|
48
|
-
"
|
|
49
|
-
"typescript": "^4.7.3"
|
|
45
|
+
"typescript": "^4.7.4"
|
|
50
46
|
}
|
|
51
47
|
}
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import { createLogger as createWinstonLogger, format, transports } from 'winston';
|
|
2
|
+
import stringify from 'safe-stable-stringify';
|
|
3
|
+
import TransportStream from 'winston-transport';
|
|
4
|
+
import { LogLevel } from './logger.types';
|
|
5
|
+
|
|
6
|
+
const { DEBUG = '', LOG_LEVEL = LogLevel.Info, UNCHAINED_LOG_FORMAT = 'unchained' } = process.env;
|
|
7
|
+
|
|
8
|
+
const { combine, label, timestamp, colorize, printf, json } = format;
|
|
9
|
+
|
|
10
|
+
const debugStringContainsModule = (debugString: string, moduleName: string) => {
|
|
11
|
+
if (!debugString) return false;
|
|
12
|
+
const loggingMatched = debugString.split(',').reduce((accumulator: any, name: string) => {
|
|
13
|
+
if (accumulator === false) return accumulator;
|
|
14
|
+
const nameRegex = name.replace(/-/i, '\\-?').replace(/:\*/i, '\\:?*').replace(/\*/i, '.*');
|
|
15
|
+
const regExp = new RegExp(`^${nameRegex}$`, 'm');
|
|
16
|
+
if (regExp.test(moduleName)) {
|
|
17
|
+
if (name.slice(0, 1) === '-') {
|
|
18
|
+
// explicitly disable
|
|
19
|
+
return false;
|
|
20
|
+
}
|
|
21
|
+
return true;
|
|
22
|
+
}
|
|
23
|
+
return accumulator;
|
|
24
|
+
}, undefined);
|
|
25
|
+
return loggingMatched || false;
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
const myFormat = printf(({ level, message, label: _label, timestamp: _timestamp, ...rest }) => { //eslint-disable-line
|
|
29
|
+
const otherPropsString = stringify(rest);
|
|
30
|
+
return `[${_label}] ${level}: ${message} ${otherPropsString}`;
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
const UnchainedLogFormats = {
|
|
34
|
+
unchained: (moduleName: string) =>
|
|
35
|
+
combine(timestamp(), label({ label: moduleName }), colorize(), myFormat),
|
|
36
|
+
json,
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
if (!UnchainedLogFormats[UNCHAINED_LOG_FORMAT.toLowerCase()]) {
|
|
40
|
+
throw new Error(
|
|
41
|
+
`UNCHAINED_LOG_FORMAT is invalid, use one of ${Object.keys(UnchainedLogFormats).join(',')}`,
|
|
42
|
+
);
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
export { transports, format };
|
|
46
|
+
|
|
47
|
+
export const createLogger = (moduleName: string, moreTransports: Array<TransportStream> = []) => {
|
|
48
|
+
const loggingMatched = debugStringContainsModule(DEBUG, moduleName);
|
|
49
|
+
return createWinstonLogger({
|
|
50
|
+
transports: [
|
|
51
|
+
new transports.Console({
|
|
52
|
+
format: UnchainedLogFormats[UNCHAINED_LOG_FORMAT](moduleName),
|
|
53
|
+
stderrLevels: [LogLevel.Error],
|
|
54
|
+
consoleWarnLevels: [LogLevel.Warning],
|
|
55
|
+
level: loggingMatched ? LogLevel.Debug : LOG_LEVEL,
|
|
56
|
+
}),
|
|
57
|
+
...moreTransports,
|
|
58
|
+
],
|
|
59
|
+
});
|
|
60
|
+
};
|
package/src/log.ts
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import winston from 'winston';
|
|
2
|
+
import { LogLevel, LogOptions } from './logger.types';
|
|
3
|
+
import { createLogger } from './createLogger';
|
|
4
|
+
|
|
5
|
+
const logger = createLogger('unchained');
|
|
6
|
+
|
|
7
|
+
export const log = (message: string, options?: LogOptions): winston.Logger => {
|
|
8
|
+
const { level = LogLevel.Info, ...meta } = options || {};
|
|
9
|
+
return logger.log(level, message, meta);
|
|
10
|
+
};
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { LoggerOptions } from 'winston';
|
|
2
|
+
|
|
3
|
+
export enum LogLevel {
|
|
4
|
+
Verbose = 'verbose',
|
|
5
|
+
Info = 'info',
|
|
6
|
+
Debug = 'debug',
|
|
7
|
+
Error = 'error',
|
|
8
|
+
Warning = 'warn',
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export interface LogOptions extends LoggerOptions {
|
|
12
|
+
level: LogLevel;
|
|
13
|
+
[x: string]: any;
|
|
14
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { assert } from 'chai';
|
|
2
|
+
|
|
3
|
+
import {
|
|
4
|
+
log,
|
|
5
|
+
createLogger,
|
|
6
|
+
transports,
|
|
7
|
+
format,
|
|
8
|
+
LogLevel,
|
|
9
|
+
} from '../src/logger-index';
|
|
10
|
+
|
|
11
|
+
describe('Test exports', () => {
|
|
12
|
+
it('log', () => {
|
|
13
|
+
assert.isFunction(log);
|
|
14
|
+
log('Test', { level: LogLevel.Warning });
|
|
15
|
+
});
|
|
16
|
+
it('createLogger', () => {
|
|
17
|
+
assert.isFunction(createLogger);
|
|
18
|
+
assert.isFunction(format);
|
|
19
|
+
assert.isObject(transports);
|
|
20
|
+
const logger = createLogger('unchained:test');
|
|
21
|
+
logger.info('Test Logger', 'With additional info');
|
|
22
|
+
});
|
|
23
|
+
});
|
|
@@ -6,7 +6,9 @@
|
|
|
6
6
|
"esModuleInterop": true,
|
|
7
7
|
"experimentalDecorators": true,
|
|
8
8
|
"forceConsistentCasingInFileNames": true,
|
|
9
|
-
"lib": [
|
|
9
|
+
"lib": [
|
|
10
|
+
"esnext"
|
|
11
|
+
],
|
|
10
12
|
"module": "esnext",
|
|
11
13
|
"moduleResolution": "node",
|
|
12
14
|
"noImplicitReturns": true,
|
|
@@ -16,8 +18,12 @@
|
|
|
16
18
|
"skipLibCheck": true,
|
|
17
19
|
"sourceMap": true,
|
|
18
20
|
"target": "esnext",
|
|
19
|
-
"types": [
|
|
21
|
+
"types": [
|
|
22
|
+
"node",
|
|
23
|
+
"jest"
|
|
24
|
+
]
|
|
20
25
|
},
|
|
21
|
-
"include": [
|
|
22
|
-
|
|
23
|
-
|
|
26
|
+
"include": [
|
|
27
|
+
"src"
|
|
28
|
+
]
|
|
29
|
+
}
|
package/babel.config.js
DELETED
package/package.js
DELETED
|
@@ -1,21 +0,0 @@
|
|
|
1
|
-
Package.describe({
|
|
2
|
-
name: 'unchained:logger',
|
|
3
|
-
version: '1.1.3',
|
|
4
|
-
summary: 'Unchained Engine: Logger',
|
|
5
|
-
git: 'https://github.com/unchainedshop/unchained',
|
|
6
|
-
documentation: 'README.md',
|
|
7
|
-
});
|
|
8
|
-
|
|
9
|
-
Npm.depends({
|
|
10
|
-
'safe-stable-stringify': '2.3.1',
|
|
11
|
-
winston: '3.7.2',
|
|
12
|
-
'winston-transport': '4.5.0',
|
|
13
|
-
});
|
|
14
|
-
|
|
15
|
-
Package.onUse((api) => {
|
|
16
|
-
api.versionsFrom('2.7.3');
|
|
17
|
-
api.use('ecmascript');
|
|
18
|
-
api.use('typescript');
|
|
19
|
-
|
|
20
|
-
api.mainModule('src/logger-index.ts', 'server');
|
|
21
|
-
});
|