Skip to content

Commit e45a162

Browse files
author
jyoes
committed
Add trackGlobalLogs plugin
1 parent 5ca01fa commit e45a162

3 files changed

Lines changed: 45 additions & 0 deletions

File tree

lib/reactotron-react-native/src/plugins/trackGlobalErrors.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,9 @@ const objectifyError = (error: Error) => {
5757

5858
// const reactNativeFrameFinder = frame => contains('/node_modules/react-native/', frame.fileName)
5959

60+
/**
61+
* Track global errors and send them to Reactotron logger.
62+
*/
6063
const trackGlobalErrors = (options?: TrackGlobalErrorsOptions) => (reactotron: ReactotronCore) => {
6164
// make sure we have the logger plugin
6265
assertHasLoggerPlugin(reactotron)
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import {
2+
InferFeatures,
3+
LoggerPlugin,
4+
ReactotronCore,
5+
assertHasLoggerPlugin,
6+
Plugin,
7+
} from "reactotron-core-client"
8+
9+
/**
10+
* Track calls to console.log, console.warn, and console.debug and send them to Reactotron logger
11+
*/
12+
const trackGlobalLogs = () => (reactotron: ReactotronCore) => {
13+
assertHasLoggerPlugin(reactotron)
14+
const client = reactotron as ReactotronCore & InferFeatures<ReactotronCore, LoggerPlugin>
15+
16+
return {
17+
onConnect: () => {
18+
const originalConsoleLog = console.log
19+
console.log = (...args: Parameters<typeof console.log>) => {
20+
originalConsoleLog(...args)
21+
client.log(...args)
22+
}
23+
24+
const originalConsoleWarn = console.warn
25+
console.warn = (...args: Parameters<typeof console.warn>) => {
26+
originalConsoleWarn(...args)
27+
client.warn(args[0])
28+
}
29+
30+
const originalConsoleDebug = console.debug
31+
console.debug = (...args: Parameters<typeof console.debug>) => {
32+
originalConsoleDebug(...args)
33+
client.debug(args[0])
34+
}
35+
36+
// console.error is taken care of by ./trackGlobalErrors.ts
37+
},
38+
} satisfies Plugin<ReactotronCore>
39+
}
40+
41+
export default trackGlobalLogs

lib/reactotron-react-native/src/reactotron-react-native.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,7 @@ reactotron.setAsyncStorageHandler = (asyncStorage: AsyncStorageStatic) => {
143143
}
144144

145145
export { asyncStorage, trackGlobalErrors, openInEditor, overlay, networking, storybook, devTools }
146+
export { default as trackGlobalLogs } from "./plugins/trackGlobalLogs"
146147

147148
export type { ClientOptions }
148149

0 commit comments

Comments
 (0)