@autofleet/logger 4.0.3 → 4.0.4
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/package.json +4 -3
- package/readme.md +63 -0
- package/tsconfig.build.json +9 -0
- package/dist/example.d.ts +0 -1
- package/dist/example.js +0 -14
- package/dist/index.test.d.ts +0 -1
- package/dist/index.test.js +0 -50
package/package.json
CHANGED
|
@@ -1,14 +1,15 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@autofleet/logger",
|
|
3
|
-
"version": "4.0.
|
|
3
|
+
"version": "4.0.4",
|
|
4
4
|
"description": "",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
|
+
"types": "dist/index.d.ts",
|
|
6
7
|
"scripts": {
|
|
7
8
|
"coverage": "jest --coverage --forceExit --runInBand",
|
|
8
9
|
"test": "jest --runInBand",
|
|
9
10
|
"test-auto": "jest --watch --runInBand",
|
|
10
|
-
"linter": "
|
|
11
|
-
"build": "tsc",
|
|
11
|
+
"linter": "eslint ./src",
|
|
12
|
+
"build": "tsc --project tsconfig.build.json",
|
|
12
13
|
"prepublishOnly": "npm run build",
|
|
13
14
|
"example": "ts-node example.ts"
|
|
14
15
|
},
|
package/readme.md
ADDED
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
# Autofleet Node.js Logger
|
|
2
|
+
|
|
3
|
+
A Winston-like logger for Node.js backend, now using Pino.
|
|
4
|
+
|
|
5
|
+
## Usage:
|
|
6
|
+
The logger provides a Winston-like API once initialized:
|
|
7
|
+
|
|
8
|
+
```js
|
|
9
|
+
import Logger from '@autofleet/logger';
|
|
10
|
+
|
|
11
|
+
const logger = Logger();
|
|
12
|
+
|
|
13
|
+
logger.info('it is working');
|
|
14
|
+
logger.error('errors displayed in logs/error.log', new Error('test error'));
|
|
15
|
+
logger.debug('debug with object', { a: 5 });
|
|
16
|
+
```
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
# Migration to V4
|
|
20
|
+
|
|
21
|
+
In V4, we transitioned the underlying logger from Winston to Pino. Since Pino has a different API, we’ve added class methods to maintain a similar API, with two key changes:
|
|
22
|
+
* **Context Middleware:** Previously, the Zehut package would receive a Winston logger upon initialization to enable automatic tracing in logs. To remove this coupling, we’ve introduced a new method, addContextMiddleware, which allows you to add labels to each log. You can use this method to add the trace ID (see example below).
|
|
23
|
+
* **TypeScript Compatibility:** Due to the migration to TypeScript, the import statement may differ in non-TypeScript files.
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
**Old Logger**
|
|
27
|
+
```js
|
|
28
|
+
const Logger = require('@autofleet/logger');
|
|
29
|
+
|
|
30
|
+
enableTracing({
|
|
31
|
+
outbreakOptions: {
|
|
32
|
+
headersPrefix: 'x-af',
|
|
33
|
+
winstonLogger: logger,
|
|
34
|
+
},
|
|
35
|
+
});
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
**New Logger**
|
|
39
|
+
```javascript
|
|
40
|
+
// Typescript
|
|
41
|
+
import Logger from '@autofleet/logger';
|
|
42
|
+
|
|
43
|
+
const logger = Logger();
|
|
44
|
+
|
|
45
|
+
logger.addContextMiddleware(() => ({
|
|
46
|
+
traceId: zehut.outbreak.getCurrentContext()?.context?.get('x-trace-id'),
|
|
47
|
+
}));
|
|
48
|
+
|
|
49
|
+
// Plain nodejs
|
|
50
|
+
const { default: Logger } = require('@autofleet/logger');
|
|
51
|
+
|
|
52
|
+
const logger = Logger();
|
|
53
|
+
|
|
54
|
+
logger.addContextMiddleware(() => {
|
|
55
|
+
try {
|
|
56
|
+
return {
|
|
57
|
+
traceId: zehut.outbreak.getCurrentContext()?.context?.get('x-trace-id')
|
|
58
|
+
}
|
|
59
|
+
} catch(e) {
|
|
60
|
+
return {}
|
|
61
|
+
}
|
|
62
|
+
});
|
|
63
|
+
```
|
package/dist/example.d.ts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|
package/dist/example.js
DELETED
|
@@ -1,14 +0,0 @@
|
|
|
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
|
-
const index_1 = __importDefault(require("./index"));
|
|
7
|
-
const logger = (0, index_1.default)('info');
|
|
8
|
-
logger.info('it is working');
|
|
9
|
-
logger.error('errors displayed in logs/error.log', new Error('test error'));
|
|
10
|
-
logger.debug('debug with object', { a: 5 });
|
|
11
|
-
const loggerMiddleware = () => ({ middleware: Math.random() });
|
|
12
|
-
logger.addContextMiddleware(loggerMiddleware);
|
|
13
|
-
logger.info('middleware added', { a: 5 });
|
|
14
|
-
logger.error('middleware added', new Error('test error'));
|
package/dist/index.test.d.ts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|
package/dist/index.test.js
DELETED
|
@@ -1,50 +0,0 @@
|
|
|
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
|
-
const index_1 = __importDefault(require("./index"));
|
|
7
|
-
const { env } = process;
|
|
8
|
-
describe('Logger', () => {
|
|
9
|
-
it('it`s default level is info', () => {
|
|
10
|
-
env.NODE_ENV = '';
|
|
11
|
-
const logger = (0, index_1.default)();
|
|
12
|
-
expect(logger).toBeDefined();
|
|
13
|
-
expect(logger.level).toBe('debug');
|
|
14
|
-
logger.info('test');
|
|
15
|
-
});
|
|
16
|
-
it('its default develpment level is debug', () => {
|
|
17
|
-
env.NODE_ENV = 'development';
|
|
18
|
-
const logger = (0, index_1.default)();
|
|
19
|
-
expect(logger.level).toBe('debug');
|
|
20
|
-
});
|
|
21
|
-
it('its default test level is info', () => {
|
|
22
|
-
env.NODE_ENV = 'test';
|
|
23
|
-
const logger = (0, index_1.default)();
|
|
24
|
-
expect(logger.level).toBe('info');
|
|
25
|
-
});
|
|
26
|
-
it('its default production level is info', () => {
|
|
27
|
-
env.NODE_ENV = 'production';
|
|
28
|
-
const logger = (0, index_1.default)();
|
|
29
|
-
expect(logger.level).toBe('info');
|
|
30
|
-
});
|
|
31
|
-
it('its level can be override by LOG_LEVEL env variable', () => {
|
|
32
|
-
env.NODE_ENV = 'development';
|
|
33
|
-
env.LOG_LEVEL = 'warn';
|
|
34
|
-
const logger = (0, index_1.default)();
|
|
35
|
-
expect(logger.level).toBe('warn');
|
|
36
|
-
});
|
|
37
|
-
it('its level can be override by function variable', () => {
|
|
38
|
-
env.NODE_ENV = 'production';
|
|
39
|
-
env.LOG_LEVEL = 'warn';
|
|
40
|
-
const logger = (0, index_1.default)('info');
|
|
41
|
-
expect(logger.level).toBe('info');
|
|
42
|
-
});
|
|
43
|
-
it('its has functions for each level', () => {
|
|
44
|
-
const logger = (0, index_1.default)();
|
|
45
|
-
expect(typeof logger.error).toBe('function');
|
|
46
|
-
expect(typeof logger.warn).toBe('function');
|
|
47
|
-
expect(typeof logger.info).toBe('function');
|
|
48
|
-
expect(typeof logger.debug).toBe('function');
|
|
49
|
-
});
|
|
50
|
-
});
|