@coya/logs
Advanced tools
Comparing version
88
logs.js
const colors = require('colors'); | ||
const fs = require('fs'); | ||
const mongo = require('mongodb').MongoClient; | ||
const Database = require('@coya/database'); | ||
/* Log levels (syslog) */ | ||
@@ -36,4 +35,2 @@ const EMERGENCY = 0; // system is unusable | ||
if(config.logsCollectionName) { | ||
this.promisesQueue = []; | ||
this.collectionIsSelected = false; | ||
this.error = db.bind(this, 'error'); | ||
@@ -45,12 +42,13 @@ this.warning = db.bind(this, 'warning'); | ||
const configCopy = Object.assign({}, config); | ||
configCopy.logsCollectionName = null; | ||
Database.connect(configCopy) | ||
.then(Database.selectCollection.bind(null, config.logsCollectionName)) | ||
this.actionsQueue = []; | ||
this.collection = null; | ||
selectCollection.call(this) | ||
.then(() => { | ||
this.collectionIsSelected = true; | ||
this.promisesQueue.forEach((promise) => { | ||
this.actionsQueue.forEach((promise) => { | ||
promise(); | ||
}); | ||
this.promisesQueue = []; | ||
this.actionsQueue = null; // not used anymore | ||
}) | ||
.catch((err) => { | ||
throw err; | ||
}); | ||
@@ -85,14 +83,14 @@ } | ||
getLogs(limit) { | ||
getLogs(limit = 0) { | ||
return new Promise((resolve, reject) => { | ||
if(this.executionMode != PROD_MODE) | ||
return reject('No logs.'); | ||
if(this.config.executionMode != PROD_MODE) | ||
return reject('No logs'); | ||
if(this.config.logsCollectionName) { | ||
if(!this.collectionIsSelected) | ||
return reject('Collection not selected.'); | ||
Database.getCursor(config.jobsCollection, query).sort({date: -1}).limit(limit) | ||
.then(resolve) | ||
.catch(reject); | ||
if(this.collection) | ||
this.collection.find().sort({date: -1}).limit(limit).toArray() | ||
.then(resolve) | ||
.catch(reject); | ||
else | ||
reject('Collection not selected.'); | ||
} | ||
@@ -113,14 +111,17 @@ else if(this.logsFile) { | ||
clear() { | ||
if(this.executionMode == PROD_MODE) { | ||
if(this.config.logsCollectionName) { | ||
if(this.collectionIsSelected) | ||
Database.deleteDocsByPattern(this.config.logsCollectionName, {}); | ||
else | ||
this.promisesQueue.push(Database.deleteDocsByPattern.bind(null, this.config.logsCollectionName, {})); | ||
if(this.config.executionMode != PROD_MODE) | ||
return; | ||
if(this.config.logsCollectionName) { | ||
if(this.collection) | ||
this.collection.drop(); | ||
else { | ||
const fct = () => { this.collection.drop() }; | ||
this.actionsQueue.push(fct.bind(this)); | ||
} | ||
if(this.logsFile) { | ||
fs.closeSync(this.logsFile); | ||
this.logsFile = fs.openSync(this.config.logsFilePath, 'w'); | ||
} | ||
} | ||
if(this.logsFile) { | ||
fs.closeSync(this.logsFile); | ||
this.logsFile = fs.openSync(this.config.logsFilePath, 'w'); | ||
} | ||
} | ||
@@ -145,6 +146,8 @@ }; | ||
const doc = {date: Date.now(), level: level, module: this.name, content: msg}; | ||
if(this.collectionIsSelected) | ||
Database.addDocs(this.config.logsCollectionName, doc); | ||
else | ||
this.promisesQueue.push(Database.addDocs.bind(null, this.config.logsCollectionName, doc)); | ||
if(this.collection) | ||
this.collection.insertOne(doc); | ||
else { | ||
const fct = () => { this.collection.insertOne(doc) }; | ||
this.actionsQueue.push(fct.bind(this)); | ||
} | ||
} | ||
@@ -158,2 +161,19 @@ | ||
return '[' + new Date().toLocaleString() + '] [' + name + '] ' + msg; | ||
} | ||
function selectCollection() { | ||
const database = this.config.dbName || this.config.database || this.config.db || this.config.databaseName; | ||
const login = this.config.dbLogin || this.config.login; | ||
const password = this.config.dbPassword || this.config.password || this.config.pw; | ||
if(!database || !login || !password) | ||
return Promise.reject(new Error('Invalid database configuration object.')); | ||
return mongo.connect('mongodb://' + login + ':' + password + '@localhost:27017/' + database) | ||
.then((database) => { | ||
return database.collection(this.config.logsCollectionName); | ||
}) | ||
.then((collection) => { | ||
this.collection = collection; | ||
}); | ||
} |
{ | ||
"name": "@coya/logs", | ||
"version": "0.1.4", | ||
"version": "0.1.5", | ||
"description": "Logs service", | ||
@@ -12,5 +12,5 @@ "main": "logs.js", | ||
"dependencies": { | ||
"@coya/database": "0.0.16", | ||
"colors": "^1.1.2" | ||
"colors": "^1.1.2", | ||
"mongodb": "^2.2.33" | ||
} | ||
} |
5388
8.37%152
13.43%+ Added
- Removed
- Removed