Only log to console if we're on a TTY stdout interface

This commit is contained in:
Scott Nonnenberg 2019-08-19 07:35:09 -07:00
parent 6ac7f4ccf6
commit b6da081d05

View file

@ -18,6 +18,8 @@ const { app, ipcMain: ipc } = electron;
const LEVELS = ['fatal', 'error', 'warn', 'info', 'debug', 'trace']; const LEVELS = ['fatal', 'error', 'warn', 'info', 'debug', 'trace'];
let logger; let logger;
const isRunningFromConsole = Boolean(process.stdout.isTTY);
module.exports = { module.exports = {
initialize, initialize,
getLogger, getLogger,
@ -40,14 +42,9 @@ function initialize() {
return cleanupLogs(logPath).then(() => { return cleanupLogs(logPath).then(() => {
const logFile = path.join(logPath, 'log.log'); const logFile = path.join(logPath, 'log.log');
const loggerOptions = {
logger = bunyan.createLogger({
name: 'log', name: 'log',
streams: [ streams: [
{
level: 'debug',
stream: process.stdout,
},
{ {
type: 'rotating-file', type: 'rotating-file',
path: logFile, path: logFile,
@ -55,7 +52,16 @@ function initialize() {
count: 3, count: 3,
}, },
], ],
}); };
if (isRunningFromConsole) {
loggerOptions.streams.push({
level: 'debug',
stream: process.stdout,
});
}
logger = bunyan.createLogger(loggerOptions);
LEVELS.forEach(level => { LEVELS.forEach(level => {
ipc.on(`log-${level}`, (first, ...rest) => { ipc.on(`log-${level}`, (first, ...rest) => {
@ -260,7 +266,7 @@ function logAtLevel(level, ...args) {
return item; return item;
}); });
logger[level](redactAll(str.join(' '))); logger[level](redactAll(str.join(' ')));
} else { } else if (isRunningFromConsole) {
console._log(...args); console._log(...args);
} }
} }