Skip to content

Instantly share code, notes, and snippets.

@satya164
Created February 29, 2016 21:22
Show Gist options
  • Save satya164/264423e10b606a92c163 to your computer and use it in GitHub Desktop.
Save satya164/264423e10b606a92c163 to your computer and use it in GitHub Desktop.
/* @flow */
/* eslint-disable no-console */
let _types: ?Array<string>,
_filters: ?Array<string | RegExp>,
_history: ?Array<{ timestamp: number; args: Array<any>; }>;
const labels = {
info: '[info]',
warn: '[warn]',
success: '[succ]',
error: '[erro]',
sent: ' <--- ',
received: ' ---> ',
};
const colors = {
info: '#2196f3',
warn: '#ff9419',
success: '#4caf50',
error: '#f44336',
sent: '#2196f3',
received: '#4caf50',
};
export function pretty(type: string, ...rest: Array<any>) {
const label = `%c${labels[type]}`;
const style = `color: ${colors[type]}; font-weight: bold;`;
if (type === '[erro]') {
console.error(label, style, ...rest);
} else {
console.log(label, style, ...rest);
}
}
export function print(tag: string, type: string, message: string, ...rest: Array<any>): { store: () => void } {
let show = false;
if (_filters) {
for (let i = 0, l = _filters.length; i < l; i++) {
const f = _filters[i];
if (typeof f === 'string' && f === tag || f instanceof RegExp && f.test(message)) {
show = true;
break;
}
}
} else {
show = true;
}
if (show === true) {
if (_types) {
show = _types.indexOf(type) > -1;
} else {
show = true;
}
if (show) {
pretty(type, message, ...rest);
}
}
return {
store() {
if (process.env.NODE_ENV !== 'production') {
_history = _history || [];
_history.push({
timestamp: Date.now(),
args: [ tag, type, message, ...rest ]
});
}
}
};
}
export function info(tag: string, ...rest: Array<any>) {
print(tag, 'info', ...rest).store();
}
export function success(tag: string, ...rest: Array<any>) {
print(tag, 'success', ...rest).store();
}
export function warn(tag: string, ...rest: Array<any>) {
print(tag, 'warn', ...rest).store();
}
export function error(tag: string, ...rest: Array<any>) {
print(tag, 'error', ...rest).store();
}
export function sent(tag: string, ...rest: Array<any>) {
print(tag, 'sent', ...rest).store();
}
export function received(tag: string, ...rest: Array<any>) {
print(tag, 'received', ...rest).store();
}
export function log(tag: string, message: string, ...rest: Array<any>) {
if (/success/.test(message)) {
success(tag, message, ...rest);
} else if (/warn/.test(message)) {
warn(tag, message, ...rest);
} else if (/err/.test(message)) {
error(tag, message, ...rest);
} else if (/get|receive/.test(message)) {
sent(tag, message, ...rest);
} else if (/sent|send/.test(message)) {
received(tag, message, ...rest);
} else {
let e = false;
for (let i = 0, l = rest.length; i < l; i++) {
if (rest[i] && rest[i] instanceof Error) {
e = true;
break;
}
}
if (e) {
error(tag, message, ...rest);
} else {
info(tag, message, ...rest);
}
}
}
export function dump(start: number, end: number) {
if (process.env.NODE_ENV !== 'production') {
const history = Array.isArray(_history) ? _history : [];
for (let i = 0, l = history.length; i < l; i++) {
const {
args,
timestamp
} = history[i];
if (start && end) {
if (timestamp > start && timestamp < end) {
print(...args);
}
} else if (start) {
if (timestamp > start) {
print(...args);
}
} else if (end) {
if (timestamp < end) {
print(...args);
}
} else {
print(...args);
}
}
return;
}
warn('logger', 'logger.dump() is only available when \'process.env.NODE_ENV !== \'production');
}
export function filter(filters: ?Array<string | RegExp> | string | RegExp, types: ?Array<string> | string) {
if (Array.isArray(filters)) {
_filters = filters;
} else if (typeof filters === 'string' || filters instanceof RegExp) {
_filters = [ filters ];
} else {
_filters = null;
}
if (Array.isArray(types)) {
_types = types;
} else if (typeof types === 'string') {
_types = [ types ];
} else {
_types = null;
}
}
export function clean() {
_history = null;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment