@orion-js/logger 3.12.0 → 3.13.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.
- package/LICENSE +21 -0
- package/jest.config.js +8 -0
- package/lib/formats.d.ts +6 -0
- package/lib/formats.js +81 -0
- package/lib/helpers/getFileName.d.ts +2 -0
- package/lib/helpers/getFileName.js +35 -0
- package/lib/helpers/getFileName.test.d.ts +1 -0
- package/lib/helpers/getFileName.test.js +15 -0
- package/lib/index.d.ts +5 -0
- package/lib/index.js +21 -0
- package/lib/index.test.d.ts +1 -0
- package/lib/index.test.js +24 -0
- package/lib/logger.d.ts +8 -0
- package/lib/logger.js +63 -0
- package/lib/types/index.d.ts +10 -0
- package/lib/types/index.js +2 -0
- package/package.json +12 -22
- package/tsconfig.json +15 -0
- package/yarn-error.log +2688 -0
- package/dist/index.cjs +0 -17110
- package/dist/index.d.ts +0 -31
- package/dist/index.js +0 -17083
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2022 Orionjs Team
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/jest.config.js
ADDED
package/lib/formats.d.ts
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { transports } from 'winston';
|
|
2
|
+
export declare const sentryFormat: import("logform").FormatWrap;
|
|
3
|
+
export declare const textConsoleFormat: import("logform").Format;
|
|
4
|
+
export declare const textConsoleTransport: transports.ConsoleTransportInstance;
|
|
5
|
+
export declare const jsonConsoleFormat: import("logform").Format;
|
|
6
|
+
export declare const jsonConsoleTransport: transports.ConsoleTransportInstance;
|
package/lib/formats.js
ADDED
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.jsonConsoleTransport = exports.jsonConsoleFormat = exports.textConsoleTransport = exports.textConsoleFormat = exports.sentryFormat = void 0;
|
|
7
|
+
const winston_1 = require("winston");
|
|
8
|
+
const node_util_1 = __importDefault(require("node:util"));
|
|
9
|
+
const lodash_1 = require("lodash");
|
|
10
|
+
const api_1 = __importDefault(require("@opentelemetry/api"));
|
|
11
|
+
const { metadata, timestamp, json, colorize, combine, printf } = winston_1.format;
|
|
12
|
+
const opentelemetryContext = (0, winston_1.format)(info => {
|
|
13
|
+
const activeSpan = api_1.default.trace.getActiveSpan();
|
|
14
|
+
if (activeSpan) {
|
|
15
|
+
const spanContex = activeSpan.spanContext();
|
|
16
|
+
if (activeSpan.name && !info.context) {
|
|
17
|
+
info.context = activeSpan.name;
|
|
18
|
+
}
|
|
19
|
+
const fields = {
|
|
20
|
+
trace_id: spanContex.traceId,
|
|
21
|
+
span_id: spanContex.spanId,
|
|
22
|
+
trace_flags: `0${spanContex.traceFlags.toString(16)}`,
|
|
23
|
+
};
|
|
24
|
+
Object.assign(info, fields);
|
|
25
|
+
}
|
|
26
|
+
return info;
|
|
27
|
+
});
|
|
28
|
+
const metaError = (0, winston_1.format)(info => {
|
|
29
|
+
if (info?.metadata?.value?.error instanceof Error) {
|
|
30
|
+
info.stack = info?.metadata?.value?.error.stack;
|
|
31
|
+
info.errorMessage = info?.metadata?.value?.error.message;
|
|
32
|
+
delete info?.metadata?.value?.error;
|
|
33
|
+
}
|
|
34
|
+
if (info?.metadata?.value instanceof Error) {
|
|
35
|
+
info.stack = info?.metadata?.value.stack;
|
|
36
|
+
info.errorMessage = info?.metadata?.value.message;
|
|
37
|
+
delete info?.metadata?.value;
|
|
38
|
+
}
|
|
39
|
+
return info;
|
|
40
|
+
});
|
|
41
|
+
exports.sentryFormat = (0, winston_1.format)(info => {
|
|
42
|
+
const { path, label, ...extra } = info;
|
|
43
|
+
return {
|
|
44
|
+
...extra,
|
|
45
|
+
tags: {
|
|
46
|
+
path: path || '',
|
|
47
|
+
request_id: label,
|
|
48
|
+
},
|
|
49
|
+
};
|
|
50
|
+
});
|
|
51
|
+
function getMetadataText(metadata) {
|
|
52
|
+
const { value, ...rest } = metadata;
|
|
53
|
+
if ((0, lodash_1.isEmpty)(rest)) {
|
|
54
|
+
if (typeof value === 'undefined')
|
|
55
|
+
return '';
|
|
56
|
+
return node_util_1.default.inspect(value);
|
|
57
|
+
}
|
|
58
|
+
return `${node_util_1.default.inspect(value)} ${node_util_1.default.inspect(rest)}`;
|
|
59
|
+
}
|
|
60
|
+
exports.textConsoleFormat = combine(colorize(), metadata({ fillExcept: ['fileName', 'level', 'message', 'stack'] }), opentelemetryContext(), metaError(), timestamp(), printf(info => {
|
|
61
|
+
// console.log(info)
|
|
62
|
+
const date = new Date(info.timestamp);
|
|
63
|
+
const timeLabel = `${date.getHours()}:${date.getMinutes()}:${date.getSeconds()}`;
|
|
64
|
+
const fileNameLabel = info.fileName ? `[${info.fileName}]` : '';
|
|
65
|
+
const stack = info.stack ? `\n${info.stack}` : '';
|
|
66
|
+
const value = getMetadataText(info.metadata);
|
|
67
|
+
const traceId = info.trace_id
|
|
68
|
+
? `${String(info.trace_id).substring(0, 8)}@${String(info.span_id).substring(0, 8)}`
|
|
69
|
+
: '';
|
|
70
|
+
const context = `${info.context || ''} ${traceId}`.trim();
|
|
71
|
+
return `[${info.level}] [${timeLabel}] [${context}] ${fileNameLabel} ${info.message} ${value} ${stack}`;
|
|
72
|
+
}));
|
|
73
|
+
exports.textConsoleTransport = new winston_1.transports.Console({
|
|
74
|
+
handleExceptions: true,
|
|
75
|
+
format: exports.textConsoleFormat,
|
|
76
|
+
});
|
|
77
|
+
exports.jsonConsoleFormat = combine(metadata({ fillExcept: ['fileName', 'level', 'message'] }), opentelemetryContext(), metaError(), timestamp(), json());
|
|
78
|
+
exports.jsonConsoleTransport = new winston_1.transports.Console({
|
|
79
|
+
handleExceptions: true,
|
|
80
|
+
format: exports.jsonConsoleFormat,
|
|
81
|
+
});
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.improveFileName = exports.getFileName = void 0;
|
|
4
|
+
const getFileName = () => {
|
|
5
|
+
try {
|
|
6
|
+
const stack = new Error().stack;
|
|
7
|
+
const lines = stack.split('\n');
|
|
8
|
+
const filePath = lines[3].split('(')[1].split(')')[0];
|
|
9
|
+
return (0, exports.improveFileName)(filePath);
|
|
10
|
+
}
|
|
11
|
+
catch (error) {
|
|
12
|
+
return;
|
|
13
|
+
}
|
|
14
|
+
};
|
|
15
|
+
exports.getFileName = getFileName;
|
|
16
|
+
const improveFileName = (path) => {
|
|
17
|
+
if (path.includes('orionjs/packages')) {
|
|
18
|
+
return path.replace(/^.+\/orionjs\/packages\//, '@orion-js/');
|
|
19
|
+
}
|
|
20
|
+
if (path.includes('.orion/build')) {
|
|
21
|
+
return path.replace(/^.+\.orion\/build\//, '');
|
|
22
|
+
}
|
|
23
|
+
if (path.includes('/node_modules/@')) {
|
|
24
|
+
const after = path.split('/node_modules/')[1];
|
|
25
|
+
const parts = after.split('/');
|
|
26
|
+
return `${parts[0]}/${parts[1]}`;
|
|
27
|
+
}
|
|
28
|
+
if (path.includes('/node_modules/')) {
|
|
29
|
+
const after = path.split('/node_modules/')[1];
|
|
30
|
+
const parts = after.split('/');
|
|
31
|
+
return `${parts[0]}`;
|
|
32
|
+
}
|
|
33
|
+
return path;
|
|
34
|
+
};
|
|
35
|
+
exports.improveFileName = improveFileName;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const getFileName_1 = require("./getFileName");
|
|
4
|
+
describe('GetFileName', () => {
|
|
5
|
+
it('Should clean @package in node_modules', () => {
|
|
6
|
+
const fileName = '/Users/nicolaslopezj/Code/Projects/justo/drivers/server/node_modules/@orion-js/dogs/lib/services/WorkerService.js:39:25';
|
|
7
|
+
const improvedFileName = (0, getFileName_1.improveFileName)(fileName);
|
|
8
|
+
expect(improvedFileName).toBe('@orion-js/dogs');
|
|
9
|
+
});
|
|
10
|
+
it('Should clean simple package in node modules', () => {
|
|
11
|
+
const fileName = '/Users/nicolaslopezj/Code/Projects/justo/drivers/server/node_modules/dogs/lib/services/WorkerService.js:39:25';
|
|
12
|
+
const improvedFileName = (0, getFileName_1.improveFileName)(fileName);
|
|
13
|
+
expect(improvedFileName).toBe('dogs');
|
|
14
|
+
});
|
|
15
|
+
});
|
package/lib/index.d.ts
ADDED
package/lib/index.js
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
|
|
5
|
+
}) : (function(o, m, k, k2) {
|
|
6
|
+
if (k2 === undefined) k2 = k;
|
|
7
|
+
o[k2] = m[k];
|
|
8
|
+
}));
|
|
9
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
10
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
11
|
+
};
|
|
12
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
13
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
14
|
+
};
|
|
15
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
16
|
+
exports.winston = void 0;
|
|
17
|
+
const winston_1 = __importDefault(require("winston"));
|
|
18
|
+
exports.winston = winston_1.default;
|
|
19
|
+
__exportStar(require("./formats"), exports);
|
|
20
|
+
__exportStar(require("./logger"), exports);
|
|
21
|
+
__exportStar(require("./types"), exports);
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const logger_1 = require("./logger");
|
|
4
|
+
(0, logger_1.setLogLevel)('debug');
|
|
5
|
+
describe('Log', () => {
|
|
6
|
+
test('Test logs', () => {
|
|
7
|
+
logger_1.logger.debug('debug');
|
|
8
|
+
logger_1.logger.info('info');
|
|
9
|
+
logger_1.logger.warn('warn');
|
|
10
|
+
logger_1.logger.warn('warn', { info: 'of the log' });
|
|
11
|
+
});
|
|
12
|
+
it('shouldnt fail when passing a non object to the metadata', () => {
|
|
13
|
+
logger_1.logger.info('info', 'a string');
|
|
14
|
+
logger_1.logger.info('info', ['array']);
|
|
15
|
+
});
|
|
16
|
+
it('should log errors correctly', () => {
|
|
17
|
+
logger_1.logger.error('an error', new Error('message'));
|
|
18
|
+
logger_1.logger.error('an error2', { error: new Error('message'), info: 'of the log' });
|
|
19
|
+
});
|
|
20
|
+
it('should pass the context', () => {
|
|
21
|
+
const withMeta = logger_1.logger.addMetadata({ context: 'context' });
|
|
22
|
+
withMeta.info('info', { notContext: 'notContext' });
|
|
23
|
+
});
|
|
24
|
+
});
|
package/lib/logger.d.ts
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import winston from 'winston';
|
|
2
|
+
import { OrionLogger } from './types';
|
|
3
|
+
export declare const winstonLogger: winston.Logger;
|
|
4
|
+
export declare const configureLogger: (options: winston.LoggerOptions) => void;
|
|
5
|
+
export declare const setLogLevel: (level: string) => string;
|
|
6
|
+
export declare const addTransport: (transport: winston.transport) => winston.Logger;
|
|
7
|
+
export declare const getLogger: (context: string) => winston.Logger;
|
|
8
|
+
export declare const logger: OrionLogger;
|
package/lib/logger.js
ADDED
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.logger = exports.getLogger = exports.addTransport = exports.setLogLevel = exports.configureLogger = exports.winstonLogger = void 0;
|
|
4
|
+
const winston_1 = require("winston");
|
|
5
|
+
const formats_1 = require("./formats");
|
|
6
|
+
const getFileName_1 = require("./helpers/getFileName");
|
|
7
|
+
const transports = [
|
|
8
|
+
process.env.ORION_DEV || process.env.JEST_WORKER_ID ? formats_1.textConsoleTransport : formats_1.jsonConsoleTransport
|
|
9
|
+
];
|
|
10
|
+
exports.winstonLogger = (0, winston_1.createLogger)({
|
|
11
|
+
levels: winston_1.config.npm.levels,
|
|
12
|
+
handleExceptions: true,
|
|
13
|
+
format: winston_1.format.errors({ stack: true }),
|
|
14
|
+
transports: transports
|
|
15
|
+
});
|
|
16
|
+
const configureLogger = (options) => {
|
|
17
|
+
return exports.winstonLogger.configure(options);
|
|
18
|
+
};
|
|
19
|
+
exports.configureLogger = configureLogger;
|
|
20
|
+
const setLogLevel = (level) => {
|
|
21
|
+
return (exports.winstonLogger.level = level);
|
|
22
|
+
};
|
|
23
|
+
exports.setLogLevel = setLogLevel;
|
|
24
|
+
const addTransport = (transport) => {
|
|
25
|
+
return exports.winstonLogger.add(transport);
|
|
26
|
+
};
|
|
27
|
+
exports.addTransport = addTransport;
|
|
28
|
+
const getLogger = (context) => {
|
|
29
|
+
return exports.winstonLogger.child({ context });
|
|
30
|
+
};
|
|
31
|
+
exports.getLogger = getLogger;
|
|
32
|
+
const createLogger = (logger) => {
|
|
33
|
+
return {
|
|
34
|
+
debug: (message, value) => {
|
|
35
|
+
const fileName = (0, getFileName_1.getFileName)();
|
|
36
|
+
return logger.debug({ message, fileName, value });
|
|
37
|
+
},
|
|
38
|
+
info: (message, value) => {
|
|
39
|
+
const fileName = (0, getFileName_1.getFileName)();
|
|
40
|
+
return logger.info({ message, fileName, value });
|
|
41
|
+
},
|
|
42
|
+
warn: (message, value) => {
|
|
43
|
+
const fileName = (0, getFileName_1.getFileName)();
|
|
44
|
+
return logger.warn({ message, fileName, value });
|
|
45
|
+
},
|
|
46
|
+
error: (message, value) => {
|
|
47
|
+
const fileName = (0, getFileName_1.getFileName)();
|
|
48
|
+
return logger.error({ message, fileName, value });
|
|
49
|
+
},
|
|
50
|
+
addContext: (module) => {
|
|
51
|
+
if (module.id) {
|
|
52
|
+
const split = String(module.id).split('.orion/build/');
|
|
53
|
+
const fileName = split.length > 1 ? split[1] : split[0];
|
|
54
|
+
return createLogger(logger.child({ fileName }));
|
|
55
|
+
}
|
|
56
|
+
return createLogger(logger.child({}));
|
|
57
|
+
},
|
|
58
|
+
addMetadata: (metadata) => {
|
|
59
|
+
return createLogger(logger.child(metadata));
|
|
60
|
+
}
|
|
61
|
+
};
|
|
62
|
+
};
|
|
63
|
+
exports.logger = createLogger(exports.winstonLogger);
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
/// <reference types="node" />
|
|
2
|
+
export declare type LogFunction = (message: string, metadata?: any) => void;
|
|
3
|
+
export interface OrionLogger {
|
|
4
|
+
debug: LogFunction;
|
|
5
|
+
info: LogFunction;
|
|
6
|
+
warn: LogFunction;
|
|
7
|
+
error: LogFunction;
|
|
8
|
+
addContext: (module: NodeJS.Module) => OrionLogger;
|
|
9
|
+
addMetadata: (metadata: any) => OrionLogger;
|
|
10
|
+
}
|
package/package.json
CHANGED
|
@@ -1,17 +1,16 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@orion-js/logger",
|
|
3
|
-
"version": "3.
|
|
4
|
-
"main": "
|
|
3
|
+
"version": "3.13.1",
|
|
4
|
+
"main": "lib/index.js",
|
|
5
5
|
"author": "nicolaslopezj",
|
|
6
6
|
"license": "MIT",
|
|
7
7
|
"scripts": {
|
|
8
|
-
"test": "
|
|
8
|
+
"test": "ORION_DEV=1 jest",
|
|
9
9
|
"prepare": "yarn run build",
|
|
10
|
-
"clean": "rm -rf ./
|
|
11
|
-
"build": "
|
|
10
|
+
"clean": "rm -rf ./lib",
|
|
11
|
+
"build": "yarn run clean && tsc",
|
|
12
12
|
"watch": "tsc -w",
|
|
13
|
-
"upgrade-interactive": "yarn upgrade-interactive"
|
|
14
|
-
"dev": "bun --watch src/index.ts"
|
|
13
|
+
"upgrade-interactive": "yarn upgrade-interactive"
|
|
15
14
|
},
|
|
16
15
|
"dependencies": {
|
|
17
16
|
"@opentelemetry/api": "^1.9.0",
|
|
@@ -22,23 +21,14 @@
|
|
|
22
21
|
"winston": "^3.6.0"
|
|
23
22
|
},
|
|
24
23
|
"devDependencies": {
|
|
24
|
+
"@types/jest": "^27.0.2",
|
|
25
25
|
"@types/lodash": "^4.14.182",
|
|
26
|
-
"
|
|
27
|
-
"
|
|
26
|
+
"jest": "27.3.1",
|
|
27
|
+
"ts-jest": "27.0.7",
|
|
28
|
+
"typescript": "^4.4.4"
|
|
28
29
|
},
|
|
29
30
|
"publishConfig": {
|
|
30
31
|
"access": "public"
|
|
31
32
|
},
|
|
32
|
-
"gitHead": "
|
|
33
|
-
|
|
34
|
-
"module": "./dist/index.js",
|
|
35
|
-
"types": "./dist/index.d.ts",
|
|
36
|
-
"exports": {
|
|
37
|
-
"types": "./dist/index.d.ts",
|
|
38
|
-
"import": "./dist/index.js",
|
|
39
|
-
"require": "./dist/index.cjs"
|
|
40
|
-
},
|
|
41
|
-
"files": [
|
|
42
|
-
"dist"
|
|
43
|
-
]
|
|
44
|
-
}
|
|
33
|
+
"gitHead": "18ab5a9711863e6bd3d3c61e7bb8f9067afca82c"
|
|
34
|
+
}
|
package/tsconfig.json
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
{
|
|
2
|
+
"compilerOptions": {
|
|
3
|
+
"strict": false,
|
|
4
|
+
"outDir": "./lib",
|
|
5
|
+
"rootDir": "./src",
|
|
6
|
+
"module": "commonjs",
|
|
7
|
+
"target": "es2020",
|
|
8
|
+
"moduleResolution": "node",
|
|
9
|
+
"lib": ["es2020"],
|
|
10
|
+
"declaration": true,
|
|
11
|
+
"esModuleInterop": true,
|
|
12
|
+
"skipLibCheck": true,
|
|
13
|
+
"forceConsistentCasingInFileNames": true
|
|
14
|
+
}
|
|
15
|
+
}
|