You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

68 lines
1.6 KiB

5 years ago
/*!
* lib/logger.js
* Copyright © 2019 Katana Cryptographic Ltd. All Rights Reserved.
*/
'use strict'
const util = require('./util')
/**
* Class providing static methods for logging
*/
class Logger {
/**
* Log a message in the console
* @param {string/object} msg
* @param {boolean} json - true if msg is a json object, false otherwise
*/
static info(msg, json) {
const logEntry = Logger._formatLog(msg, json)
console.log(logEntry)
}
/**
* Log an error message
* @param {object} e - error
* @param {string} msg - message associated to the error
*/
static error(e, msg) {
const logEntry = Logger._formatLog(msg)
console.error(logEntry)
//const errorEntry = Logger._formatLog(e)
if (e) {
console.error(e)
}
}
/**
* Format log entry
* @param {string/object} msg
* @param {boolean} json - true if msg is a json object, false otherwise
*/
static _formatLog(msg, json) {
json = json || false
const data = json ? JSON.stringify(msg, null, 2) : msg
const memUse = process.memoryUsage()
const mib = util.pad100(util.toMb(memUse.rss))
const D = new Date()
const y = D.getUTCFullYear()
const m = util.pad10(D.getUTCMonth() + 1)
const d = util.pad10(D.getUTCDate())
const h = util.pad10(D.getUTCHours())
const mn = util.pad10(D.getUTCMinutes())
const s = util.pad10(D.getUTCSeconds())
const ms = util.pad100(D.getUTCMilliseconds())
const parts = ['[', y, m, d, ' ', h, ':', mn, ':', s, '.', ms, ' ', mib, ' MiB', '] ', data]
return parts.join('')
}
}
module.exports = Logger