const fs = require('fs')
const path = require('path')
const node_time = require('node-datetime')
const util = require('util')

module.exports = class logger {
    constructor(filepath) {
        this.#filepath = filepath
        this.#filename = path.basename(filepath)
        var dirname = path.dirname(filepath)
        
        if (!fs.existsSync(dirname))
            fs.mkdirSync(dirname, {recursive:true})
    }
    error(...arg) {
        this.#log('error', arg)
    }
    log(...arg) {
        this.#log('', arg)
    }

    #log(message = '', ...arg) {
        var args = ''
        arg.forEach(value => {
            args += util.format(value) + ' '
        })
        args = (node_time.create().format('d-m-Y H:M:S')).toString() + ((message != '') ? ': ' + message : '') + ': ' + args
        console.log(this.#filename + ': ' + args)
        fs.appendFileSync(this.#filepath, args + '\n')
    }
    
    #filepath
    #filename
}