├── .editorconfig ├── .gitattributes ├── .gitignore ├── .travis.yml ├── CHANGELOG.md ├── LICENSE ├── README.md ├── example.png ├── index.js ├── package.json ├── test.js └── yarn.lock /.editorconfig: -------------------------------------------------------------------------------- 1 | root = true 2 | 3 | [*] 4 | indent_style = space 5 | indent_size = 2 6 | end_of_line = lf 7 | charset = utf-8 8 | trim_trailing_whitespace = true 9 | insert_final_newline = true 10 | 11 | [*.md] 12 | trim_trailing_whitespace = false -------------------------------------------------------------------------------- /.gitattributes: -------------------------------------------------------------------------------- 1 | * text=auto 2 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | coverage -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: node_js 2 | node_js: 3 | - 'node' 4 | install: 5 | - yarn 6 | - npm install -g codecov 7 | script: 8 | - yarn test:cov && codecov -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | 2 | ## v1.0.1 3 | > Thu, 19 Oct 2017 21:45:25 GMT 4 | -------------------------------------------------------------------------------- 5 | 6 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) tiaanduplessis (https://github.com/tiaanduplessis) 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in 13 | all copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | THE SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | 2 |

3 | 4 |

5 |

react-native-log-level

6 |
7 | Multi level logger for React Native 8 |
9 |
10 |
11 | 12 | npm package version 13 | 14 | 15 | npm downloads 16 | 17 | 18 | standard JS linter 19 | 20 | 21 | prettier code formatting 22 | 23 | 24 | travis ci build status 25 | 26 | 27 | Codecov 28 | 29 | 30 | Greenkeeper 31 | 32 | 33 | project license 34 | 35 | 36 | make a pull request 37 | 38 |
39 |
40 |
41 | 42 | Github Watch Badge 43 | 44 | 45 | Github Star Badge 46 | 47 | 48 | Tweet 49 | 50 |
51 |
52 |
53 | Built with ❤︎ by tiaanduplessis and contributors 54 |
55 | 56 |

Table of Contents

