Skip to content

Commit

Permalink
feat: Add log utils
Browse files Browse the repository at this point in the history
  • Loading branch information
EslamElMeniawy committed Feb 10, 2022
1 parent a67a43b commit 023ce5d
Show file tree
Hide file tree
Showing 4 changed files with 349 additions and 6 deletions.
2 changes: 2 additions & 0 deletions package.json
Expand Up @@ -71,6 +71,7 @@
"react-native-paper": "^4.11.2",
"react-native-safe-area-context": "^3.3.2",
"react-native-vector-icons": "^9.0.0",
"reactotron-react-native": "^5.0.1",
"release-it": "^14.2.2",
"typescript": "^4.1.3"
},
Expand Down Expand Up @@ -160,6 +161,7 @@
},
"optionalDependencies": {
"@klarna/react-native-vector-drawable": "^0.3.0",
"@react-native-firebase/crashlytics": "^14.3.1",
"react-native-fast-image": "^8.5.11",
"react-native-keyboard-aware-scroll-view": "^0.9.5",
"react-native-progress": "^5.0.0",
Expand Down
2 changes: 2 additions & 0 deletions src/index.tsx
Expand Up @@ -23,6 +23,7 @@ import {
isIPhoneWithMonobrow,
getStatusBarHeight,
} from './utils/StatusBarHeight';
import { configureLog } from './utils/LogConfig';

// Types import.
import type FlatListItem from './types/FlatListItem';
Expand Down Expand Up @@ -54,6 +55,7 @@ export {
isIPhone12Max,
isIPhoneWithMonobrow,
getStatusBarHeight,
configureLog,
};

// Types export.
Expand Down
124 changes: 124 additions & 0 deletions src/utils/LogConfig.tsx
@@ -0,0 +1,124 @@
// External imports.
import Reactotron from 'reactotron-react-native';
import crashlytics from '@react-native-firebase/crashlytics';
import { NativeModules } from 'react-native';

// #region Types
type LogLevel = 'INFO' | 'LOG' | 'WARN' | 'ERROR';

type Options = {
appName?: string;
firebaseLogLevels?: LogLevel[];
isLocalLogEnable?: boolean;
};
// #endregion

let firebaseLogLevels: LogLevel[] = [];
let isLocalLogEnable: boolean = false;

const configureReactotron = (appName?: string): void => {
const { scriptURL } = NativeModules.SourceCode;
const scriptHostname = scriptURL.split('://')[1].split(':')[0];

Reactotron.configure({
name: appName,
host: scriptHostname,
})
.useReactNative()
.connect();

// Clear log on start.
if (Reactotron.clear) {
Reactotron.clear();
}
};

const info = (message: string, ...args: any[]): void => {
const tag = 'INFO';

if (firebaseLogLevels.includes(tag)) {
crashlytics().log(
`## ${tag} ## Message: ${message} ## Data: ${JSON.stringify(args)}`
);
}

if (isLocalLogEnable) {
Reactotron.display({
name: tag,
preview: message,
value: { message, args },
});
}
};

const log = (message: string, ...args: any[]): void => {
const tag = 'LOG';

if (firebaseLogLevels.includes(tag)) {
crashlytics().log(
`## ${tag} ## Message: ${message} ## Data: ${JSON.stringify(args)}`
);
}

if (isLocalLogEnable) {
Reactotron.display({
name: tag,
preview: message,
value: { message, args },
});
}
};

const warn = (message: string, ...args: any[]): void => {
const tag = 'WARN';

if (firebaseLogLevels.includes(tag)) {
crashlytics().log(
`## ${tag} ## Message: ${message} ## Data: ${JSON.stringify(args)}`
);
}

if (isLocalLogEnable) {
Reactotron.display({
name: tag,
preview: message,
value: { message, args },
important: true,
});
}
};

const error = (message: string, ...args: any[]): void => {
const tag = 'ERROR';

if (firebaseLogLevels.includes(tag)) {
crashlytics().recordError(
new Error(
`## ${tag} ## Message: ${message} ## Data: ${JSON.stringify(args)}`
)
);
}

if (isLocalLogEnable) {
Reactotron.display({
name: tag,
preview: message,
value: { message, args },
important: true,
});
}
};

const connectConsoleToReactotron = (): void => {
console.info = info;
console.log = log;
console.warn = warn;
console.error = error;
};

export const configureLog = (options?: Options): void => {
firebaseLogLevels = options?.firebaseLogLevels || [];
isLocalLogEnable = options?.isLocalLogEnable || false;
configureReactotron(options?.appName);
connectConsoleToReactotron();
};

0 comments on commit 023ce5d

Please sign in to comment.