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.

113 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. }
  23. }
  24. }
  25. static get jsonAttributes() {
  26. return ['config']
  27. }
  28. static async getLoggers() {
  29. return WIKI.models.loggers.query()
  30. }
  31. static async refreshLoggersFromDisk() {
  32. let trx
  33. try {
  34. const dbLoggers = await WIKI.models.loggers.query()
  35. // -> Fetch definitions from disk
  36. const loggersDirs = await fs.readdir(path.join(WIKI.SERVERPATH, 'modules/logging'))
  37. let diskLoggers = []
  38. for (let dir of loggersDirs) {
  39. const def = await fs.readFile(path.join(WIKI.SERVERPATH, 'modules/logging', dir, 'definition.yml'), 'utf8')
  40. diskLoggers.push(yaml.safeLoad(def))
  41. }
  42. WIKI.data.loggers = diskLoggers.map(logger => ({
  43. ...logger,
  44. props: commonHelper.parseModuleProps(logger.props)
  45. }))
  46. // -> Insert new loggers
  47. let newLoggers = []
  48. for (let logger of WIKI.data.loggers) {
  49. if (!_.some(dbLoggers, ['key', logger.key])) {
  50. newLoggers.push({
  51. key: logger.key,
  52. isEnabled: (logger.key === 'console'),
  53. level: logger.defaultLevel,
  54. config: _.transform(logger.props, (result, value, key) => {
  55. _.set(result, key, value.default)
  56. return result
  57. }, {})
  58. })
  59. } else {
  60. const loggerConfig = _.get(_.find(dbLoggers, ['key', logger.key]), 'config', {})
  61. await WIKI.models.loggers.query().patch({
  62. config: _.transform(logger.props, (result, value, key) => {
  63. if (!_.has(result, key)) {
  64. _.set(result, key, value.default)
  65. }
  66. return result
  67. }, loggerConfig)
  68. }).where('key', logger.key)
  69. }
  70. }
  71. if (newLoggers.length > 0) {
  72. trx = await WIKI.models.Objection.transaction.start(WIKI.models.knex)
  73. for (let logger of newLoggers) {
  74. await WIKI.models.loggers.query(trx).insert(logger)
  75. }
  76. await trx.commit()
  77. WIKI.logger.info(`Loaded ${newLoggers.length} new loggers: [ OK ]`)
  78. } else {
  79. WIKI.logger.info(`No new loggers found: [ SKIPPED ]`)
  80. }
  81. } catch (err) {
  82. WIKI.logger.error(`Failed to scan or load new loggers: [ FAILED ]`)
  83. WIKI.logger.error(err)
  84. if (trx) {
  85. trx.rollback()
  86. }
  87. }
  88. }
  89. static async pageEvent({ event, page }) {
  90. const loggers = await WIKI.models.storage.query().where('isEnabled', true)
  91. if (loggers && loggers.length > 0) {
  92. _.forEach(loggers, logger => {
  93. WIKI.queue.job.syncStorage.add({
  94. event,
  95. logger,
  96. page
  97. }, {
  98. removeOnComplete: true
  99. })
  100. })
  101. }
  102. }
  103. }