57 |
58 | Table of Contents 59 |
  • Features
  • 60 |
  • Install
  • 61 |
  • Usage
  • 62 |
  • Contribute
  • 63 |
  • License
  • 64 |
    65 | 66 | ## Features 67 | 68 | - Includes 5 different log levels (`trace`, `debug`, `info`, `warn` & `error`) 69 | - Colorized output 70 | - No external dependencies 71 | - Returns the value provided for cleaner logging 72 | - Only logs during development 73 | 74 | ## Install 75 | 76 | ```sh 77 | $ npm install react-native-log-level 78 | # OR 79 | $ yarn add react-native-log-level 80 | ``` 81 | 82 | ## Usage 83 | 84 | ```js 85 | import createLogger from 'react-native-log-level' 86 | 87 | // Create a instance 88 | const log = createLogger({ 89 | level: 'info' // Optionally set the log level. Defaults to 'info' 90 | }) 91 | 92 | const obj = { 93 | foo: 1, 94 | bar: 2 95 | } 96 | 97 | log.trace('hi') 98 | log.debug('hi') 99 | log.info('hi', obj) 100 | log.warn('hi', obj) 101 | log.error('hi') 102 | ``` 103 | 104 | Since the value logged is returned, cleaner logging is possible 105 | 106 | ```js 107 | 108 | if (log.info(it === anotherThing)) { 109 | // Do Stuff 110 | } 111 | 112 | ``` 113 | 114 | ## Contributing 115 | 116 | Contributions are welcome! 117 | 118 | 1. Fork it. 119 | 2. Create your feature branch: `git checkout -b my-new-feature` 120 | 3. Commit your changes: `git commit -am 'Add some feature'` 121 | 4. Push to the branch: `git push origin my-new-feature` 122 | 5. Submit a pull request :D 123 | 124 | Or open up [a issue](https://github.com/tiaanduplessis/react-native-log-level/issues). 125 | 126 | ## License 127 | 128 | Licensed under the MIT License. 129 | -------------------------------------------------------------------------------- /example.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tiaanduplessis/react-native-log-level/c47a4456b02c1f96f9404f69b04ef5842fec2372/example.png -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | const pad = time => time.padStart(2, '0') 2 | 3 | const getTimeStamp = () => { 4 | const date = new Date() 5 | const hours = pad(date.getHours().toString()) 6 | const minutes = pad(date.getMinutes().toString()) 7 | const secs = pad(date.getSeconds().toString()) 8 | 9 | return `${hours}:${minutes}:${secs}` 10 | } 11 | 12 | const echo = (message, data) => data || message 13 | 14 | const createLevel = (priority, color) => ({ 15 | priority, 16 | color 17 | }) 18 | 19 | const levels = { 20 | trace: createLevel(10, '#6699cc'), 21 | debug: createLevel(20, '#66cccc'), 22 | info: createLevel(30, '#99cc99'), 23 | warn: createLevel(40, '#ffcc66'), 24 | error: createLevel(50, '#f2777a') 25 | } 26 | 27 | const createLogger = ({ level = 'info', dev = !!__DEV__ } = {}) => { 28 | if (!levels[level]) throw Error('Invalid log level set') 29 | 30 | const log = level => { 31 | const { color } = levels[level] 32 | const css = `color:#fff;font-weight:bold;background-color:${color};padding:3px;` 33 | 34 | return (message = '', data = '') => { 35 | const messageIsString = typeof message === 'string' 36 | const dataOutput = !messageIsString ? message : data 37 | const messageOutput = !messageIsString ? '' : message 38 | 39 | const levelStringFormatted = level.toUpperCase().padEnd(6) 40 | const timeStampValue = getTimeStamp() 41 | 42 | const output = `%c${timeStampValue} ${levelStringFormatted}%c ${messageOutput}` 43 | 44 | console.log(output, css, 'color:inherit;', dataOutput) 45 | 46 | return dataOutput || messageOutput 47 | } 48 | } 49 | 50 | const getAllTypeOfLogs = (obj, current) => { 51 | const shouldLog = levels[current].priority >= levels[level].priority && dev 52 | 53 | const shouldLogResponse = shouldLog ? log(current) : echo 54 | 55 | return { ...obj, [current]: shouldLogResponse } 56 | } 57 | 58 | const logger = Object.keys(levels).reduce(getAllTypeOfLogs, {}) 59 | 60 | return logger 61 | } 62 | 63 | export default createLogger 64 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "react-native-log-level", 3 | "version": "1.2.0", 4 | "description": "Multi level logger for React Native", 5 | "license": "MIT", 6 | "repository": { 7 | "url": "https://github.com/tiaanduplessis/react-native-log-level", 8 | "type": "git" 9 | }, 10 | "homepage": "https://github.com/tiaanduplessis/react-native-log-level", 11 | "bugs": "https://github.com/tiaanduplessis/react-native-log-level", 12 | "main": "index.js", 13 | "files": [ 14 | "index.js", 15 | "test.js" 16 | ], 17 | "scripts": { 18 | "test": "jest", 19 | "test:cov": "npm run test -- --coverage", 20 | "format": "prettier-standard index.js", 21 | "start": "npm run test", 22 | "precommit": "npm test", 23 | "pretest": "npm run format" 24 | }, 25 | "author": "tiaanduplessis ", 26 | "devDependencies": { 27 | "consolemock": "^1.1.0", 28 | "husky": "^3.1.0", 29 | "jest": "^24.9.0", 30 | "metro-react-native-babel-preset": "^0.57.0", 31 | "prettier-standard": "^16.0.0" 32 | }, 33 | "babel": { 34 | "presets": [ 35 | "module:metro-react-native-babel-preset" 36 | ] 37 | }, 38 | "jest": { 39 | "globals": { 40 | "__DEV__": true 41 | } 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /test.js: -------------------------------------------------------------------------------- 1 | import createLogger from './' 2 | import makeConsoleMock from 'consolemock' 3 | 4 | global.console = makeConsoleMock() 5 | 6 | function logMultiple(log, message = 'none') { 7 | log.trace(message) 8 | log.debug(message) 9 | log.info(message) 10 | log.warn(message) 11 | log.error(message) 12 | } 13 | 14 | test('should be defined', () => { 15 | expect(createLogger).toBeDefined() 16 | }) 17 | 18 | test('should return new instance', () => { 19 | const log = createLogger() 20 | expect(log).toBeDefined() 21 | expect(log.info).toBeDefined() 22 | expect(log.error).toBeDefined() 23 | }) 24 | 25 | test('should log to console', () => { 26 | console.clearHistory() 27 | const log = createLogger({ level: 'trace' }) 28 | logMultiple(log) 29 | expect(console.history().length).toBe(5) 30 | }) 31 | 32 | test('should log based on level', () => { 33 | console.clearHistory() 34 | const log = createLogger({ level: 'warn' }) 35 | logMultiple(log) 36 | expect(console.history().length).toBe(2) 37 | }) 38 | --------------------------------------------------------------------------------