@unchainedshop/logger 1.1.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/README.md ADDED
@@ -0,0 +1,5 @@
1
+ # Logger (Unchained Engine)
2
+
3
+ This package handles logging for all modules.
4
+
5
+ It exports two actions _log_ and _createLogger_ to log information based on the initial logger configuration.
@@ -0,0 +1,3 @@
1
+ export default {
2
+ presets: [['@babel/preset-env', { targets: { node: 'current' } }], '@babel/preset-typescript'],
3
+ };
@@ -0,0 +1,4 @@
1
+ import { format, transports } from 'winston';
2
+ import TransportStream from 'winston-transport';
3
+ export { transports, format };
4
+ export declare const createLogger: (moduleName: string, moreTransports?: Array<TransportStream>) => import("winston").Logger;
@@ -0,0 +1,51 @@
1
+ import { createLogger as createWinstonLogger, format, transports } from 'winston';
2
+ import stringify from 'safe-stable-stringify';
3
+ import { LogLevel } from './logger.types';
4
+ const { DEBUG = '', LOG_LEVEL = LogLevel.Info, UNCHAINED_LOG_FORMAT = 'unchained' } = process.env;
5
+ const { combine, label, timestamp, colorize, printf, json } = format;
6
+ const debugStringContainsModule = (debugString, moduleName) => {
7
+ if (!debugString)
8
+ return false;
9
+ const loggingMatched = debugString.split(',').reduce((accumulator, name) => {
10
+ if (accumulator === false)
11
+ return accumulator;
12
+ const nameRegex = name.replace(/-/i, '\\-?').replace(/:\*/i, '\\:?*').replace(/\*/i, '.*');
13
+ const regExp = new RegExp(`^${nameRegex}$`, 'm');
14
+ if (regExp.test(moduleName)) {
15
+ if (name.slice(0, 1) === '-') {
16
+ // explicitly disable
17
+ return false;
18
+ }
19
+ return true;
20
+ }
21
+ return accumulator;
22
+ }, undefined);
23
+ return loggingMatched || false;
24
+ };
25
+ const myFormat = printf(({ level, message, label: _label, timestamp: _timestamp, ...rest }) => {
26
+ const otherPropsString = stringify(rest);
27
+ return `[${_label}] ${level}: ${message} ${otherPropsString}`;
28
+ });
29
+ const UnchainedLogFormats = {
30
+ unchained: (moduleName) => combine(timestamp(), label({ label: moduleName }), colorize(), myFormat),
31
+ json,
32
+ };
33
+ if (!UnchainedLogFormats[UNCHAINED_LOG_FORMAT.toLowerCase()]) {
34
+ throw new Error(`UNCHAINED_LOG_FORMAT is invalid, use one of ${Object.keys(UnchainedLogFormats).join(',')}`);
35
+ }
36
+ export { transports, format };
37
+ export const createLogger = (moduleName, moreTransports = []) => {
38
+ const loggingMatched = debugStringContainsModule(DEBUG, moduleName);
39
+ return createWinstonLogger({
40
+ transports: [
41
+ new transports.Console({
42
+ format: UnchainedLogFormats[UNCHAINED_LOG_FORMAT](moduleName),
43
+ stderrLevels: [LogLevel.Error],
44
+ consoleWarnLevels: [LogLevel.Warning],
45
+ level: loggingMatched ? LogLevel.Debug : LOG_LEVEL,
46
+ }),
47
+ ...moreTransports,
48
+ ],
49
+ });
50
+ };
51
+ //# sourceMappingURL=createLogger.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"createLogger.js","sourceRoot":"","sources":["../src/createLogger.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,IAAI,mBAAmB,EAAE,MAAM,EAAE,UAAU,EAAE,MAAM,SAAS,CAAC;AAClF,OAAO,SAAS,MAAM,uBAAuB,CAAC;AAE9C,OAAO,EAAE,QAAQ,EAAE,MAAM,gBAAgB,CAAC;AAE1C,MAAM,EAAE,KAAK,GAAG,EAAE,EAAE,SAAS,GAAG,QAAQ,CAAC,IAAI,EAAE,oBAAoB,GAAG,WAAW,EAAE,GAAG,OAAO,CAAC,GAAG,CAAC;AAElG,MAAM,EAAE,OAAO,EAAE,KAAK,EAAE,SAAS,EAAE,QAAQ,EAAE,MAAM,EAAE,IAAI,EAAE,GAAG,MAAM,CAAC;AAErE,MAAM,yBAAyB,GAAG,CAAC,WAAmB,EAAE,UAAkB,EAAE,EAAE;IAC5E,IAAI,CAAC,WAAW;QAAE,OAAO,KAAK,CAAC;IAC/B,MAAM,cAAc,GAAG,WAAW,CAAC,KAAK,CAAC,GAAG,CAA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,CAAC,oBAAoB,CAAC,WAAW,EAAE,CAAC,EAAE;IAC5D,MAAM,IAAI,KAAK,CACb,+CAA+C,MAAM,CAAC,IAAI,CAAC,mBAAmB,CAAC,CAAC,IAAI,CAAC,GAAG,CAAC,EAAE,CAC5F,CAAC;CACH;AAED,OAAO,EAAE,UAAU,EAAE,MAAM,EAAE,CAAC;AAE9B,MAAM,CAAC,MAAM,YAAY,GAAG,CAAC,UAAkB,EAAE,iBAAyC,EAAE,EAAE,EAAE;IAC9F,MAAM,cAAc,GAAG,yBAAyB,CAAC,KAAK,EAAE,UAAU,CAAC,CAAC;IACpE,OAAO,mBAAmB,CAAC;QACzB,UAAU,EAAE;YACV,IAAI,UAAU,CAAC,OAAO,CAAC;gBACrB,MAAM,EAAE,mBAAmB,CAAC,oBAAoB,CAAC,CAAC,UAAU,CAAC;gBAC7D,YAAY,EAAE,CAAC,QAAQ,CAAC,KAAK,CAAC;gBAC9B,iBAAiB,EAAE,CAAC,QAAQ,CAAC,OAAO,CAAC;gBACrC,KAAK,EAAE,cAAc,CAAC,CAAC,CAAC,QAAQ,CAAC,KAAK,CAAC,CAAC,CAAC,SAAS;aACnD,CAAC;YACF,GAAG,cAAc;SAClB;KACF,CAAC,CAAC;AACL,CAAC,CAAC"}
package/lib/log.d.ts ADDED
@@ -0,0 +1,3 @@
1
+ import winston from 'winston';
2
+ import { LogOptions } from './logger.types';
3
+ export declare const log: (message: string, options?: LogOptions) => winston.Logger;
package/lib/log.js ADDED
@@ -0,0 +1,8 @@
1
+ import { LogLevel } from './logger.types';
2
+ import { createLogger } from './createLogger';
3
+ const logger = createLogger('unchained');
4
+ export const log = (message, options) => {
5
+ const { level = LogLevel.Info, ...meta } = options || {};
6
+ return logger.log(level, message, meta);
7
+ };
8
+ //# sourceMappingURL=log.js.map
package/lib/log.js.map ADDED
@@ -0,0 +1 @@
1
+ {"version":3,"file":"log.js","sourceRoot":"","sources":["../src/log.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,QAAQ,EAAc,MAAM,gBAAgB,CAAC;AACtD,OAAO,EAAE,YAAY,EAAE,MAAM,gBAAgB,CAAC;AAE9C,MAAM,MAAM,GAAG,YAAY,CAAC,WAAW,CAAC,CAAC;AAEzC,MAAM,CAAC,MAAM,GAAG,GAAG,CAAC,OAAe,EAAE,OAAoB,EAAkB,EAAE;IAC3E,MAAM,EAAE,KAAK,GAAG,QAAQ,CAAC,IAAI,EAAE,GAAG,IAAI,EAAE,GAAG,OAAO,IAAI,EAAE,CAAC;IACzD,OAAO,MAAM,CAAC,GAAG,CAAC,KAAK,EAAE,OAAO,EAAE,IAAI,CAAC,CAAC;AAC1C,CAAC,CAAC"}
@@ -0,0 +1,4 @@
1
+ import { createLogger, format, transports } from './createLogger';
2
+ import { LogLevel } from './logger.types';
3
+ import { log } from './log';
4
+ export { log, createLogger, format, transports, LogLevel };
@@ -0,0 +1,5 @@
1
+ import { createLogger, format, transports } from './createLogger';
2
+ import { LogLevel } from './logger.types';
3
+ import { log } from './log';
4
+ export { log, createLogger, format, transports, LogLevel };
5
+ //# sourceMappingURL=logger-index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"logger-index.js","sourceRoot":"","sources":["../src/logger-index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,MAAM,EAAE,UAAU,EAAE,MAAM,gBAAgB,CAAC;AAClE,OAAO,EAAE,QAAQ,EAAE,MAAM,gBAAgB,CAAC;AAC1C,OAAO,EAAE,GAAG,EAAE,MAAM,OAAO,CAAC;AAE5B,OAAO,EAAE,GAAG,EAAE,YAAY,EAAE,MAAM,EAAE,UAAU,EAAE,QAAQ,EAAE,CAAC"}
@@ -0,0 +1,11 @@
1
+ import { LoggerOptions } from 'winston';
2
+ export declare enum LogLevel {
3
+ Verbose = "verbose",
4
+ Info = "info",
5
+ Debug = "debug",
6
+ Error = "error",
7
+ Warning = "warn"
8
+ }
9
+ export interface LogOptions extends LoggerOptions {
10
+ level: LogLevel;
11
+ }
@@ -0,0 +1,9 @@
1
+ export var LogLevel;
2
+ (function (LogLevel) {
3
+ LogLevel["Verbose"] = "verbose";
4
+ LogLevel["Info"] = "info";
5
+ LogLevel["Debug"] = "debug";
6
+ LogLevel["Error"] = "error";
7
+ LogLevel["Warning"] = "warn";
8
+ })(LogLevel || (LogLevel = {}));
9
+ //# sourceMappingURL=logger.types.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"logger.types.js","sourceRoot":"","sources":["../src/logger.types.ts"],"names":[],"mappings":"AAEA,MAAM,CAAN,IAAY,QAMX;AAND,WAAY,QAAQ;IAClB,+BAAmB,CAAA;IACnB,yBAAa,CAAA;IACb,2BAAe,CAAA;IACf,2BAAe,CAAA;IACf,4BAAgB,CAAA;AAClB,CAAC,EANW,QAAQ,KAAR,QAAQ,QAMnB"}
package/package.js ADDED
@@ -0,0 +1,21 @@
1
+ Package.describe({
2
+ name: 'unchained:logger',
3
+ version: '1.1.0',
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
+ });
package/package.json ADDED
@@ -0,0 +1,51 @@
1
+ {
2
+ "name": "@unchainedshop/logger",
3
+ "version": "1.1.0",
4
+ "main": "lib/logger-index.js",
5
+ "types": "lib/logger-index.d.ts",
6
+ "type": "module",
7
+ "scripts": {
8
+ "clean": "rm -rf lib",
9
+ "build": "npm run clean && tsc -p tsconfig.build.json",
10
+ "watch": "tsc --watch",
11
+ "link:core": "npm link @unchainedshop/types",
12
+ "test": "jest --watch"
13
+ },
14
+ "repository": {
15
+ "type": "git",
16
+ "url": "git+https://github.com/unchainedshop/unchained.git"
17
+ },
18
+ "keywords": [
19
+ "unchained",
20
+ "ecommerce",
21
+ "core"
22
+ ],
23
+ "author": "Joël Meiller",
24
+ "license": "EUPL-1.2",
25
+ "bugs": {
26
+ "url": "https://github.com/unchainedshop/unchained/issues"
27
+ },
28
+ "homepage": "https://github.com/unchainedshop/unchained#readme",
29
+ "dependencies": {
30
+ "safe-stable-stringify": "^2.3.1",
31
+ "winston": "^3.7.2",
32
+ "winston-transport": "^4.5.0"
33
+ },
34
+ "devDependencies": {
35
+ "@babel/core": "^7.18.5",
36
+ "@babel/preset-env": "^7.18.2",
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",
41
+ "@types/winston": "^2.4.4",
42
+ "@unchainedshop/types": "../@unchainedshop/types",
43
+ "babel-jest": "^28.1.1",
44
+ "chai": "^4.3.6",
45
+ "cross-env": "^7.0.3",
46
+ "jest": "^28.1.1",
47
+ "ts-jest": "^28.0.5",
48
+ "ts-node": "^10.8.1",
49
+ "typescript": "^4.7.3"
50
+ }
51
+ }
@@ -0,0 +1,23 @@
1
+ {
2
+ "compilerOptions": {
3
+ "allowJs": true,
4
+ "allowSyntheticDefaultImports": true,
5
+ "declaration": true,
6
+ "esModuleInterop": true,
7
+ "experimentalDecorators": true,
8
+ "forceConsistentCasingInFileNames": true,
9
+ "lib": ["esnext"],
10
+ "module": "esnext",
11
+ "moduleResolution": "node",
12
+ "noImplicitReturns": true,
13
+ "noUnusedLocals": false,
14
+ "outDir": "lib",
15
+ "preserveWatchOutput": true,
16
+ "skipLibCheck": true,
17
+ "sourceMap": true,
18
+ "target": "esnext",
19
+ "types": ["node"]
20
+ },
21
+ "include": ["src"]
22
+ }
23
+