From c948a5bd47868424eb79c5548a034bdec5545172 Mon Sep 17 00:00:00 2001 From: les Date: Tue, 9 Feb 2021 12:17:10 +0100 Subject: [PATCH] start to use winston to log --- server/api/controller/event.js | 26 ++++++++++++++------------ server/api/index.js | 1 - server/api/mail.js | 7 ++++--- server/index.js | 8 ++++++-- server/log.js | 1 + server/taskManager.js | 25 +++++++++++++++---------- 6 files changed, 40 insertions(+), 28 deletions(-) create mode 100644 server/log.js diff --git a/server/api/controller/event.js b/server/api/controller/event.js index 41f37054..dfef5fca 100644 --- a/server/api/controller/event.js +++ b/server/api/controller/event.js @@ -19,7 +19,7 @@ const APUser = require('../models/ap_user') const exportController = require('./export') -const debug = require('debug')('controller:event') +const log = require('../../log') const eventController = { @@ -52,7 +52,7 @@ const eventController = { }, async getNotifications (event, action) { - debug('getNotifications "%s" (%s)', event.title, action) + log.info('getNotifications "%s" (%s)', event.title, action) function match (event, filters) { // matches if no filter specified if (!filters) { return true } @@ -189,7 +189,7 @@ const eventController = { const notifier = require('../../notifier') notifier.notifyEvent('Create', event.id) } catch (e) { - debug(e) + log.info(e) res.sendStatus(404) } }, @@ -206,7 +206,7 @@ const eventController = { await event.update({ is_visible: false }) res.sendStatus(200) } catch (e) { - debug(e) + log.info(e) res.sendStatus(404) } }, @@ -225,7 +225,7 @@ const eventController = { }) res.json(events) } catch (e) { - debug(e) + log.info(e) res.sendStatus(400) } }, @@ -259,7 +259,7 @@ const eventController = { async add (req, res) { // req.err comes from multer streaming error if (req.err) { - debug(req.err) + log.info(req.err) return res.status(400).json(req.err.toString()) } @@ -326,7 +326,8 @@ const eventController = { notifier.notifyEvent('Create', event.id) } } catch (e) { - debug(e) + process.winstonLog.error(e) + log.info(e) res.sendStatus(400) } }, @@ -361,7 +362,7 @@ const eventController = { await fs.unlinkSync(old_path) await fs.unlinkSync(old_thumb_path) } catch (e) { - debug(e.toString()) + log.info(e.toString()) } } eventDetails.image_path = req.file.filename @@ -406,7 +407,7 @@ const eventController = { fs.unlinkSync(old_thumb_path) fs.unlinkSync(old_path) } catch (e) { - debug(e.toString()) + log.info(e.toString()) } } const notifier = require('../../notifier') @@ -506,15 +507,16 @@ const eventController = { const frequency = recurrent.frequency const type = recurrent.type - debug(`NOW IS ${cursor} while event is at ${start_date} (freq: ${frequency})`) + log.info(`NOW IS ${cursor} while event is at ${start_date} (freq: ${frequency})`) cursor = cursor.hour(start_date.hour()).minute(start_date.minute()).second(0) - debug(`set cursor to correct date and hour => ${cursor}`) + log.info(`set cursor to correct date and hour => ${cursor}`) + + if (!frequency) { return } // each week or 2 if (frequency[1] === 'w') { cursor = cursor.day(start_date.day()) - debug(`Imposto il giorno della settimana ${cursor}`) if (cursor.isBefore(dayjs())) { cursor = cursor.add(7, 'day') } diff --git a/server/api/index.js b/server/api/index.js index 81bc13d1..4660c78d 100644 --- a/server/api/index.js +++ b/server/api/index.js @@ -89,7 +89,6 @@ api.post('/event', hasPerm('event:write'), upload.single('image'), eventControll api.put('/event', hasPerm('event:write'), upload.single('image'), eventController.update) api.get('/event/import', helpers.importURL) - // remove event api.delete('/event/:id', hasPerm('event:remove'), eventController.remove) diff --git a/server/api/mail.js b/server/api/mail.js index d590aa08..ea816a9e 100644 --- a/server/api/mail.js +++ b/server/api/mail.js @@ -3,12 +3,13 @@ const path = require('path') const moment = require('dayjs') const config = require('config') const settingsController = require('./controller/settings') -const debug = require('debug')('email') +const log = process.winstonLog const { Task, TaskManager } = require('../taskManager') const locales = require('../../locales') const mail = { send (addresses, template, locals, locale = settingsController.settings.instance_locale) { + log.debug('Enqueue new email ', template, locale) const task = new Task({ name: 'MAIL', removable: true, @@ -19,7 +20,7 @@ const mail = { }, _send (addresses, template, locals, locale) { - debug(`Send ${template} email to ${addresses} with locale ${locale}`) + log.debug(`Send ${template} email to ${addresses} with locale ${locale}`) const email = new Email({ views: { root: path.join(__dirname, '..', 'emails') }, htmlToText: true, @@ -61,7 +62,7 @@ const mail = { } return email.send(msg) .catch(e => { - debug('Error sending email =>', e.toString()) + log.error('Error sending email =>', e.toString()) }) } } diff --git a/server/index.js b/server/index.js index 097f106c..6ce5d905 100644 --- a/server/index.js +++ b/server/index.js @@ -4,7 +4,6 @@ const { Nuxt, Builder } = require('nuxt') const nuxtConfig = require('../nuxt.config.js') const config = require('config') const consola = require('consola') -const { TaskManager } = require('./taskManager') async function main () { nuxtConfig.server = config.server @@ -23,10 +22,15 @@ async function main () { await nuxt.listen() } catch (e) { consola.error(e.toString()) + process.winstonLog.error(e) return } - consola.info('Listen on %s:%d , visit me here => %s', config.server.host, config.server.port, config.baseurl) + + const { TaskManager } = require('./taskManager') TaskManager.start() + const msg = `Listen on ${config.server.host}:${config.server.port} , visit me here => ${config.baseurl}` + consola.info(msg) + process.winstonLog.info(msg) // close connections/port/unix socket function shutdown () { diff --git a/server/log.js b/server/log.js new file mode 100644 index 00000000..ed902372 --- /dev/null +++ b/server/log.js @@ -0,0 +1 @@ +module.exports = process.winstonLog diff --git a/server/taskManager.js b/server/taskManager.js index cb3784f2..ecd918df 100644 --- a/server/taskManager.js +++ b/server/taskManager.js @@ -1,7 +1,12 @@ -const debug = require('debug')('TaskManager') +const log = process.winstonLog // require('./log') // // require('debug')('TaskManager') const eventController = require('./api/controller/event') // const notifier = require('./notifier') +const loopInterval = process.env.NODE_ENV === 'production' ? 15 : 1 +const minute = 60 / loopInterval +const hour = minute * 60 +const day = hour * 24 + class Task { constructor ({ name, removable = false, repeatEach = 1, method, args = [] }) { this.name = name @@ -21,11 +26,11 @@ class Task { try { const ret = this.method.apply(this, this.args) if (ret && typeof ret.then === 'function') { - ret.catch(e => debug('TASK ERROR ', this.name, e)) + ret.catch(e => log.error('TASK ERROR ', this.name, e)) return ret } } catch (e) { - debug('TASK ERROR ', this.name, e) + log.error('TASK ERROR ', this.name, e) return Promise.resolve(false) } } @@ -44,22 +49,22 @@ class TaskManager { this.tasks = [] } - start (interval = 60 * 1000) { - debug('START') + start (interval = loopInterval) { + log.info(`START TASK MANAGER WITH LOOP INTERVAL OF ${interval} seconds`) this.interval = interval - this.timeout = setTimeout(this.tick.bind(this), interval) + this.timeout = setTimeout(this.tick.bind(this), interval * 1000) } stop () { if (this.timeout) { - debug('STOP') + log.debug('STOP TASKMANAGER') clearTimeout(this.timeout) this.timeout = false } } add (task) { - debug('ADD TASK ', task.name) + log.info(`ADD TASK ${task.name}`) this.tasks.push(task) } @@ -79,7 +84,7 @@ class TaskManager { async tick () { await this.process() - this.timeout = setTimeout(this.tick.bind(this), this.interval) + this.timeout = setTimeout(this.tick.bind(this), this.interval * 1000) } } @@ -89,7 +94,7 @@ const TS = new TaskManager() TS.add(new Task({ name: 'RECURRENT_EVENT', method: eventController._createRecurrent, - repeatEach: 1 // check each 10 minutes + repeatEach: 10 * minute // check each 10 minutes })) // daily morning notification