├── README.md ├── index.js └── package.json /README.md: -------------------------------------------------------------------------------- 1 | # React-Native-Logger 2 | 3 | Sync or Async Aware logger for react-native 4 | 5 | This Project address couple of issues 6 | 7 | - Simple to use 8 | - Group logs 9 | - Filter Logs by name 10 | - Async aware 11 | 12 | ### Installation (NPM or Yarn) 13 | 14 | ```bash 15 | npm install react-native-logger --save 16 | ``` 17 | 18 | ```bash 19 | yarn add react-native-logger 20 | ``` 21 | 22 | ### Usage 23 | 24 | ```js 25 | import { logger } from 'react-native-logger' 26 | ``` 27 | 28 | There are 3 functions. 29 | 30 | #### log: (...args: Array) => void 31 | 32 | similar to `console.log`. for example 33 | 34 | ``` js 35 | logger.log('this is awesome', 10, 10) 36 | 37 | // logs as 38 | // this is awesome 10 10 39 | ``` 40 | 41 | #### group: (name: string, title: string, log: LogFn) => void 42 | 43 | it accepts 3 arguments. `name` is an identifier which you can use in `setFilters`. `title` is a simple description about the log it self. and `log` is a function which your loggin happening. The signiture of LogFn is 44 | 45 | ```js 46 | type LogFn: (log: (...args: Array) => void) => void | Promise 47 | ``` 48 | 49 | It basically group related logs into one. also you can return a promise which cause the logs waits until you tell it so. 50 | 51 | ```js 52 | logger.group('api', 'loads users list', async (log) => { 53 | 54 | const users = await apis.getAllUsers(); 55 | 56 | log('list of users', users); 57 | 58 | actions.updateUsersList(users); 59 | }); 60 | ``` 61 | 62 | #### setFilters: (...names: Array): void 63 | 64 | simply filters out everything except those names that you pass to logs. to clear the filter simply call it without arguments. 65 | 66 | ```js 67 | logger.filters('api') 68 | ``` 69 | 70 | cheers, 71 | 72 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | // @flow 2 | 3 | type groupLogFn = (...args: Array) => void 4 | 5 | const isDebugEnabled = global && typeof global.__DEV__ !== 'undefined' && !!global.__DEV__ 6 | 7 | const noop = () => {} 8 | const displayLogs = (name: string, title: string, logs: Array>, show: boolean) => { 9 | if (!show) { 10 | return 11 | } 12 | 13 | if (console.groupCollapsed) { 14 | console.groupCollapsed(`${name}: ${title}`) 15 | } 16 | 17 | for (var i = 0; i < logs.length; i++) { 18 | console.log(...logs[i]) 19 | } 20 | 21 | if (console.groupEnd){ 22 | console.groupEnd() 23 | } 24 | } 25 | 26 | let filters: Array = [] 27 | 28 | export const logger = { 29 | setFilters: (...names: Array): void => { 30 | filters = names 31 | }, 32 | log: (...args: Array): void => { 33 | isDebugEnabled && console.log(...args) 34 | }, 35 | group: (name: string, title: string, logFn: (log: groupLogFn) => void | Promise = noop) => { 36 | const show = filters.length === 0 || filters.indexOf(name) !== -1 37 | 38 | const logs: Array = [] 39 | const done = logFn((...args: Array): void => { 40 | if (isDebugEnabled && show) { 41 | logs.push(args) 42 | } 43 | }) 44 | 45 | if (done instanceof Promise) { 46 | done.then(() => { 47 | displayLogs(name, title, logs, show) 48 | }).catch(() => { 49 | displayLogs(name, title, logs, show) 50 | }) 51 | } else { 52 | displayLogs(name, title, logs, show) 53 | } 54 | } 55 | } 56 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "react-native-logger", 3 | "version": "1.0.3", 4 | "description": "fast and simple logger for iOS and Android", 5 | "main": "index.js", 6 | "scripts": { 7 | "test": "echo \"Error: no test specified\" && exit 1" 8 | }, 9 | "repository": { 10 | "type": "git", 11 | "url": "https://github.com/alinz/react-native-logger.git" 12 | }, 13 | "keywords": [ 14 | "react-component", 15 | "react-native", 16 | "logger" 17 | ], 18 | "author": { 19 | "name": "Ali Najafizadeh", 20 | "email": "a.najafizadeh@gmail.com", 21 | "url": "https://github.com/alinz" 22 | }, 23 | "license": "MIT", 24 | "bugs": { 25 | "url": "https://github.com/alinz/react-native-logger/issues" 26 | }, 27 | "homepage": "https://github.com/alinz/react-native-logger" 28 | } 29 | --------------------------------------------------------------------------------