mirror of
https://github.com/Permissionless-Software-Foundation/psf-bch-api-base.git
synced 2026-09-21 16:52:00 -07:00
80 lines
2.0 KiB
JavaScript
80 lines
2.0 KiB
JavaScript
/*
|
|||
|
|
Instantiates and configures the Winston logging library. This utility library
|
||
|
|
can be called by other parts of the application to conveniently tap into the
|
||
|
|
logging library.
|
||
|
|
*/
|
||
|
|
|
||
|
|
// Global npm libraries
|
||
|
|
import winston from 'winston'
|
||
|
|
import 'winston-daily-rotate-file'
|
||
|
|
|
||
|
|
// Local libraries
|
||
|
|
import config from '../config/index.js'
|
||
|
|
|
||
|
|
// Hack to get __dirname back.
|
||
|
|
// https://blog.logrocket.com/alternatives-dirname-node-js-es-modules/
|
||
|
|
import * as url from 'url'
|
||
|
|
const __dirname = url.fileURLToPath(new URL('.', import.meta.url))
|
||
|
|
|
||
|
|
let _this = null
|
||
|
|
|
||
|
|
class Wlogger {
|
||
|
|
constructor (localConfig = {}) {
|
||
|
|
this.config = config
|
||
|
|
|
||
|
|
// Configure daily-rotation transport.
|
||
|
|
this.transport = new winston.transports.DailyRotateFile({
|
||
|
|
filename: `${__dirname.toString()}/../../logs/rest2nostr-${
|
||
|
|
this.config.env
|
||
|
|
}-%DATE%.log`,
|
||
|
|
datePattern: 'YYYY-MM-DD',
|
||
|
|
zippedArchive: false,
|
||
|
|
maxSize: '1m', // 1 megabyte
|
||
|
|
maxFiles: '5d', // 5 days
|
||
|
|
format: winston.format.combine(
|
||
|
|
winston.format.timestamp(),
|
||
|
|
winston.format.json()
|
||
|
|
)
|
||
|
|
})
|
||
|
|
|
||
|
|
this.transport.on('rotate', this.notifyRotation)
|
||
|
|
|
||
|
|
// This controls what goes into the log FILES
|
||
|
|
this.wlogger = winston.createLogger({
|
||
|
|
level: this.config.logLevel || 'info',
|
||
|
|
format: winston.format.json(),
|
||
|
|
transports: [
|
||
|
|
this.transport
|
||
|
|
]
|
||
|
|
})
|
||
|
|
|
||
|
|
// Bind 'this' object to all methods
|
||
|
|
this.notifyRotation = this.notifyRotation.bind(this)
|
||
|
|
this.outputToConsole = this.outputToConsole.bind(this)
|
||
|
|
|
||
|
|
_this = this
|
||
|
|
}
|
||
|
|
|
||
|
|
notifyRotation (oldFilename, newFilename) {
|
||
|
|
_this.wlogger.info('Rotating log files')
|
||
|
|
}
|
||
|
|
|
||
|
|
outputToConsole () {
|
||
|
|
this.wlogger.add(
|
||
|
|
new winston.transports.Console({
|
||
|
|
format: winston.format.simple(),
|
||
|
|
level: this.config.logLevel || 'info'
|
||
|
|
})
|
||
|
|
)
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
const logger = new Wlogger()
|
||
|
|
|
||
|
|
// Allow the logger to write to the console.
|
||
|
|
logger.outputToConsole()
|
||
|
|
|
||
|
|
const wlogger = logger.wlogger
|
||
|
|
|
||
|
|
export { wlogger as default, Wlogger }
|