2019-03-12 22:07:57 +00:00
|
|
|
/**
|
|
|
|
* @license
|
|
|
|
* Copyright 2019 Balena Ltd.
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
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';
|
|
|
|
|
2019-09-11 18:34:43 +00:00
|
|
|
/**
|
|
|
|
* General purpose logger class with support for log streams and colours.
|
|
|
|
* Call `Logger.getLogger()` to retrieve a global shared instance of this
|
|
|
|
* class. The `new Logger()` pattern is not recommended because it may lead
|
|
|
|
* to Node printing "MaxListenersExceededWarning" warning messages to the
|
|
|
|
* console.
|
|
|
|
*/
|
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;
|
2019-01-14 16:46:36 +00:00
|
|
|
livepush: NodeJS.ReadWriteStream;
|
2017-12-20 21:46:01 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
public formatMessage: (name: string, message: string) => string;
|
|
|
|
|
2019-09-11 18:34:43 +00:00
|
|
|
protected constructor() {
|
2017-12-20 21:46:01 +00:00
|
|
|
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]'));
|
2019-01-14 16:46:36 +00:00
|
|
|
logger.addPrefix('live', chalk.yellow('[Live]'));
|
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'),
|
2019-01-14 16:46:36 +00:00
|
|
|
livepush: logger.createLogStream('live'),
|
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);
|
|
|
|
}
|
|
|
|
|
2019-09-11 18:34:43 +00:00
|
|
|
protected static logger: Logger;
|
|
|
|
|
|
|
|
/** Retrieve a global shared instance of this class */
|
|
|
|
public static getLogger() {
|
|
|
|
if (!this.logger) {
|
|
|
|
this.logger = new Logger();
|
|
|
|
}
|
|
|
|
return this.logger;
|
|
|
|
}
|
|
|
|
|
2019-03-12 22:07:57 +00:00
|
|
|
public logInfo(msg: string) {
|
2017-12-20 21:46:01 +00:00
|
|
|
return this.streams.info.write(msg + eol);
|
|
|
|
}
|
|
|
|
|
2019-03-12 22:07:57 +00:00
|
|
|
public logDebug(msg: string) {
|
2017-12-20 21:46:01 +00:00
|
|
|
return this.streams.debug.write(msg + eol);
|
|
|
|
}
|
|
|
|
|
2019-03-12 22:07:57 +00:00
|
|
|
public logSuccess(msg: string) {
|
2017-12-20 21:46:01 +00:00
|
|
|
return this.streams.success.write(msg + eol);
|
|
|
|
}
|
|
|
|
|
2019-03-12 22:07:57 +00:00
|
|
|
public logWarn(msg: string) {
|
2017-12-20 21:46:01 +00:00
|
|
|
return this.streams.warn.write(msg + eol);
|
|
|
|
}
|
|
|
|
|
2019-03-12 22:07:57 +00:00
|
|
|
public logError(msg: string) {
|
2017-12-20 21:46:01 +00:00
|
|
|
return this.streams.error.write(msg + eol);
|
|
|
|
}
|
2018-10-15 11:32:27 +00:00
|
|
|
|
2019-03-12 22:07:57 +00:00
|
|
|
public logBuild(msg: string) {
|
2018-10-15 11:32:27 +00:00
|
|
|
return this.streams.build.write(msg + eol);
|
|
|
|
}
|
2018-10-16 10:24:28 +00:00
|
|
|
|
2019-03-12 22:07:57 +00:00
|
|
|
public logLogs(msg: string) {
|
2018-10-16 10:24:28 +00:00
|
|
|
return this.streams.logs.write(msg + eol);
|
|
|
|
}
|
2019-01-14 16:46:36 +00:00
|
|
|
|
2019-04-16 10:33:35 +00:00
|
|
|
public logLivepush(msg: string) {
|
2019-01-14 16:46:36 +00:00
|
|
|
return this.streams.livepush.write(msg + eol);
|
|
|
|
}
|
2017-12-20 21:46:01 +00:00
|
|
|
}
|
2018-01-11 09:22:26 +00:00
|
|
|
|
|
|
|
export = Logger;
|