You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

110 lines
3.2 KiB

  1. const Model = require('objection').Model
  2. const path = require('path')
  3. const fs = require('fs-extra')
  4. const _ = require('lodash')
  5. const yaml = require('js-yaml')
  6. const commonHelper = require('../helpers/common')
  7. /* global WIKI */
  8. /**
  9. * Logger model
  10. */
  11. module.exports = class Logger extends Model {
  12. static get tableName() { return 'loggers' }
  13. static get idColumn() { return 'key' }
  14. static get jsonSchema () {
  15. return {
  16. type: 'object',
  17. required: ['key', 'isEnabled'],
  18. properties: {
  19. key: {type: 'string'},
  20. isEnabled: {type: 'boolean'},
  21. level: {type: 'string'},
  22. config: {type: 'object'}
  23. }
  24. }
  25. }
  26. static async getLoggers() {
  27. return WIKI.models.loggers.query()
  28. }
  29. static async refreshLoggersFromDisk() {
  30. let trx
  31. try {
  32. const dbLoggers = await WIKI.models.loggers.query()
  33. // -> Fetch definitions from disk
  34. const loggersDirs = await fs.readdir(path.join(WIKI.SERVERPATH, 'modules/logging'))
  35. let diskLoggers = []
  36. for (let dir of loggersDirs) {
  37. const def = await fs.readFile(path.join(WIKI.SERVERPATH, 'modules/logging', dir, 'definition.yml'), 'utf8')
  38. diskLoggers.push(yaml.safeLoad(def))
  39. }
  40. WIKI.data.loggers = diskLoggers.map(logger => ({
  41. ...logger,
  42. props: commonHelper.parseModuleProps(logger.props)
  43. }))
  44. // -> Insert new loggers
  45. let newLoggers = []
  46. for (let logger of WIKI.data.loggers) {
  47. if (!_.some(dbLoggers, ['key', logger.key])) {
  48. newLoggers.push({
  49. key: logger.key,
  50. isEnabled: (logger.key === 'console'),
  51. level: logger.defaultLevel,
  52. config: _.transform(logger.props, (result, value, key) => {
  53. _.set(result, key, value.default)
  54. return result
  55. }, {})
  56. })
  57. } else {
  58. const loggerConfig = _.get(_.find(dbLoggers, ['key', logger.key]), 'config', {})
  59. await WIKI.models.loggers.query().patch({
  60. config: _.transform(logger.props, (result, value, key) => {
  61. if (!_.has(result, key)) {
  62. _.set(result, key, value.default)
  63. }
  64. return result
  65. }, loggerConfig)
  66. }).where('key', logger.key)
  67. }
  68. }
  69. if (newLoggers.length > 0) {
  70. trx = await WIKI.models.Objection.transaction.start(WIKI.models.knex)
  71. for (let logger of newLoggers) {
  72. await WIKI.models.loggers.query(trx).insert(logger)
  73. }
  74. await trx.commit()
  75. WIKI.logger.info(`Loaded ${newLoggers.length} new loggers: [ OK ]`)
  76. } else {
  77. WIKI.logger.info(`No new loggers found: [ SKIPPED ]`)
  78. }
  79. } catch (err) {
  80. WIKI.logger.error(`Failed to scan or load new loggers: [ FAILED ]`)
  81. WIKI.logger.error(err)
  82. if (trx) {
  83. trx.rollback()
  84. }
  85. }
  86. }
  87. static async pageEvent({ event, page }) {
  88. const loggers = await WIKI.models.storage.query().where('isEnabled', true)
  89. if (loggers && loggers.length > 0) {
  90. _.forEach(loggers, logger => {
  91. WIKI.queue.job.syncStorage.add({
  92. event,
  93. logger,
  94. page
  95. }, {
  96. removeOnComplete: true
  97. })
  98. })
  99. }
  100. }
  101. }