File size: 1,772 Bytes
befd7da
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
'use strict';

const omit = require('lodash.omit');

// // //

/**
 * Sentry stream for Bunyan
 */
class SentryStream {

  /**
   * SentryStream constructor
   * @param  {Object} client the Sentry client
   * @return {void}
   */
  constructor(client) {
    this.client = client;
  }

  /**
   * Method call by Bunyan to save log record
   * @param  {Object} record log properties
   * @return {Boolean}        true
   */
  write(record) {
    const err = record.err;
    const tags = record.tags;
    const level = this.getSentryLevel(record);

    if (err) {
      const extra = omit(record, 'err', 'tags');
      this.client.captureException(this.deserializeError(err), { extra, level, tags });
    } else {
      const extra = omit(record, 'msg', 'tags');
      this.client.captureMessage(record.msg, { extra, level, tags });
    }
    return (true);
  }

  /**
   * Convert Bunyan level number to Sentry level label.
   * Rule : >50=error ; 40=warning ; info otherwise
   * @param  {Object} record Bunyan log record
   * @return {String}        Sentry level
   */
  getSentryLevel(record) {
    const level = record.level;

    if (level >= 50) return 'error';
    if (level === 40) return 'warning';

    return 'info';
  }

  /**
   * Error deserialiazing function. Bunyan serialize the error to object : https://github.com/trentm/node-bunyan/blob/master/lib/bunyan.js#L1089
   * @param  {object} data serialized Bunyan
   * @return {Error}      the deserialiazed error
   */
  deserializeError(data) {
    if (data instanceof Error) return data;

    const error = new Error(data.message);
    error.name = data.name;
    error.stack = data.stack;
    error.code = data.code;
    error.signal = data.signal;
    return error;
  }
}

module.exports = SentryStream;