@atomist/automation-client
Version:
Atomist API for software low-level client
157 lines • 5.83 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const cluster = require("cluster");
const fs = require("fs");
const stringify = require("json-stringify-safe");
const _ = require("lodash");
const os = require("os");
const p = require("path");
const serializeError = require("serialize-error");
const stripAnsi = require("strip-ansi");
const winston = require("winston");
const context = require("./cls");
exports.LoggingConfig = {
format: "logger",
};
function setLogLevel(level) {
winstonLogger.transports.console.level = level;
}
exports.setLogLevel = setLogLevel;
function addFileTransport(filename, level) {
const path = p.resolve(filename);
if (!fs.existsSync(p.dirname(path))) {
fs.mkdirSync(p.dirname(path));
}
winstonLogger.add(winston.transports.File, {
level,
filename: p.basename(path),
dirname: p.dirname(path),
maxsize: 10 * 1024 * 1024,
maxFiles: 10,
tailable: true,
zippedArchive: true,
json: false,
formatter,
});
}
exports.addFileTransport = addFileTransport;
function formatter(options) {
if (exports.LoggingConfig.format === "cli") {
return options.message;
}
const executionContext = context.get();
let ctx;
if (cluster.isMaster) {
ctx = options.colorize ? winston.config.colorize(options.level, "m")
: "m";
}
else {
ctx = options.colorize ? winston.config.colorize(options.level, "w")
: "w";
}
ctx += ":" + (options.colorize ? winston.config.colorize(options.level, process.pid.toString())
: process.pid);
if (executionContext) {
if (executionContext.invocationId) {
ctx += ":" + (options.colorize ? winston.config.colorize(options.level, executionContext.invocationId)
: executionContext.invocationId);
}
if (executionContext.workspaceName) {
ctx += ":" + (options.colorize ? winston.config.colorize(options.level, executionContext.workspaceName)
: executionContext.workspaceName);
}
else if (executionContext.workspaceId) {
ctx += ":" + (options.colorize ? winston.config.colorize(options.level, executionContext.workspaceId)
: executionContext.workspaceId);
}
if (executionContext.operation) {
ctx += ":" + (options.colorize ? winston.config.colorize(options.level, executionContext.operation)
: executionContext.operation);
}
if (executionContext.ts) {
const duration = _.padStart((new Date().getTime() - executionContext.ts).toString(), 3, "0");
ctx += ":" + (options.colorize ? winston.config.colorize(options.level, duration)
: duration);
}
}
const level = options.colorize ? winston.config.colorize(options.level, _.padEnd(options.level, 5)) :
_.padEnd(options.level, 5);
const formatted = (options.timestamp ? new Date().toISOString() : "") + (ctx ? " [" + ctx + "]" : "")
+ " [" + level + "] " + (options.message ? options.message : "") +
(options.meta && Object.keys(options.meta).length ?
(options.message ? ": " : "") + stringify(options.meta) : "");
if (options.colorize) {
return formatted;
}
else {
return stripAnsi(formatted);
}
}
exports.formatter = formatter;
const winstonLogger = new winston.Logger({
level: "debug",
// handleExceptions: true,
// humanReadableUnhandledException: true,
exitOnError: false,
transports: [
new (winston.transports.Console)({
level: "info",
json: false,
colorize: require("chalk").supportsColor,
prettyPrint: true,
timestamp: true,
showLevel: true,
align: true,
stderrLevels: ["error"],
formatter,
}),
],
});
function directConsoleToLogger() {
// Redirect console logging methods to our logging setup
console.error = (message, ...optionalParams) => {
winstonLogger.error(message, ...optionalParams);
};
console.info = (message, ...optionalParams) => {
winstonLogger.info(message, ...optionalParams);
};
console.log = (message, ...optionalParams) => {
winstonLogger.info(message, ...optionalParams);
};
console.trace = (message, ...optionalParams) => {
winstonLogger.debug(message, ...optionalParams);
};
console.warn = (message, ...optionalParams) => {
winstonLogger.warn(message, ...optionalParams);
};
}
function initLogging() {
// Normal startup of a client will redirect console logging
// If startup happens with ATOMIST_DISABLE_LOGGING no console output will be redirected
// and winston's console transport is set to silent to allow full control over logging
// output
if (process.env.ATOMIST_DISABLE_LOGGING !== "true") {
directConsoleToLogger();
}
else {
winstonLogger.transports.console.silent = true;
// Add file logging into log directory
const appDir = __dirname.split(p.join("node_modules", "@atomist", "automation-client"))[0];
let pj;
try {
pj = require(p.join(appDir, "package.json"));
}
catch (e) {
pj = { name: "atm-client" };
}
const filename = p.join(os.homedir(), ".atomist", "log", `${pj.name.replace(/^.*\//, "")}-local.log`);
addFileTransport(filename, "debug");
}
}
exports.logger = winstonLogger;
// Ideally we wouldn't need this, but I'm still adding proper error handling
process.on("uncaughtException", err => {
console.error(serializeError(err));
});
initLogging();
//# sourceMappingURL=logger.js.map