@coya/logs
Advanced tools
Comparing version 0.0.5 to 0.1.0
146
logs.js
@@ -0,4 +1,6 @@ | ||
const colors = require('colors'); | ||
const fs = require('fs'); | ||
const colors = require('colors'); | ||
const Database = require('@coya/database'); | ||
/* Log levels (syslog) */ | ||
@@ -21,46 +23,59 @@ const EMERGENCY = 0; // system is unusable | ||
this.name = name; | ||
this.config = config; | ||
// default values | ||
this.executionMode = DEV_MODE; | ||
this.logsFilePath = null; | ||
this.outputType = 'file'; | ||
this.collectionName = null; | ||
if(config.executionMode == PROD_MODE || config.executionMode == 'prod') | ||
config.executionMode = PROD_MODE; | ||
else if(config.executionMode == DEV_MODE || config.executionMode == 'dev') | ||
config.executionMode = DEV_MODE; | ||
else if(config.executionMode == DEBUG_MODE || config.executionMode == 'debug') | ||
config.executionMode = DEBUG_MODE; | ||
else | ||
config.executionMode = DEV_MODE; | ||
if(config.executionMode == PROD_MODE) { | ||
if(config.logsCollectionName) { | ||
this.docsQueue = []; | ||
this.collectionIsSelected = false; | ||
this.error = db.bind(this, 'error'); | ||
this.warning = db.bind(this, 'warning'); | ||
this.notice = db.bind(this, 'notice'); | ||
this.info = db.bind(this, 'info'); | ||
this.debug = db.bind(this, 'debug'); | ||
if(config.executionMode !== undefined) { | ||
if(config.executionMode == PROD_MODE || config.executionMode == 'prod') | ||
this.executionMode = PROD_MODE; | ||
else if(config.executionMode == DEV_MODE || config.executionMode == 'dev') | ||
this.executionMode = DEV_MODE; | ||
else if(config.executionMode == DEBUG_MODE || config.executionMode == 'debug') | ||
this.executionMode = DEBUG_MODE; | ||
else | ||
throw new Error('Invalid execution mode.'); | ||
const configCopy = Object.assign({}, config); | ||
configCopy.logsCollectionName = null; | ||
Database.connect(configCopy) | ||
.then(Database.selectCollection.bind(null, config.logsCollectionName)) | ||
.then(() => { | ||
this.collectionIsSelected = true; | ||
this.docsQueue.forEach((doc) => { | ||
Database.addDocs(config.logsCollectionName, doc); | ||
}); | ||
this.docsQueue = []; | ||
}); | ||
} | ||
else if(config.logsFilePath) { | ||
this.logsFile = fs.openSync(config.logsFilePath, 'a'); | ||
this.error = file.bind(this, 'red'); | ||
this.warning = file.bind(this, 'yellow'); | ||
this.notice = file.bind(this, 'blue'); | ||
this.info = file.bind(this, 'green'); | ||
this.debug = nothing; | ||
} | ||
else { | ||
this.error = nothing; | ||
this.warning = nothing; | ||
this.notice = nothing; | ||
this.info = nothing; | ||
this.debug = nothing; | ||
} | ||
} | ||
if(config.logsFilePath) { | ||
this.logsFilePath = config.logsFilePath; | ||
this.logsFile = fs.openSync(this.logsFilePath, 'a'); | ||
else { | ||
this.error = error.bind(this, 'red'); | ||
this.warning = log.bind(this, 'yellow'); | ||
this.notice = log.bind(this, 'blue'); | ||
this.info = log.bind(this, 'green'); | ||
this.debug = log.bind(this, 'white'); | ||
} | ||
if(config.outputType) { | ||
if(config.outputType == 'file') | ||
this.outputType = 'file'; | ||
else if(config.outputType == 'database') | ||
this.outputType = 'database'; | ||
else | ||
throw new Error('Invalid output type.'); | ||
} | ||
if(config.collectionName) { | ||
if(this.outputType != 'database') | ||
throw new Error('Invalid parameter "collectionName".'); | ||
this.collectionName = config.collectionName; | ||
} | ||
this.error = (this.executionMode === PROD_MODE && this.logsFilePath) ? errorProd : errorDev; | ||
this.warning = (this.executionMode === PROD_MODE && this.logsFilePath) ? warningProd : warningDev; | ||
this.notice = (this.executionMode === PROD_MODE && this.logsFilePath) ? noticeProd : noticeDev; | ||
this.info = (this.executionMode === PROD_MODE && this.logsFilePath) ? infoProd : infoDev; | ||
this.debug = (this.executionMode === DEBUG_MODE) ? debugDebug : debugDevOrProd; | ||
this.info('Logs handler instantiated.'); | ||
@@ -70,3 +85,3 @@ } | ||
getLogs(callback) { | ||
fs.readFile(this.logsFilePath, function(err, data) { | ||
fs.readFile(this.config.logsFilePath, function(err, data) { | ||
data = '<html><body style=\'display: flex; flex-direction: column-reverse;\'>' + data + '</body></html>'; | ||
@@ -78,5 +93,5 @@ callback(err, data); | ||
clear() { | ||
if(this.logsFilePath) { | ||
if(this.config.logsFilePath) { | ||
fs.closeSync(this.logsFile); | ||
this.logsFile = fs.openSync(this.logsFilePath, 'w'); | ||
this.logsFile = fs.openSync(this.config.logsFilePath, 'w'); | ||
} | ||
@@ -86,46 +101,21 @@ } | ||
function errorDev(msg) { | ||
console.error(formatMessage(msg, this.name).red); | ||
} | ||
function nothing() {} | ||
function errorProd(msg) { | ||
fs.write(this.logsFile, '<div style=\'color: red\'>' + formatMessage(msg, this.name) + '</div>'); | ||
function log(color, msg) { | ||
console.log(formatMessage(msg, this.name)[color]); | ||
} | ||
function warningDev(msg) { | ||
console.log(formatMessage(msg, this.name).yellow); | ||
function error(color, msg) { | ||
console.error(formatMessage(msg, this.name)[color]); | ||
} | ||
function warningProd(msg) { | ||
fs.write(this.logsFile, '<div style=\'color: yellow\'>' + formatMessage(msg, this.name) + '</div>'); | ||
function file(color, msg) { | ||
fs.write(this.logsFile, '<div style="color: ' + color + '">' + formatMessage(msg, this.name) + '</div>'); | ||
} | ||
function noticeDev(msg) { | ||
console.log(formatMessage(msg, this.name).blue); | ||
function db(level, msg) { | ||
const doc = {date: Date.now(), level: level, module: this.name, content: msg}; | ||
this.collectionIsSelected ? Database.addDocs(this.config.logsCollectionName, doc) : this.docsQueue.push(doc); | ||
} | ||
function noticeProd(msg) { | ||
fs.write(this.logsFile, '<div style=\'color: blue\'>' + formatMessage(msg, this.name) + '</div>'); | ||
} | ||
function infoDev(msg) { | ||
console.log(formatMessage(msg, this.name).green); | ||
} | ||
function infoProd(msg) { | ||
fs.write(this.logsFile, '<div style=\'color: green\'>' + formatMessage(msg, this.name) + '</div>'); | ||
} | ||
function debugDevOrProd(msg) { | ||
} | ||
function debugDebug(msg) { | ||
console.log(formatMessage(msg, this.name).white); | ||
} | ||
function file(msg) { | ||
fs.write(this.logsFile, '<div style=\'color: black\'>' + '[' + new Date().toLocaleString() + '] ' + msg + '</div>'); | ||
} | ||
function formatMessage(msg, name) { | ||
@@ -132,0 +122,0 @@ if(msg instanceof Error) |
{ | ||
"name": "@coya/logs", | ||
"version": "0.0.5", | ||
"version": "0.1.0", | ||
"description": "Logs service", | ||
@@ -5,0 +5,0 @@ "main": "logs.js", |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
3
4090
106