start to use winston to log

This commit is contained in:
les 2021-02-09 12:17:10 +01:00
parent f10cfdbd82
commit c948a5bd47
6 changed files with 40 additions and 28 deletions

View file

@ -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')
}

View file

@ -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)

View file

@ -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())
})
}
}

View file

@ -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 () {

1
server/log.js Normal file
View file

@ -0,0 +1 @@
module.exports = process.winstonLog

View file

@ -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