Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve logger a bit #1026

Merged
merged 5 commits into from
Oct 2, 2019
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ Debug messages are disabled by default, enable them with the `--log-debug` flag.
| `--log-debug` | Log debug messages |
| `--log-to-console` | Output the log to stout / chrome dev console |
| `--machine-readable-stacktrace` | Enable JSON stacktrace |
| `--no-color` | Disable colors in the output of main process |

#### Log locations

Expand Down
9 changes: 4 additions & 5 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,8 @@
"simple-markdown": "^0.4.4",
"styled-components": "^4.3.2",
"tempy": "^0.3.0",
"use-debounce": "^3.0.1"
"use-debounce": "^3.0.1",
"colors": "^1.4.0"
},
"devDependencies": {
"@babel/core": "^7.5.5",
Expand All @@ -91,7 +92,6 @@
"@types/mapbox-gl": "^0.54.2",
"babel-loader": "^8.0.6",
"chai": "^4.2.0",
"colors": "^1.3.3",
"cp-file": "^7.0.0",
"depcheck": "^0.8.3",
"electron": "^4.2.9",
Expand Down
44 changes: 33 additions & 11 deletions src/logger.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
const esp = require('error-stack-parser')
const { app, remote } = require('electron')
const colors = require('colors/safe')
const startTime = Date.now()

const rc = remote ? remote.app.rc : app ? app.rc : {}

Expand All @@ -17,7 +19,7 @@ function setLogHandler (LogHandler) {
handler = LogHandler
}

function log (channel, level, ...args) {
function log ({ channel, isMainProcess }, level, stacktrace, args) {
const variant = LoggerVariants[level]
if (!handler) {
/* ignore-console-log */
Expand All @@ -26,9 +28,28 @@ function log (channel, level, ...args) {
console.log(`Log Message: ${channel} ${level} ${args.join(' ')}`)
throw Error('Failed to log message - Handler not initilized yet')
}
handler(channel, variant.level, ...args)
handler(channel, variant.level, stacktrace, ...args)
if (rc['log-to-console']) {
variant.log(channel, variant.level, ...args)
if (isMainProcess) {
const begining = `${Math.round((Date.now() - startTime) / 100) / 10}s ${[
'[D]',
colors.blue('[i]'),
colors.yellow('[w]'),
colors.red('[E]'),
colors.red('[C]')
][level]}${colors.grey(channel)}:`
if (!stacktrace) {
console.log(begining, ...args)
Simon-Laux marked this conversation as resolved.
Show resolved Hide resolved
} else {
console.log(begining, ...args, colors.red(stacktrace))
Simon-Laux marked this conversation as resolved.
Show resolved Hide resolved
}
} else {
if (stacktrace) {
variant.log(channel, variant.level, stacktrace, ...args)
} else {
variant.log(channel, variant.level, ...args)
}
}
}
}

Expand All @@ -39,34 +60,35 @@ function getStackTrace () {
}

class Logger {
constructor (channel) {
constructor (channel, isMainProcess) {
this.channel = channel
this.isMainProcess = isMainProcess
}

debug (...args) {
if (!rc['log-debug']) return
log(this.channel, 0, [], ...args)
log(this, 0, undefined, args)
}

info (...args) {
log(this.channel, 1, [], ...args)
log(this, 1, undefined, args)
}

warn (...args) {
log(this.channel, 2, getStackTrace(), ...args)
log(this, 2, getStackTrace(), args)
}

error (...args) {
log(this.channel, 3, getStackTrace(), ...args)
log(this, 3, getStackTrace(), args)
}

critical (...args) {
log(this.channel, 4, getStackTrace(), ...args)
log(this, 4, getStackTrace(), args)
}
}

function getLogger (channel) {
return new Logger(channel)
function getLogger (channel, isMainProcess = false) {
return new Logger(channel, isMainProcess)
}

module.exports = { setLogHandler, Logger, getLogger }
2 changes: 1 addition & 1 deletion src/main/deltachat/backup.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const fs = require('fs-extra')
const { ipcMain } = require('electron')
const path = require('path')
const EventEmitter = require('events').EventEmitter
const log = require('../../logger').getLogger('main/deltachat/backup')
const log = require('../../logger').getLogger('main/deltachat/backup', true)

function backupExport (dir) {
this._dc.importExport(C.DC_IMEX_EXPORT_BACKUP, dir)
Expand Down
2 changes: 1 addition & 1 deletion src/main/deltachat/chatlist.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const C = require('deltachat-node/constants')
const log = require('../../logger').getLogger('main/deltachat/chatlist')
const log = require('../../logger').getLogger('main/deltachat/chatlist', true)
const { app } = require('electron')

function selectChat (chatId) {
Expand Down
2 changes: 1 addition & 1 deletion src/main/deltachat/chatmethods.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const DeltaChat = require('deltachat-node')
const C = require('deltachat-node/constants')
const log = require('../../logger').getLogger('main/deltachat/chatmethods')
const log = require('../../logger').getLogger('main/deltachat/chatmethods', true)

function getInfo () {
if (this.ready === true) {
Expand Down
5 changes: 3 additions & 2 deletions src/main/deltachat/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
const C = require('deltachat-node/constants')
const eventStrings = require('deltachat-node/events')
const EventEmitter = require('events').EventEmitter
const log = require('../../logger').getLogger('main/deltachat')
const log = require('../../logger').getLogger('main/deltachat', true)
const logCoreEv = require('../../logger').getLogger('core/event', true)
const windows = require('../windows')
const { app } = require('electron')

Expand Down Expand Up @@ -38,7 +39,7 @@ class DeltaChatController extends EventEmitter {

if (data1 === 0) data1 = ''

log.debug('Core Event', event, data1, data2)
logCoreEv.debug(event, data1, data2)
}

callMethod (evt, methodName, args) {
Expand Down
2 changes: 1 addition & 1 deletion src/main/deltachat/login.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const DeltaChat = require('deltachat-node')
const C = require('deltachat-node/constants')
const log = require('../../logger').getLogger('main/deltachat/login')
const log = require('../../logger').getLogger('main/deltachat/login', true)
const path = require('path')

/**
Expand Down
2 changes: 1 addition & 1 deletion src/main/deltachat/settings.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const C = require('deltachat-node/constants')
const log = require('../../logger').getLogger('main/deltachat/settings')
const log = require('../../logger').getLogger('main/deltachat/settings', true)

function setConfig (key, value) {
log.info(`Setting config ${key}:${value}`)
Expand Down
2 changes: 1 addition & 1 deletion src/main/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ mkdirp.sync(getLogsPath())
// Setup Logger
const logHandler = require('./log-handler')()
const logger = require('../logger')
const log = logger.getLogger('main/index')
const log = logger.getLogger('main/index', true)
logger.setLogHandler(logHandler.log)
process.on('exit', logHandler.end)

Expand Down
2 changes: 1 addition & 1 deletion src/main/ipc.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const { getConfigPath } = require('../application-constants')
const localize = require('../localize')
const menu = require('./menu')
const windows = require('./windows')
const log = require('../logger').getLogger('main/ipc')
const log = require('../logger').getLogger('main/ipc', true)
const DeltaChat = (() => {
try {
return require('./deltachat/index.js')
Expand Down
2 changes: 1 addition & 1 deletion src/main/log-handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ function logName () {
`${pad(d.getHours())}-`,
`${pad(d.getMinutes())}-`,
`${pad(d.getSeconds())}`,
'.log'
'.log.tsv'
].join('')
return path.join(dir, fileName)
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/menu.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module.exports = { init }

const { app, Menu, shell } = require('electron')
const log = require('../logger').getLogger('main/menu')
const log = require('../logger').getLogger('main/menu', true)
const windows = require('./windows')
const {
homePageUrl,
Expand Down
2 changes: 1 addition & 1 deletion src/main/state.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const appConfig = require('../application-config')
const { EventEmitter } = require('events')
const log = require('../logger').getLogger('main/state')
const log = require('../logger').getLogger('main/state', true)

const SAVE_DEBOUNCE_INTERVAL = 1000

Expand Down
2 changes: 1 addition & 1 deletion src/main/windows/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ const {
windowDefaults
} = require('../../application-constants')

const log = require('../../logger').getLogger('main/mainWindow')
const log = require('../../logger').getLogger('main/mainWindow', true)

function init (app, options) {
if (main.win) {
Expand Down