webpack-auto-inject-version/src/core/log.js

71 lines
1.3 KiB
JavaScript
Raw Normal View History

2017-04-11 06:33:32 +10:00
import chalk from 'chalk';
2018-03-15 22:07:22 +11:00
import config from 'config';
2017-04-11 09:11:09 +10:00
import { isArgv } from 'core/utils';
2018-03-15 22:07:22 +11:00
2017-04-11 06:33:32 +10:00
const endOfLine = require('os').EOL;
2017-04-13 07:32:39 +10:00
class Log {
2017-04-11 06:33:32 +10:00
logLevel = 3; // default 1
constructor() {
this.getLogLevel();
}
getLogLevel() {
2017-04-13 07:32:39 +10:00
if (isArgv('aiv-log-full')) {
2017-04-11 06:33:32 +10:00
this.logLevel = 3;
2017-04-13 07:32:39 +10:00
} else if (isArgv('aiv-log-none')) {
2017-04-11 06:33:32 +10:00
this.logLevel = 0;
}
}
/**
* Get console log head
2018-03-15 22:07:22 +11:00
* @return {string}
2017-04-11 06:33:32 +10:00
*/
getHead() {
2017-04-13 07:32:39 +10:00
return endOfLine + chalk.bgYellow.black('[AIV] : ');
2017-04-11 06:33:32 +10:00
}
/**
* Get log text by ID from config file
2018-03-15 22:07:22 +11:00
* @param id
2017-04-11 06:33:32 +10:00
*/
getText(id) {
return config.LOGS_TEXT[id];
}
/**
* Call any type
* @param type
2018-03-15 22:07:22 +11:00
* @param msgId
2017-04-11 06:33:32 +10:00
*/
call(type, msgId) {
2017-04-13 07:32:39 +10:00
if (typeof this[type] === 'function') {
2017-04-11 06:33:32 +10:00
this[type](this.getText(msgId));
}
}
2017-04-13 07:32:39 +10:00
error(msg) {
if (config.SILENT) return;
2017-04-13 07:32:39 +10:00
if (this.logLevel < 3) return;
2017-04-11 06:33:32 +10:00
console.log(`${this.getHead()} ${chalk.red('error')} : ${msg}`);
}
2017-04-13 07:32:39 +10:00
info(msg) {
if (config.SILENT) return;
2017-04-13 07:32:39 +10:00
if (!this.logLevel) return;
2017-04-11 06:33:32 +10:00
console.log(`${this.getHead()} ${chalk.blue('info')} : ${msg}`);
}
2017-04-13 07:32:39 +10:00
warn(msg) {
if (config.SILENT) return;
2017-04-13 07:32:39 +10:00
if (!this.logLevel) return;
2017-04-11 06:33:32 +10:00
console.log(`${this.getHead()} ${chalk.yellow('warn')} : ${msg}`);
}
}
2017-04-13 07:32:39 +10:00
export default new Log();