UNPKG

4.13 kBJavaScriptView Raw
1const path = require('path');
2const uuid = require("node-uuid");
3const logger = require('npmlog');
4const colors = require('colors/safe');
5
6//this is a little weird, it's some old code i found from an unmaintained lib.
7//i took the chunk i needed, a function that converts an object to a valid heroku log string.
8const HerokuLogger = require('./dep-heroku.js');
9const Analytics = require('./analytics/');
10
11logger.prefixStyle = {
12 bg: 'blue',
13 fg: 'white'
14};
15
16const _replace_buffer = (args) => args.map(arg => {
17 const not_a_buffer = (
18 !Buffer.isBuffer(arg) &&
19 !(arg && typeof arg === 'object' && arg.type === 'Buffer')
20 );
21 if (not_a_buffer) {
22 return arg
23 }
24 return 'BUFFER'
25})
26
27//iterates through the error stack,
28//and pulls out the file matching the file of the caller
29const _get_caller_file_line = (type) => {
30 try {
31 let err = new Error();
32 let caller_data;
33 let current_data;
34 let call_stack = [];
35 Error.prepareStackTrace = function (err, stack) { return stack; };
36 let outer_shifted = err.stack.shift();
37 current_data = {
38 file: outer_shifted.getFileName().split(path.resolve(__dirname, '..', '..'))[1],
39 line: outer_shifted.getLineNumber()
40 };
41 let count = 0;
42 while (err.stack.length) {
43 let shifted = err.stack.shift();
44 caller_data = {
45 file: _shorten_file_path(shifted.getFileName()),
46 line: shifted.getLineNumber()
47 };
48 if(current_data.file !== caller_data.file) {
49 if (type === 'string') {
50 return current_data.file + '::' + current_data.line
51 }
52 return caller_data
53 }
54 }
55 } catch (err) {}
56 return undefined;
57};
58
59let remote;
60
61exports.init_remote = (_remote) => {
62 remote = _remote;
63};
64
65exports.process_errors = (on_error) => {
66 process.on('unhandledRejection', (reason, promise) => {
67 on_error(reason);
68 });
69 process.on('uncaughtException', (err) => {
70 on_error(err);
71 });
72};
73
74exports.json = json => logger.info('data', JSON.stringify(json, null, 2));
75
76exports.cli = log => {
77 const caller_data = _get_caller_file_line('string');
78 logger.info(caller_data, log);
79}
80
81exports.color = (color, log) => console.log(colors[color](log_two));
82
83exports.analytics = Analytics;
84
85exports.heroku = {
86 //[priority low, medium, high]
87 exception: (message, priority, optional_payload) => {
88 if (priority !== 'low' && priority !== 'medium' && priority !== 'high') {
89 priority = 'low'
90 }
91 const caller_data = _get_caller_file_line();
92 console.log(HerokuLogger.log('system_exception', Object.assign({}, caller_data, { message, priority }, optional_payload && { payload: JSON.stringify(optional_payload, ...(remote === 'development' ? [null, 2] : [])) } || null)));
93 },
94 basic_info_logger: (message) => {
95 console.log(HerokuLogger.log('basic', { data: message }));
96 },
97 worker_info: (message, obj) => {
98 console.log(HerokuLogger.log('worker:' + process.pid + ' message: ' + message, obj));
99 },
100 worker_succeeded: (domain, method, args) => {
101 args = _replace_buffer(args);
102 console.log(HerokuLogger.log('worker_succeeded', {
103 args: remote !== 'development' ? JSON.stringify(args) : JSON.stringify(args, null, 2),
104 timestamp: Date.now(),
105 domain,
106 method
107 }));
108 },
109 worker_failed: (domain, method, args) => {
110 args = _replace_buffer(args);
111 console.log(HerokuLogger.log('worker_failed', {
112 args: remote !== 'development' ? JSON.stringify(args) : JSON.stringify(args, null, 2),
113 timestamp: Date.now(),
114 domain,
115 method
116 }));
117 },
118 service_events_success_logger: (service, args) => {
119 args = _replace_buffer(args);
120 console.log(HerokuLogger.log('service_succeeded', {
121 timestamp: Date.now(),
122 args: remote !== 'development' ? JSON.stringify(args) : JSON.stringify(args, null, 2),
123 service
124 }));
125 },
126 service_events_error_logger: (service, args) => {
127 args = _replace_buffer(args);
128 console.log(HerokuLogger.log('service_failed', {
129 timestamp: Date.now(),
130 args: remote !== 'development' ? JSON.stringify(args) : JSON.stringify(args, null, 2),
131 service
132 }));
133 },
134};