Skip to content

Instantly share code, notes, and snippets.

@HugoRoca
Last active April 29, 2019 21:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save HugoRoca/5de5df9fe26d4c659ba4bce8bd44a89e to your computer and use it in GitHub Desktop.
Save HugoRoca/5de5df9fe26d4c659ba4bce8bd44a89e to your computer and use it in GitHub Desktop.
/*
logger: {
connectionString: "mongodb+srv://usrmongotdapp:Mongo2018@personalizacionqas-xfhrx.mongodb.net",
database: "StreamLog",
collection: "Logs",
defaultSource: "lb.estrategia",
enableLogInfo: true,
enableLogError: true
}
*/
const config = require("../../config");
const mongoClient = require("mongodb").MongoClient;
module.exports = class Logger{
getConnetionString() {
return config.logger.connectionString;
}
getDataBaseString() {
return config.logger.database;
}
getCollection() {
return config.logger.collection;
}
getDefaultSource() {
return config.logger.defaultSource;
}
logErrorIsEnabled() {
return config.logger.enableLogError || true;
}
logInfoIsEnabled() {
return config.logger.enableLogInfo || true;
}
getLogEntry(level, campaign, cuv, personalizationType, params, exception){
let source = this.getDefaultSource();
return {
source,
createdAt: new Date(),
countryCode: config.paisActual,
level: level || "info", //level | error
campaign: campaign || "",
cuv: cuv || "",
personalizationType: personalizationType || "",
params: params,
exception: level === "error" ? exception : null
};
}
async log(data){
return new Promise((resolve, reject) =>{
data.createdAt = new Date();
data.countryCode = config.paisActual;
mongoClient.connect(this.getConnetionString(), { useNewUrlParser: true }).then(client => {
const db = client.db(this.getDataBaseString());
const collection = db.collection(this.getCollection());
collection.insertOne(data, (insertErr, insertResponse) => {
if (insertErr) reject(insertErr);
resolve(insertResponse);
});
}).catch(err => {
console.error("Error al conectarse con la base de datos: ", err);
});
});
}
async logBulk(dataArray){
return new Promise((resolve, reject) =>{
mongoClient.connect(this.getConnetionString(), { useNewUrlParser: true }).then(client => {
const db = client.db(this.getDataBaseString());
const collection = db.collection(this.getCollection());
let options =
{
writeConcern: {w: 0, j:false},
ordered: false
};
collection.insertMany(dataArray, options, (insertErr, insertResponse) => {
if (insertErr) reject(insertErr);
resolve(insertResponse);
});
}).catch(err => {
console.error("Error al conectarse con la base de datos: ", err);
});
});
}
async info(campaign, cuv, personalizationType, params=null){
if(this.logInfoIsEnabled()){
let data = this.getLogEntry("info", campaign, cuv, personalizationType, params);
return await this.log(data);
}
}
async error(exceptionMessage, campaign=null, cuv=null, personalizationType=null, params=null){
if(this.logErrorIsEnabled()){
let data = this.getLogEntry("error", campaign, cuv, personalizationType, params, exceptionMessage);
return await this.log(data);
}
}
async infoBulk(array){
if(this.logInfoIsEnabled()){
let date = new Date();
let source = this.getDefaultSource();
let country = config.paisActual;
for (let index = 0; index < array.length; index++) {
array[index].source = source;
array[index].createdAt = date;
array[index].countryCode = country;
array[index].level = "info";
}
return await this.logBulk(array);
}
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment