2017-12-20 21:46:01 +00:00
|
|
|
import chalk from 'chalk';
|
2018-10-16 10:25:37 +00:00
|
|
|
import _ = require('lodash');
|
|
|
|
import { EOL as eol } from 'os';
|
2017-12-20 21:46:01 +00:00
|
|
|
import { StreamLogger } from 'resin-stream-logger';
|
|
|
|
|
2018-01-11 09:22:26 +00:00
|
|
|
class Logger {
|
2017-12-20 21:46:01 +00:00
|
|
|
public streams: {
|
|
|
|
build: NodeJS.ReadWriteStream;
|
|
|
|
info: NodeJS.ReadWriteStream;
|
|
|
|
debug: NodeJS.ReadWriteStream;
|
|
|
|
success: NodeJS.ReadWriteStream;
|
|
|
|
warn: NodeJS.ReadWriteStream;
|
|
|
|
error: NodeJS.ReadWriteStream;
|
2018-10-16 10:24:28 +00:00
|
|
|
logs: NodeJS.ReadWriteStream;
|
2017-12-20 21:46:01 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
public formatMessage: (name: string, message: string) => string;
|
|
|
|
|
|
|
|
constructor() {
|
|
|
|
const logger = new StreamLogger();
|
|
|
|
logger.addPrefix('build', chalk.blue('[Build]'));
|
|
|
|
logger.addPrefix('info', chalk.cyan('[Info]'));
|
|
|
|
logger.addPrefix('debug', chalk.magenta('[Debug]'));
|
|
|
|
logger.addPrefix('success', chalk.green('[Success]'));
|
|
|
|
logger.addPrefix('warn', chalk.yellow('[Warn]'));
|
|
|
|
logger.addPrefix('error', chalk.red('[Error]'));
|
2018-10-16 10:24:28 +00:00
|
|
|
logger.addPrefix('logs', chalk.green('[Logs]'));
|
2017-12-20 21:46:01 +00:00
|
|
|
|
|
|
|
this.streams = {
|
|
|
|
build: logger.createLogStream('build'),
|
|
|
|
info: logger.createLogStream('info'),
|
|
|
|
debug: logger.createLogStream('debug'),
|
|
|
|
success: logger.createLogStream('success'),
|
|
|
|
warn: logger.createLogStream('warn'),
|
2018-01-04 14:07:55 +00:00
|
|
|
error: logger.createLogStream('error'),
|
2018-10-16 10:24:28 +00:00
|
|
|
logs: logger.createLogStream('logs'),
|
2017-12-20 21:46:01 +00:00
|
|
|
};
|
|
|
|
|
2018-01-04 16:17:43 +00:00
|
|
|
_.forEach(this.streams, function(stream, key) {
|
|
|
|
if (key !== 'debug' || process.env.DEBUG) {
|
|
|
|
stream.pipe(process.stdout);
|
2017-12-20 21:46:01 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
this.formatMessage = logger.formatWithPrefix.bind(logger);
|
|
|
|
}
|
|
|
|
|
|
|
|
logInfo(msg: string) {
|
|
|
|
return this.streams.info.write(msg + eol);
|
|
|
|
}
|
|
|
|
|
|
|
|
logDebug(msg: string) {
|
|
|
|
return this.streams.debug.write(msg + eol);
|
|
|
|
}
|
|
|
|
|
|
|
|
logSuccess(msg: string) {
|
|
|
|
return this.streams.success.write(msg + eol);
|
|
|
|
}
|
|
|
|
|
|
|
|
logWarn(msg: string) {
|
|
|
|
return this.streams.warn.write(msg + eol);
|
|
|
|
}
|
|
|
|
|
|
|
|
logError(msg: string) {
|
|
|
|
return this.streams.error.write(msg + eol);
|
|
|
|
}
|
2018-10-15 11:32:27 +00:00
|
|
|
|
|
|
|
logBuild(msg: string) {
|
|
|
|
return this.streams.build.write(msg + eol);
|
|
|
|
}
|
2018-10-16 10:24:28 +00:00
|
|
|
|
|
|
|
logLogs(msg: string) {
|
|
|
|
return this.streams.logs.write(msg + eol);
|
|
|
|
}
|
2017-12-20 21:46:01 +00:00
|
|
|
}
|
2018-01-11 09:22:26 +00:00
|
|
|
|
|
|
|
export = Logger;
|