From 6f658a6f0bfcca17ca45d988704a0ee379fcc78c Mon Sep 17 00:00:00 2001 From: AkiraFukushima Date: Sat, 24 Mar 2018 10:57:41 +0900 Subject: [PATCH] refs #101 Add electron-log for production logger --- package-lock.json | 5 +++++ package.json | 1 + src/main/auth.js | 3 ++- src/main/index.js | 22 ++++++++++++++++++---- src/main/streaming.js | 9 +++++---- 5 files changed, 31 insertions(+), 9 deletions(-) diff --git a/package-lock.json b/package-lock.json index 1973f949..8bb4d385 100644 --- a/package-lock.json +++ b/package-lock.json @@ -4409,6 +4409,11 @@ } } }, + "electron-log": { + "version": "2.2.14", + "resolved": "https://registry.npmjs.org/electron-log/-/electron-log-2.2.14.tgz", + "integrity": "sha512-Rj+XyK4nShe/nv9v1Uks4KEfjtQ6N+eSnx5CLpAjG6rlyUdAflyFHoybcHSLoq9l9pGavclULWS5IXgk8umc2g==" + }, "electron-osx-sign": { "version": "0.4.8", "resolved": "https://registry.npmjs.org/electron-osx-sign/-/electron-osx-sign-0.4.8.tgz", diff --git a/package.json b/package.json index 0e88a7dc..6dd3ff5e 100644 --- a/package.json +++ b/package.json @@ -61,6 +61,7 @@ }, "dependencies": { "axios": "^0.18.0", + "electron-log": "^2.2.14", "element-ui": "^2.2.1", "google-fonts-webpack-plugin": "^0.4.4", "is-empty": "^1.2.0", diff --git a/src/main/auth.js b/src/main/auth.js index e262cd6f..66b77282 100644 --- a/src/main/auth.js +++ b/src/main/auth.js @@ -1,4 +1,5 @@ import Mastodon from 'mastodon-api' +import log from 'electron-log' const appName = 'whalebird' const scope = 'read write follow' @@ -23,7 +24,7 @@ export default class Authentication { getAuthorizationUrl (domain = 'mastodon.social') { this.setOtherInstance(domain) return Mastodon.createOAuthApp(this.baseURL + '/api/v1/apps', appName, scope) - .catch(err => console.error(err)) + .catch(err => log.error(err)) .then((res) => { this.clientId = res.client_id this.clientSecret = res.client_secret diff --git a/src/main/index.js b/src/main/index.js index 973df1ba..dfd8cdda 100644 --- a/src/main/index.js +++ b/src/main/index.js @@ -3,11 +3,18 @@ import { app, ipcMain, BrowserWindow, shell, Menu } from 'electron' import Datastore from 'nedb' import empty from 'is-empty' +import log from 'electron-log' import Authentication from './auth' import Account from './account' import Streaming from './streaming' +/** + * Set log level + */ +log.transports.console.level = 'debug' +log.transports.file.level = 'info' + /** * Set `__static` path to static files in production * https://simulatedgreg.gitbooks.io/electron-vue/content/en/using-static-assets.html @@ -198,11 +205,11 @@ let auth = new Authentication(db) ipcMain.on('get-auth-url', (event, domain) => { auth.getAuthorizationUrl(domain) .catch((err) => { - console.error(err) + log.error(err) event.sender.send('error-get-auth-url', err) }) .then((url) => { - console.log(url) + log.debug(url) event.sender.send('response-get-auth-url', url) // Open authorize url in default browser. shell.openExternal(url) @@ -212,7 +219,7 @@ ipcMain.on('get-auth-url', (event, domain) => { ipcMain.on('get-access-token', (event, code) => { auth.getAccessToken(code) .catch((err) => { - console.error(err) + log.error(err) event.sender.send('error-get-access-token', err) }) .then((token) => { @@ -240,7 +247,7 @@ ipcMain.on('list-accounts', (event, _) => { const account = new Account(db) account.listAccounts() .catch((err) => { - console.error(err) + log.error(err) event.sender.send('error-list-accounts', err) }) .then((accounts) => { @@ -252,6 +259,7 @@ ipcMain.on('get-local-account', (event, id) => { const account = new Account(db) account.getAccount(id) .catch((err) => { + log.error(err) event.sender.send('error-get-local-account', err) }) .then((account) => { @@ -266,6 +274,7 @@ ipcMain.on('start-user-streaming', (event, ac) => { const account = new Account(db) account.getAccount(ac._id) .catch((err) => { + log.error(err) event.sender.send('error-start-user-streaming', err) }) .then((account) => { @@ -284,6 +293,7 @@ ipcMain.on('start-user-streaming', (event, ac) => { event.sender.send('notification-start-user-streaming', notification) }, (err) => { + log.error(err) event.sender.send('error-start-user-streaming', err) } ) @@ -303,6 +313,7 @@ ipcMain.on('start-local-streaming', (event, ac) => { const account = new Account(db) account.getAccount(ac._id) .catch((err) => { + log.error(err) event.sender.send('error-start-local-streaming', err) }) .then((account) => { @@ -319,6 +330,7 @@ ipcMain.on('start-local-streaming', (event, ac) => { event.sender.send('update-start-local-streaming', update) }, (err) => { + log.error(err) event.sender.send('error-start-local-streaming', err) } ) @@ -336,6 +348,7 @@ ipcMain.on('start-public-streaming', (event, ac) => { const account = new Account(db) account.getAccount(ac._id) .catch((err) => { + log.error(err) event.sender.send('error-start-public-streaming', err) }) .then((account) => { @@ -352,6 +365,7 @@ ipcMain.on('start-public-streaming', (event, ac) => { event.sender.send('update-start-public-streaming', update) }, (err) => { + log.error(err) event.sender.send('error-start-public-streaming', err) } ) diff --git a/src/main/streaming.js b/src/main/streaming.js index 4aee744d..f7994894 100644 --- a/src/main/streaming.js +++ b/src/main/streaming.js @@ -1,4 +1,5 @@ import Mastodon from 'mastodon-api' +import log from 'electron-log' export default class Streaming { constructor (account) { @@ -14,7 +15,7 @@ export default class Streaming { startUserStreaming (updateCallback, notificationCallback, errCallback) { this.listener = this.client.stream('/streaming/user') - console.log('/streaming/user started') + log.info('/streaming/user started') this.listener.on('message', (msg) => { switch (msg.event) { @@ -25,7 +26,7 @@ export default class Streaming { notificationCallback(msg.data) break default: - console.log(msg) + log.debug(msg) break } }) @@ -37,7 +38,7 @@ export default class Streaming { start (path, updateCallback, errCallback) { this.listener = this.client.stream(path) - console.log(`${path} started`) + log.info(`${path} started`) this.listener.on('message', (msg) => { switch (msg.event) { @@ -56,6 +57,6 @@ export default class Streaming { stop () { this.listener.stop() - console.log('streaming stopped') + log.info('streaming stopped') } }