-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
53 lines (46 loc) · 1.37 KB
/
index.js
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
/**
* Copyright (c) 2018, HouseRater LLC.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @providesModule react-native-lumberjack
*/
import React from "react";
import { NativeModules } from "react-native";
import { format } from "util";
const RNLumberjack = NativeModules.RNLumberjackManager;
function stackToString (e) {
let s = e.stack, ce;
if (typeof e.cause === 'function' && (ce = e.cause()))
s += '\nCaused by: ' + stackToString(ce);
return s
}
function LoggingBuilder(loggerName) {
return [
"verbose",
"debug",
"info",
"warn",
"error"
].reduce((logger, logName) => {
logger[logName] = (...args) => {
args = args.map((arg) => {
if (arg instanceof Error) {
const errorBody = {
message: arg.message,
code: arg.code,
stack: stackToString(arg)
};
return `Error(${arg.name})${JSON.stringify(errorBody)}`
}
return arg;
});
const message = format(...args);
RNLumberjack[logName](message, loggerName);
};
return logger;
}, {})
}
module.exports = LoggingBuilder;