start to use winston to log
This commit is contained in:
@@ -19,7 +19,7 @@ const APUser = require('../models/ap_user')
|
|||||||
|
|
||||||
const exportController = require('./export')
|
const exportController = require('./export')
|
||||||
|
|
||||||
const debug = require('debug')('controller:event')
|
const log = require('../../log')
|
||||||
|
|
||||||
const eventController = {
|
const eventController = {
|
||||||
|
|
||||||
@@ -52,7 +52,7 @@ const eventController = {
|
|||||||
},
|
},
|
||||||
|
|
||||||
async getNotifications (event, action) {
|
async getNotifications (event, action) {
|
||||||
debug('getNotifications "%s" (%s)', event.title, action)
|
log.info('getNotifications "%s" (%s)', event.title, action)
|
||||||
function match (event, filters) {
|
function match (event, filters) {
|
||||||
// matches if no filter specified
|
// matches if no filter specified
|
||||||
if (!filters) { return true }
|
if (!filters) { return true }
|
||||||
@@ -189,7 +189,7 @@ const eventController = {
|
|||||||
const notifier = require('../../notifier')
|
const notifier = require('../../notifier')
|
||||||
notifier.notifyEvent('Create', event.id)
|
notifier.notifyEvent('Create', event.id)
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
debug(e)
|
log.info(e)
|
||||||
res.sendStatus(404)
|
res.sendStatus(404)
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
@@ -206,7 +206,7 @@ const eventController = {
|
|||||||
await event.update({ is_visible: false })
|
await event.update({ is_visible: false })
|
||||||
res.sendStatus(200)
|
res.sendStatus(200)
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
debug(e)
|
log.info(e)
|
||||||
res.sendStatus(404)
|
res.sendStatus(404)
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
@@ -225,7 +225,7 @@ const eventController = {
|
|||||||
})
|
})
|
||||||
res.json(events)
|
res.json(events)
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
debug(e)
|
log.info(e)
|
||||||
res.sendStatus(400)
|
res.sendStatus(400)
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
@@ -259,7 +259,7 @@ const eventController = {
|
|||||||
async add (req, res) {
|
async add (req, res) {
|
||||||
// req.err comes from multer streaming error
|
// req.err comes from multer streaming error
|
||||||
if (req.err) {
|
if (req.err) {
|
||||||
debug(req.err)
|
log.info(req.err)
|
||||||
return res.status(400).json(req.err.toString())
|
return res.status(400).json(req.err.toString())
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -326,7 +326,8 @@ const eventController = {
|
|||||||
notifier.notifyEvent('Create', event.id)
|
notifier.notifyEvent('Create', event.id)
|
||||||
}
|
}
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
debug(e)
|
process.winstonLog.error(e)
|
||||||
|
log.info(e)
|
||||||
res.sendStatus(400)
|
res.sendStatus(400)
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
@@ -361,7 +362,7 @@ const eventController = {
|
|||||||
await fs.unlinkSync(old_path)
|
await fs.unlinkSync(old_path)
|
||||||
await fs.unlinkSync(old_thumb_path)
|
await fs.unlinkSync(old_thumb_path)
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
debug(e.toString())
|
log.info(e.toString())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
eventDetails.image_path = req.file.filename
|
eventDetails.image_path = req.file.filename
|
||||||
@@ -406,7 +407,7 @@ const eventController = {
|
|||||||
fs.unlinkSync(old_thumb_path)
|
fs.unlinkSync(old_thumb_path)
|
||||||
fs.unlinkSync(old_path)
|
fs.unlinkSync(old_path)
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
debug(e.toString())
|
log.info(e.toString())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
const notifier = require('../../notifier')
|
const notifier = require('../../notifier')
|
||||||
@@ -506,15 +507,16 @@ const eventController = {
|
|||||||
const frequency = recurrent.frequency
|
const frequency = recurrent.frequency
|
||||||
const type = recurrent.type
|
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)
|
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
|
// each week or 2
|
||||||
if (frequency[1] === 'w') {
|
if (frequency[1] === 'w') {
|
||||||
cursor = cursor.day(start_date.day())
|
cursor = cursor.day(start_date.day())
|
||||||
debug(`Imposto il giorno della settimana ${cursor}`)
|
|
||||||
if (cursor.isBefore(dayjs())) {
|
if (cursor.isBefore(dayjs())) {
|
||||||
cursor = cursor.add(7, 'day')
|
cursor = cursor.add(7, 'day')
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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.put('/event', hasPerm('event:write'), upload.single('image'), eventController.update)
|
||||||
api.get('/event/import', helpers.importURL)
|
api.get('/event/import', helpers.importURL)
|
||||||
|
|
||||||
|
|
||||||
// remove event
|
// remove event
|
||||||
api.delete('/event/:id', hasPerm('event:remove'), eventController.remove)
|
api.delete('/event/:id', hasPerm('event:remove'), eventController.remove)
|
||||||
|
|
||||||
|
|||||||
@@ -3,12 +3,13 @@ const path = require('path')
|
|||||||
const moment = require('dayjs')
|
const moment = require('dayjs')
|
||||||
const config = require('config')
|
const config = require('config')
|
||||||
const settingsController = require('./controller/settings')
|
const settingsController = require('./controller/settings')
|
||||||
const debug = require('debug')('email')
|
const log = process.winstonLog
|
||||||
const { Task, TaskManager } = require('../taskManager')
|
const { Task, TaskManager } = require('../taskManager')
|
||||||
const locales = require('../../locales')
|
const locales = require('../../locales')
|
||||||
|
|
||||||
const mail = {
|
const mail = {
|
||||||
send (addresses, template, locals, locale = settingsController.settings.instance_locale) {
|
send (addresses, template, locals, locale = settingsController.settings.instance_locale) {
|
||||||
|
log.debug('Enqueue new email ', template, locale)
|
||||||
const task = new Task({
|
const task = new Task({
|
||||||
name: 'MAIL',
|
name: 'MAIL',
|
||||||
removable: true,
|
removable: true,
|
||||||
@@ -19,7 +20,7 @@ const mail = {
|
|||||||
},
|
},
|
||||||
|
|
||||||
_send (addresses, template, locals, locale) {
|
_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({
|
const email = new Email({
|
||||||
views: { root: path.join(__dirname, '..', 'emails') },
|
views: { root: path.join(__dirname, '..', 'emails') },
|
||||||
htmlToText: true,
|
htmlToText: true,
|
||||||
@@ -61,7 +62,7 @@ const mail = {
|
|||||||
}
|
}
|
||||||
return email.send(msg)
|
return email.send(msg)
|
||||||
.catch(e => {
|
.catch(e => {
|
||||||
debug('Error sending email =>', e.toString())
|
log.error('Error sending email =>', e.toString())
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,7 +4,6 @@ const { Nuxt, Builder } = require('nuxt')
|
|||||||
const nuxtConfig = require('../nuxt.config.js')
|
const nuxtConfig = require('../nuxt.config.js')
|
||||||
const config = require('config')
|
const config = require('config')
|
||||||
const consola = require('consola')
|
const consola = require('consola')
|
||||||
const { TaskManager } = require('./taskManager')
|
|
||||||
|
|
||||||
async function main () {
|
async function main () {
|
||||||
nuxtConfig.server = config.server
|
nuxtConfig.server = config.server
|
||||||
@@ -23,10 +22,15 @@ async function main () {
|
|||||||
await nuxt.listen()
|
await nuxt.listen()
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
consola.error(e.toString())
|
consola.error(e.toString())
|
||||||
|
process.winstonLog.error(e)
|
||||||
return
|
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()
|
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
|
// close connections/port/unix socket
|
||||||
function shutdown () {
|
function shutdown () {
|
||||||
|
|||||||
1
server/log.js
Normal file
1
server/log.js
Normal file
@@ -0,0 +1 @@
|
|||||||
|
module.exports = process.winstonLog
|
||||||
@@ -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 eventController = require('./api/controller/event')
|
||||||
// const notifier = require('./notifier')
|
// 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 {
|
class Task {
|
||||||
constructor ({ name, removable = false, repeatEach = 1, method, args = [] }) {
|
constructor ({ name, removable = false, repeatEach = 1, method, args = [] }) {
|
||||||
this.name = name
|
this.name = name
|
||||||
@@ -21,11 +26,11 @@ class Task {
|
|||||||
try {
|
try {
|
||||||
const ret = this.method.apply(this, this.args)
|
const ret = this.method.apply(this, this.args)
|
||||||
if (ret && typeof ret.then === 'function') {
|
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
|
return ret
|
||||||
}
|
}
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
debug('TASK ERROR ', this.name, e)
|
log.error('TASK ERROR ', this.name, e)
|
||||||
return Promise.resolve(false)
|
return Promise.resolve(false)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -44,22 +49,22 @@ class TaskManager {
|
|||||||
this.tasks = []
|
this.tasks = []
|
||||||
}
|
}
|
||||||
|
|
||||||
start (interval = 60 * 1000) {
|
start (interval = loopInterval) {
|
||||||
debug('START')
|
log.info(`START TASK MANAGER WITH LOOP INTERVAL OF ${interval} seconds`)
|
||||||
this.interval = interval
|
this.interval = interval
|
||||||
this.timeout = setTimeout(this.tick.bind(this), interval)
|
this.timeout = setTimeout(this.tick.bind(this), interval * 1000)
|
||||||
}
|
}
|
||||||
|
|
||||||
stop () {
|
stop () {
|
||||||
if (this.timeout) {
|
if (this.timeout) {
|
||||||
debug('STOP')
|
log.debug('STOP TASKMANAGER')
|
||||||
clearTimeout(this.timeout)
|
clearTimeout(this.timeout)
|
||||||
this.timeout = false
|
this.timeout = false
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
add (task) {
|
add (task) {
|
||||||
debug('ADD TASK ', task.name)
|
log.info(`ADD TASK ${task.name}`)
|
||||||
this.tasks.push(task)
|
this.tasks.push(task)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -79,7 +84,7 @@ class TaskManager {
|
|||||||
|
|
||||||
async tick () {
|
async tick () {
|
||||||
await this.process()
|
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({
|
TS.add(new Task({
|
||||||
name: 'RECURRENT_EVENT',
|
name: 'RECURRENT_EVENT',
|
||||||
method: eventController._createRecurrent,
|
method: eventController._createRecurrent,
|
||||||
repeatEach: 1 // check each 10 minutes
|
repeatEach: 10 * minute // check each 10 minutes
|
||||||
}))
|
}))
|
||||||
|
|
||||||
// daily morning notification
|
// daily morning notification
|
||||||
|
|||||||
Reference in New Issue
Block a user