Skip to content

Instantly share code, notes, and snippets.

@emeraldsanto
Last active August 4, 2020 02:01
Show Gist options
  • Save emeraldsanto/819b64319cbbb1e96b7fd12ba05715f7 to your computer and use it in GitHub Desktop.
Save emeraldsanto/819b64319cbbb1e96b7fd12ba05715f7 to your computer and use it in GitHub Desktop.
Simple logger with custom tags and colors.
import crashlytics from "@react-native-firebase/crashlytics";
export type LogLevel = "info" | "warn" | "error";
const ANSI_YELLOW = "\u001b[1;33m";
const ANSI_RED = "\u001b[1;31m";
const ANSI_BLUE = "\u001b[34m";
const ANSI_RESET_CODE = "\u001b[30m";
const LOG_COLORS: Record<LogLevel, string> = {
info: ANSI_BLUE,
warn: ANSI_YELLOW,
error: ANSI_RED
}
/**
* Logs data to the console only when in DEV mode using tags and colors.
* Logs to Crashlytics in production.
* @param {string} tag - An identifier representing the origin of the log.
* @param {string} message - The data to log.
* @param {"info" | "warn" | "error"} [level] - The level (will affect log color).
*/
export function log(tag: string, message: string, level: "info" | "warn" | "error" = "info") {
if (!__DEV__) {
crashlytics().log(`${level}: [${tag}] - ${message}`);
return;
}
const color = LOG_COLORS[level] || ANSI_BLUE;
console.log(`[${tag}] - ${color}${message}`, ANSI_RESET_CODE);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment