Skip to content

Instantly share code, notes, and snippets.

@tranquan
Last active July 30, 2018 18:58
Show Gist options
  • Save tranquan/f5c6dd1ae71e9705a39b73b03df384db to your computer and use it in GitHub Desktop.
Save tranquan/f5c6dd1ae71e9705a39b73b03df384db to your computer and use it in GitHub Desktop.
Reactotron Log Configure to connect Console.log
/**
* Copyright (C) SaigonMD, Inc - All Rights Reserved
* Licensed under the MIT license.
* Written by Tran Quan <tranquan221b@gmail.com>, July 2018
*/
/**
* Log helper using Reactotron https://github.com/infinitered/reactotron
* - log faster than console because no need to turn-on remote debug
* - able to customize the message
*/
import DeviceInfo from "react-native-device-info";
import Reactotron from "reactotron-react-native";
namespace LogConfig {
let isLogEnable = false;
/**
* Configure Reactotron and redirect console.log to Reactotron.log
*/
export function configure(options: any = {}) {
isLogEnable = options.enableLog ? options.enableLog : false;
configureReactotron();
connectConsoleToReactotron();
}
function configureReactotron() {
// simulator; host is auto select
if (DeviceInfo.isEmulator()) {
Reactotron.configure({
name: "App",
}).connect();
}
// device; host is copmputer ip address
else {
Reactotron.configure({
name: "App",
host: "127.0.0.1",
}).connect();
}
// clear log on start
Reactotron.clear();
}
function connectConsoleToReactotron() {
console.info = info;
console.log = log;
console.warn = warn;
console.error = error;
}
function log(message: string, ...args: any[]) {
if (!isLogEnable) return;
Reactotron.display({
name: "LOG",
preview: message,
value: { message, args },
});
}
export function info(message: string, ...args: any[]) {
if (!isLogEnable) return;
Reactotron.display({
name: "INFO",
preview: message,
value: { message, args },
});
}
export function warn(message: string, ...args: any[]) {
if (!isLogEnable) return;
Reactotron.display({
name: "WARN",
preview: message,
value: { message, args },
important: true,
});
}
export function error(message: string, ...args: any[]) {
if (!isLogEnable) return;
Reactotron.display({
name: "ERROR",
preview: message,
value: { message, args },
important: true,
});
}
}
export default LogConfig;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment