airdcpp-apisocket
Version:
Javascript connector for AirDC++ Web API
69 lines • 2.48 kB
JavaScript
import chalk from 'chalk';
// @ts-ignore
import { isBrowser, isJsDom } from 'is-in-browser';
import invariant from 'invariant';
export const LOG_NONE = 'none';
export const LOG_ERROR = 'error';
export const LOG_WARN = 'warn';
export const LOG_INFO = 'info';
export const LOG_VERBOSE = 'verbose';
const Severities = {
[LOG_NONE]: -1,
[LOG_ERROR]: 0,
[LOG_WARN]: 1,
[LOG_INFO]: 2,
[LOG_VERBOSE]: 3,
};
// Should we format the line with timestamp and coloring or let the logger implementation to handle it?
// Do this when running in terminal (node.js/tests in browser env)
const shouldFormatLine = isJsDom || !isBrowser;
const Logger = ({ logLevel: logSetting = LOG_VERBOSE, logOutput = console }) => {
const logLevel = Severities[logSetting];
invariant(
// @ts-ignore: This condition will always return true since the function is always defined
logOutput.log && logOutput.info && logOutput.warn && logOutput.error, 'Invalid logOutput provided');
const formatCurrentTime = () => {
const d = new Date();
return `[${d.toLocaleDateString()} ${d.toLocaleTimeString()}:${d.getMilliseconds()}]`;
};
const print = (args, printHandler, argFormat) => {
let printableArgs = [...Array.prototype.slice.call(args)];
if (shouldFormatLine && argFormat) {
// Add the current time as well
printableArgs = [
chalk.magenta(formatCurrentTime()),
...printableArgs.map(arg => argFormat(typeof arg === 'object' ? JSON.stringify(arg, null, ' ') : arg)),
];
}
printHandler.apply(logOutput, printableArgs);
};
const Impl = {
verbose() {
if (logLevel < Severities[LOG_VERBOSE]) {
return;
}
print(arguments, logOutput.log, chalk.gray);
},
info() {
if (logLevel < Severities[LOG_INFO]) {
return;
}
print(arguments, logOutput.info, chalk.white.bold);
},
warn() {
if (logLevel < Severities[LOG_WARN]) {
return;
}
print(arguments, logOutput.warn, chalk.yellow.bold);
},
error() {
if (logLevel < Severities[LOG_ERROR]) {
return;
}
print(arguments, logOutput.error, chalk.red.bold);
},
};
return Impl;
};
export default Logger;
//# sourceMappingURL=SocketLogger.js.map