onions-node
Version:
onions-node backend
42 lines (31 loc) • 875 B
JavaScript
const {logger} = require('@razor-node/core')
const path = require('path')
const fs = require('fs')
module.exports = (app, loggerOptions) => {
const logPath = loggerOptions.logDir
const pathArray = logPath.split('/')
if (!path.isAbsolute(logPath)) {
throw new Error('logPath need by a absolute path!')
}
const mkdir = (p) => {
if (!fs.existsSync(p)) {
fs.mkdirSync(p)
}
}
const recursiveMkLogPath = (parent = '', index = 0) => {
const current = pathArray[index]
const p = path.join(parent, current)
if (current) {
mkdir(p)
}
if (index >= pathArray.length - 1) {
return
}
recursiveMkLogPath(p, index+1)
}
recursiveMkLogPath()
const clsLogger = logger.getClsLoggerInstance(loggerOptions)
app.use(logger.clsLoggerMiddleware(loggerOptions))
global.logger = clsLogger
return clsLogger
}