Created
July 26, 2019 13:49
-
-
Save titoasty/13b07216103484466898043c953ccc37 to your computer and use it in GitHub Desktop.
let's colorize react-native log-ios / log-android
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const spawn = require('child_process').spawn; | |
const chalk = require('chalk'); | |
const endOfLine = require('os').EOL; | |
const deviceType = process.argv[2]; | |
const cmd = spawn('react-native', [deviceType == 'ios' ? 'log-ios' : 'log-android']); | |
function log(msg) { | |
console.log(msg); | |
} | |
cmd.stdout.on('data', function(data) { | |
msgs = data.toString(); | |
msgs.split(endOfLine).forEach(function(msg) { | |
if (msg.length <= 0) return; | |
const pos = msg.indexOf(':', msg.indexOf('React')); | |
const header = msg.substr(0, pos + 1); | |
const headerData = header.split(/\s+/); | |
const level = headerData[4]; | |
const bodyStr = msg.substr(pos + 1); | |
const headerStr = headerData.join(' '); | |
switch (level) { | |
case 'V': // Verbose (lowest priority) | |
log(chalk.green(headerStr) + bodyStr); | |
break; | |
case 'D': // Debug | |
log(chalk.gray(headerStr + bodyStr)); | |
break; | |
case 'I': // Info (default priority) | |
log(chalk.blue(headerStr) + bodyStr); | |
break; | |
case 'W': // Warning | |
log(chalk.yellow(headerStr + bodyStr)); | |
break; | |
case 'E': // Error | |
log(chalk.red(headerStr + bodyStr)); | |
break; | |
case 'F': // Fatal | |
log(chalk.red.bold(headerStr + bodyStr)); | |
break; | |
case 'S': // Silent | |
log(chalk.gray(headerStr) + bodyStr); | |
break; | |
default: | |
log(chalk.gray(headerStr) + bodyStr); | |
} | |
}); | |
}); | |
cmd.stderr.on('data', function(data) { | |
console.log('stderr: ' + data.toString()); | |
}); | |
cmd.on('exit', function(code) { | |
console.log('child process exited with code ' + code.toString()); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment