Skip to content

Commit

Permalink
Only log to console if we're on a TTY stdout interface
Browse files Browse the repository at this point in the history
  • Loading branch information
scottnonnenberg-signal committed Aug 21, 2019
1 parent 6ac7f4c commit b6da081
Showing 1 changed file with 14 additions and 8 deletions.
22 changes: 14 additions & 8 deletions app/logging.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ const { app, ipcMain: ipc } = electron;
const LEVELS = ['fatal', 'error', 'warn', 'info', 'debug', 'trace'];
let logger;

const isRunningFromConsole = Boolean(process.stdout.isTTY);

module.exports = {
initialize,
getLogger,
Expand All @@ -40,22 +42,26 @@ function initialize() {

return cleanupLogs(logPath).then(() => {
const logFile = path.join(logPath, 'log.log');

logger = bunyan.createLogger({
const loggerOptions = {
name: 'log',
streams: [
{
level: 'debug',
stream: process.stdout,
},
{
type: 'rotating-file',
path: logFile,
period: '1d',
count: 3,
},
],
});
};

if (isRunningFromConsole) {
loggerOptions.streams.push({
level: 'debug',
stream: process.stdout,
});
}

logger = bunyan.createLogger(loggerOptions);

LEVELS.forEach(level => {
ipc.on(`log-${level}`, (first, ...rest) => {
Expand Down Expand Up @@ -260,7 +266,7 @@ function logAtLevel(level, ...args) {
return item;
});
logger[level](redactAll(str.join(' ')));
} else {
} else if (isRunningFromConsole) {
console._log(...args);
}
}
Expand Down

0 comments on commit b6da081

Please sign in to comment.