@zenvia/logger 1.6.1 → 1.6.2

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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@zenvia/logger",
3
- "version": "1.6.1",
3
+ "version": "1.6.2",
4
4
  "description": "A wrapper for Winston Logging Node.js library that formats the output on STDOUT as Logstash JSON format.",
5
5
  "license": "MIT",
6
6
  "main": "./src/index",
package/src/lib/logger.js CHANGED
@@ -10,25 +10,7 @@ const rTrace = require('cls-rtracer');
10
10
 
11
11
  const appPackage = require(path.join(appRootDir, 'package'));
12
12
 
13
- const sanitizeInfo = (info) => {
14
- const sanitizeCRLFInjection = (str) => str
15
- .replace(/\n|\r/g, (x) => (x === '\n' ? '#n' : '#r'));
16
-
17
- Object.keys(info).forEach((key) => {
18
- if (typeof info[key] === 'string') {
19
- info[key] = sanitizeCRLFInjection(info[key]);
20
- return;
21
- }
22
-
23
- if (info[key] instanceof Function) {
24
- delete info[key];
25
- }
26
- });
27
- };
28
-
29
13
  const customFormatJson = winston.format((info) => {
30
- sanitizeInfo(info);
31
-
32
14
  let stack;
33
15
 
34
16
  if (info.stack) {
@@ -49,6 +31,12 @@ const customFormatJson = winston.format((info) => {
49
31
  traceId: rTrace.id(),
50
32
  };
51
33
 
34
+ Object.keys(info).forEach((key) => {
35
+ if (info[key] instanceof Function) {
36
+ delete info[key];
37
+ }
38
+ });
39
+
52
40
  return info;
53
41
  });
54
42
 
@@ -70,6 +70,21 @@ describe('Logger test', () => {
70
70
  JSON.parse(actualOutput).should.be.deep.equal(expectedOutput);
71
71
  });
72
72
 
73
+ it('should remove attributes that are log functions, leaving only the @timestamp, application, message and level fields', () => {
74
+ logger.info('some message', { field1: () => {} });
75
+ const expectedOutput = {
76
+ '@timestamp': '2018-06-05T18:20:42.345Z',
77
+ '@version': 1,
78
+ application: 'application-name',
79
+ host: os.hostname(),
80
+ message: 'some message',
81
+ level: 'INFO',
82
+ };
83
+
84
+ const actualOutput = stdMocks.flush().stdout[0];
85
+ JSON.parse(actualOutput).should.be.deep.equal(expectedOutput);
86
+ });
87
+
73
88
  it('should log @timestamp, application, message, level and environment fields', () => {
74
89
  process.env.NODE_ENV = 'test';
75
90
  logger.info('some message');
@@ -241,50 +256,6 @@ describe('Logger test', () => {
241
256
  });
242
257
 
243
258
  describe('Logging format', () => {
244
- it('should replace LF characters from log (POSIX systems)', () => {
245
- logger.debug(`some message
246
- other CRLF injection message`);
247
- const expectedOutput = {
248
- '@timestamp': '2018-06-05T18:20:42.345Z',
249
- '@version': 1,
250
- application: 'application-name',
251
- host: os.hostname(),
252
- message: 'some message#nother CRLF injection message',
253
- level: 'DEBUG',
254
- };
255
-
256
- const actualOutput = stdMocks.flush().stdout[0];
257
- JSON.parse(actualOutput).should.be.deep.equal(expectedOutput);
258
-
259
- logger.debug('some\n CRLF\n injection\n message');
260
- const expectedOutput2 = {
261
- '@timestamp': '2018-06-05T18:20:42.345Z',
262
- '@version': 1,
263
- application: 'application-name',
264
- host: os.hostname(),
265
- message: 'some#n CRLF#n injection#n message',
266
- level: 'DEBUG',
267
- };
268
-
269
- const actualOutput2 = stdMocks.flush().stdout[0];
270
- JSON.parse(actualOutput2).should.be.deep.equal(expectedOutput2);
271
- });
272
-
273
- it('should replace CRLF characters from log (Windows systems)', () => {
274
- logger.debug('some\r\n CRLF\r\n injection\r\n message');
275
- const expectedOutput = {
276
- '@timestamp': '2018-06-05T18:20:42.345Z',
277
- '@version': 1,
278
- application: 'application-name',
279
- host: os.hostname(),
280
- message: 'some#r#n CRLF#r#n injection#r#n message',
281
- level: 'DEBUG',
282
- };
283
-
284
- const actualOutput = stdMocks.flush().stdout[0];
285
- JSON.parse(actualOutput).should.be.deep.equal(expectedOutput);
286
- });
287
-
288
259
  it('should get not format when LOGGING_FORMATTER_DISABLED environment is true', () => {
289
260
  delete require.cache[require.resolve('../../src/lib/logger')];
290
261
  process.env.LOGGING_FORMATTER_DISABLED = 'true';