Skip to content
Snippets Groups Projects
Select Git revision
  • 9e6f34c60f10b63861ebbd64e5b90c809c131497
  • main default protected
  • 3.1.7
  • 3.1.6
  • 3.1.5
  • 3.1.4
  • 3.1.3
  • 3.1.2
  • 3.1.1
  • 3.1.0
  • 2.14.0
  • 2.13.5
  • 2.13.4
  • 2.13.3
  • 2.13.2
  • 2.13.0
  • 2.12.1
  • 2.12.0
  • 2.11.0
  • 2.10.1
  • 2.10.0
  • 2.9.1
22 results

index.js

Blame
  • index.js 3.50 KiB
    function Log (options) {
      this.levels = {
        'DEBUG': 5,
        'INFO': 4,
        'NOTICE': 3,
        'WARN': 2,
        'ERROR': 1,
        'FATAL': 0
      }
      this.options = options || {}
      if (this.options.loglevel) { // DEBUG, INFO, NOTICE, WARN, ERROR, FATAL
        this.loglevel = this.levels[this.options.loglevel]
      } else {
        this.loglevel = 2
      }
      if (this.options.name) {
        this.name = this.options.name
      } else {
        this.name = require('./package.json').name
      }
      if (this.options.hostname) {
        this.hostname = this.options.hostname
      } else {
        let os = require('os')
        this.hostname = os.hostname()
      }
      if (this.options.file) {
        this.fs = require('fs')
        this.path = require('path')
        try {
          if (!this.fs.existsSync(this.options.path)) this.fs.mkdirSync(this.options.path, { recursive: true })
        } catch (error) {
          throw new Error('Could not create path ' + this.options.path + '\n' + error.toString())
        }
    
        try {
          this.fs.accessSync(this.path.join(this.options.path, this.options.file), this.fs.constants.R_OK | this.fs.constants.W_OK)
        } catch (err) {
          try {
            this.fs.writeFileSync(this.path.join(this.options.path, this.options.file), '', { flag: 'wx' })
          } catch (err) {
            throw new Error('Could not Create File ' + this.options.file + '\n' + err.toString())
          }
        }
      }
      this.info = function (message) {
        if (this.loglevel >= this.levels.INFO) {
          return this.log('INFO', message)
        } else {
          return ''
        }
      }
      this.notice = function (message) {
        if (this.loglevel >= this.levels.NOTICE) {
          return this.log('NOTICE', message)
        } else {
          return ''
        }
      }
      this.fatal = function (message) {
        if (this.loglevel >= this.levels.FATAL) {
          return this.log('FATAL', message)
        } else {
          return ''
        }
      }
      this.warn = function (message) {
        if (this.loglevel >= this.levels.WARN) {
          return this.log('WARN', message)
        } else {