Skip to content
Snippets Groups Projects
Select Git revision
  • 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
21 results

index.js

Blame
  • index.js 3.27 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.info = function (message, tags) {
        if (this.loglevel >= this.levels.INFO) {
          return this.log('INFO', message, tags)
        } else {
          return ''
        }
      }
      this.notice = function (message, tags) {
        if (this.loglevel >= this.levels.NOTICE) {
          return this.log('NOTICE', message, tags)
        } else {
          return ''
        }
      }
      this.fatal = function (message, tags) {
        if (this.loglevel >= this.levels.FATAL) {
          return this.log('FATAL', message, tags)
        } else {
          return ''
        }
      }
      this.warn = function (message, tags) {
        if (this.loglevel >= this.levels.WARN) {
          return this.log('WARN', message, tags)
        } else {
          return ''
        }
      }
      this.error = function (message, tags) {
        if (this.loglevel >= this.levels.ERROR) {
          return this.log('ERROR', message, tags)
        } else {
          return ''
        }
      }
      this.debug = function (message, tags) {
        if (this.loglevel >= this.levels.DEBUG) {
          return this.log('DEBUG', message, tags)
        } else {
          return ''
        }
      }
      this.log = function (tag, message, tags) {
        let tadd = ''
        if (typeof tags !== 'undefined' && typeof tags === 'string') {
          if (Array.isArray(tags)) {
            tadd = ' #' + tags.join(' #')
          } else if (tags.includes(' ')) {
            tadd = ' #' + tags.split(' ').join(' #')
          } else if (tags.includes(',')) {
            tadd = ' #' + tags.split(',').join(' #')
          } else {
            tadd = ' #' + tags
          }
        } else { tadd = tags}
        
        let msg = this.getDate() + '\t' + this.hostname + '\t' + this.name + '\t' + tag + '\t' + message + tadd
        switch (tag) {
          case 'INFO':
            console.info(msg)
            break
          case 'NOTICE':
            console.log(msg)
            break
          case 'WARN':
            console.warn(msg)
            break
          case 'ERROR':
            console.error(msg)
            break
          case 'FATAL':
            console.error(msg)
            break
          case 'DEBUG':
            console.log(msg)
            break
          default:
            console.log(msg)
        }
        if (this.options.file) {
          this.fs.appendFile(this.options.file, msg + '\n', function (error) {
            if (error) {
              console.error('Cannot write to File ' + this.options.file)
            }
          })
        }
        return msg
      }
      this.getDate = function () {
        var tzoffset = (new Date()).getTimezoneOffset() * 60000 // offset in milliseconds
        var localISOTime = (new Date(Date.now() - tzoffset)).toISOString().slice(0, -1)
        return localISOTime.split('.')[0].trim()
      }
      return this
    }
    module.exports = Log