how to store logs into mysql db using winston in Node.js - mysql

I am doing logging for my application using winston. I have done the file transport using this:
class LoggerHelper extends BaseHelper {
constructor(_cApp) {
super(_cApp);
this._props = {};
}
initialize() {
this._prepareConfigs();
this._createTransportObj();
this._createLoggerObj();
}
_prepareConfigs() {
this._props.dirname = this._configs.logsFolder;
this._props.filename = this._configs.filenameConvention;
this._props.datePattern = this._configs.datePattern;
this._props.maxSize = this._configs.maxSize;
this._props.level = this._configs.level;
this._props.timestamp = this._configs.timestamp;
this._props.prettyPrint = this._configs.prettyPrint;
}
_createTransportObj() {
var DailyRotateFile = winston.transports.DailyRotateFile;
this._transport = new DailyRotateFile(this._props);
}
_createLoggerObj() {
this._logger = winston.createLogger({
transports: [this._transport],
exitOnError: false
});
}
_log(type, error, description, stage, vars) {
var logMsg = {};
var msg = '';
var fileIndex = 3;
if(this._isError(error)) {
var err = error;
msg = error.message;
fileIndex = 1;
} else {
var err = new Error();
msg = error;
}
var caller_line = err.stack.split("at ")[fileIndex];
var index = caller_line.indexOf("(");
var lastIndex = caller_line.lastIndexOf(")");
index = caller_line.slice(index + 1, lastIndex);
var line = index.match(/:[0-9]+:/).toLocaleString();
line = line.replace(/[^0-9]/g, '');
var curTime = new FE.utils.date();
var timestamp = curTime.format('YYYY-MM-DD HH:MM:SS');
logMsg.level = type || 'info';
logMsg.time = timestamp || '';
logMsg.msg = msg || '';
logMsg.desc = description || '';
logMsg.stg = stage || '000';
logMsg.file = index || 'Not Found';
logMsg.stack = err.stack || 'Not Found';
logMsg.line = line || 'Not Found';
var logStr = JSON.stringify(logMsg);
this._logger.log(type, logMsg);
}
info(error, description, stage, vars) {
return this._log('info', error, description, stage, vars);
}
error(error, description, stage, vars) {
return this._log('error', error, description, stage, vars);
}
warn(error, description, stage, vars) {
return this._log('warn', error, description, stage, vars);
}
verbose(error, description, stage, vars) {
return this._log('verbose', error, description, stage, vars);
}
debug(error, description, stage, vars) {
return this._log('debug', error, description, stage, vars);
}
silly(error, description, stage, vars) {
return this._log('silly', error, description, stage, vars);
}
/**
* Checks if value is an Error or Error-like object
* #static
* #param {Any} val Value to test
* #return {Boolean} Whether the value is an Error or Error-like object
*/
_isError(val) {
return !!val && typeof val === 'object' && (
val instanceof Error || (
val.hasOwnProperty('message') && val.hasOwnProperty('stack')
)
);
}
}
module.exports = LoggerHelper;
Now I want to store my logs into a mysql db table as well. I did come across a winston plugin for Mongo but I don't see any support for storing it into a mysql db. Is there a way I can achieve this?
Thanks in advance.

I was facing the same issue recently. after doing some research I found one package 'winston-sql-transport' to save logs into mysql.
see this:
const { Logger } = require('winston');
const { SQLTransport } = require('./../lib/winston-sql-transport');
const logger = new Logger({
transports: [
new SQLTransport({
tableName: 'winston_logs',
})]
});
module.exports = logger;

Related

How to get the selected equipment object id

we have used forge Aggregate Viewer to display the multiple BIM models. But if we click/double click any of the equipment in the Aggregate Forge Viewer the equipment will be zoomed.
but not able to get the selected equipment object id by using the c# code.
Note: If we upload the single file, we are able to get the selected equipment object id in the Forge Viewer.
We used below code, but its not get hitted when we select equipment.
viewer.addEventListener(Autodesk.Viewing.SELECTION_CHANGED_EVENT, (args) => {
Kindly help us to get the selected equipment object id in Aggregate Forge View while displaying the multiple files.
Kindly share the sample code for our reference.
FITTOVIEW AND SELECTION CHANGE CODE
const Loadedevent = () => {
var objval = document.getElementById('<%=hid_objectid.ClientID%>').value;
if (objval != '') {
var mdlurn = "";
mdlurn = document.getElementById('<%=hid_mdlurn.ClientID%>').value;
const models = viewer.getVisibleModels().find(m => m.getData().urn === mdlurn);
viewer.fitToView([parseInt(objval)], models);
viewer.select([parseInt(objval)], models, Autodesk.Viewing.SelectionType.OVERLAYED);
}
}
SelectionChangeEvent
viewer.addEventListener(Autodesk.Viewing.AGGREGATE_SELECTION_CHANGED_EVENT, (args) => {
if (args.dbIdArray.length === 1) {
viewer.getProperties(args.dbIdArray[0], function (data) {
if (FromPage == '') {
if (FromSelection == '') {
if (Count === 0) {
var instanceTree = viewer.model.getData().instanceTree;
var parentId = instanceTree.getNodeParentId(args.dbIdArray[0]);
viewer.select([parentId], viewer.model, Autodesk.Viewing.SelectionType.OVERLAYED);
Count = 1;
} else {
itemobject = args.dbIdArray[0];
Count = 0;
}
} else {
FromSelection = '';
itemobject = args.dbIdArray[0];
}
} else {
FromPage = '';
itemobject = args.dbIdArray[0];
}
});
}
});
Edited Code:
viewer.addEventListener(Autodesk.Viewing.AGGREGATE_SELECTION_CHANGED_EVENT, (args) => {
if (!args.selections || args.selections.length <= 0)
return;
if (args.selections.length == 1 ) {
viewer.getProperties(args.selections[0].dbIdArray[0], function (data) {
if (FromPage == '') {
if (FromSelection == '') {
if (Count === 0) {
var instanceTree = viewer.model.getData().instanceTree;
var parentId = instanceTree.getNodeParentId(args.selections[0].dbIdArray);
viewer.select([parentId], viewer.model, Autodesk.Viewing.SelectionType.OVERLAYED);
Count = 1;
} else {
const dbIds = args.selections[0].dbIdArray;
itemobject = dbIds[0];
Count = 0;
}
} else {
FromSelection = '';
const dbIds = args.selections[0].dbIdArray;
itemobject = dbIds[0];
}
} else {
FromPage = '';
const dbIds = args.selections[0].dbIdArray;
itemobject = dbIds[0];
}
});
}
});
In the multiple models scenario, you must use Autodesk.Viewing.AGGREGATE_SELECTION_CHANGED_EVENT instead. See https://forge.autodesk.com/blog/multi-model-refresher
/**
* Triggered when objects are selected or deselected.
* Event payload:
* {
* type: 'aggregateSelection',
* target: Viewer3D,
* selections: Array<{
* model: Model,
* nodeArray: Array<number>,
* dbIdArray: Array<number>,
* fragIdsArray: Array<number>
* }>
* }
*/
viewer.addEventListener(
Autodesk.Viewing.AGGREGATE_SELECTION_CHANGED_EVENT,
(event) => {
// codes for selecting changed
});
To get selected objects, call viewer.getAggregateSelection() instead.
Update
Here are the revised code snippets for you:
FITTOVIEW AND SELECTION CHANGE CODE
const Loadedevent = () => {
let objval = document.getElementById('<%=hid_objectid.ClientID%>').value;
if (objval != '') {
let mdlurn = "";
mdlurn = document.getElementById('<%=hid_mdlurn.ClientID%>').value;
let model = viewer.getVisibleModels().find(m => m.getData().urn === mdlurn); //!<<< find() will return first found item only, so defining the variable in the singular form instead to avoid confusion.
let dbId = parseInt( objval );
viewer.select( [ dbId ] , model, Autodesk.Viewing.SelectionType.OVERLAYED );
// or viewer.setAggregateSelection([ { ids: [ dbId ], model, selectionType: Autodesk.Viewing.SelectionType.OVERLAYED } ]);
viewer.fitToView( [ dbId ], model );
}
}
SelectionChangeEvent
viewer.addEventListener(Autodesk.Viewing.AGGREGATE_SELECTION_CHANGED_EVENT, (event) => {
if (!event.selections || event.selections.length <= 0 || event.selections.length > 1)
return;
let selSet = event.selections[0];
const dbIds = selSet.dbIdArray;
const model = selSet.model;
const dbId = dbIds[0];
model.getProperties(dbId, function(data) {
if (FromPage == '') {
if (FromSelection == '') {
if (Count === 0) {
let instanceTree = model.getInstanceTree();
let parentId = instanceTree.getNodeParentId(dbId);
viewer.select(parentId, model, Autodesk.Viewing.SelectionType.OVERLAYED);
// or viewer.setAggregateSelection([ { ids: [parentId ], model, selectionType: Autodesk.Viewing.SelectionType.OVERLAYED } ])
Count = 1;
} else {
itemobject = dbId;
Count = 0;
}
} else {
FromSelection = '';
itemobject = dbId;
}
} else {
FromPage = '';
itemobject = dbId;
}
});
});

Angular: 2 bugs in saving to local storage

I have 2 bugs in methods that save products to local storage (when a user adds them to 'favorites').
The code is part of Angular service, but it can be read without this reference also.
First bug: Sometimes, when a user saves a product to favorites, not one, but multiple products get stored.
Second bug: When I store too many products, I get an error saying that the storage is full (it's full because I parse JSON wrongly, so unwanted additional backslashes are added).
This is the codepan (you can see the result in the console): https://codepen.io/mandy555/pen/dyPVGxj
storage;
storageKeys;
x;
i = 0;
saveId;
arr;
normalArr;
productInStore;
productsInStore;
idsInStore;
newIds;
newProducts;
favorites;
storageAvailable(type) {
try {
this.storage = window[type];
this.x = '__storage_test__';
this.storage.setItem('x', this.x);
this.storage.removeItem('x');
return true;
}
catch (e) {
return e instanceof DOMException && (
// everything except Firefox
e.code === 22 ||
// Firefox
e.code === 1014 ||
// test name field too, because code might not be present
// everything except Firefox
e.name === 'QuotaExceededError' ||
// Firefox
e.name === 'NS_ERROR_DOM_QUOTA_REACHED') &&
// acknowledge QuotaExceededError only if there's something already stored
(this.storage && this.storage.length !== 0);
}
}
storeFavorite(product = '', productId = '') {
productId = String(productId);
this.normalArr = [];
if (this.storageAvailable('localStorage')) {
this.storage = window.localStorage;
if (!this.storage.getItem('favorites_id')) {
this.saveId = productId;
this.normalArr.push(product);
} else {
this.productsInStore = JSON.parse(this.storage.getItem('favorites_product'));
if (this.storage.getItem('favorites_id').indexOf(',') === -1) {
this.arr = [this.storage.getItem('favorites_id')];
this.normalArr = this.productsInStore;
} else {
this.arr = this.storage.getItem('favorites_id').split(',');
this.normalArr = this.productsInStore;
}
if (!this.arr.includes(productId) && productId !== '') {
this.saveId = this.storage.getItem('favorites_id') + ',' + productId;
this.normalArr.push(product);
} else {
this.saveId = this.storage.getItem('favorites_id');
}
}
this.storage.setItem('favorites_id', this.saveId);
this.storage.setItem('favorites_product', JSON.stringify(this.normalArr));
} else {
this.storage = null;
}
}
deleteFavorite(productId = '') {
productId = String(productId);
this.normalArr = [];
if (this.storageAvailable('localStorage')) {
this.storage = window.localStorage;
if (this.storage.getItem('favorites_id') && this.storage.getItem('favorites_product')) {
this.productsInStore = JSON.parse(this.storage.getItem('favorites_product'));
if (this.storage.getItem('favorites_id').indexOf(',') === -1) {
this.storage.clear();
} else {
this.idsInStore = this.storage.getItem('favorites_id').split(',');
this.newIds = this.idsInStore.filter(id => {
return id !== productId;
});
this.newproducts = this.productsInStore.filter(product => {
return String(product.id) !== productId;
});
this.newIds = this.newIds.join();
this.storage.setItem('favorites_id', JSON.stringify(this.newIds));
this.storage.setItem('favorites_product', JSON.stringify(this.newproducts));
}
}
} else {
this.storage = null;
}
}
The first problem was that I forgot to assign storage to local storage. The second solution was to save x and not JSON.stringify(x) in the local storage, since x was already a string. The corrected version is here:
https://codepen.io/mandy555/pen/dyPVGxj

Haxe IE9 xmlHTTPrequest issue

I'm having problems displaying my Haxe game in IE9. It's actually not displaying at all. We tracked down the issue inside the compiled JS file for Haxe and found out that the problem is within the haxe.HTTP API.
There are certain things that need to be checked and done for IE9 to work with xmlhttprequests. These things were not done in the Haxe API.
This is the http class without my fix:
this.url = url;
this.headers = new List();
this.params = new List();
this.async = true;
};
$hxClasses["haxe.Http"] = haxe.Http;
haxe.Http.__name__ = ["haxe","Http"];
haxe.Http.prototype = {
setParameter: function(param,value) {
this.params = Lambda.filter(this.params,function(p) {
return p.param != param;
});
this.params.push({ param : param, value : value});
return this;
}
,request: function(post) {
var me = this;
me.responseData = null;
var r = this.req = js.Browser.createXMLHttpRequest();
var onreadystatechange = function(_) {
if(r.readyState != 4) return;
var s;
try {
s = r.status;
} catch( e ) {
s = null;
}
if(s == undefined) s = null;
if(s != null) me.onStatus(s);
if(s != null && s >= 200 && s < 400) {
me.req = null;
me.onData(me.responseData = r.responseText);
} else if(s == null) {
me.req = null;
me.onError("Failed to connect or resolve host");
} else switch(s) {
case 12029:
me.req = null;
me.onError("Failed to connect to host");
break;
case 12007:
me.req = null;
me.onError("Unknown host");
break;
default:
me.req = null;
me.responseData = r.responseText;
me.onError("Http Error #" + r.status);
}
};
if(this.async) r.onreadystatechange = onreadystatechange;
var uri = this.postData;
if(uri != null) post = true; else {
var $it0 = this.params.iterator();
while( $it0.hasNext() ) {
var p = $it0.next();
if(uri == null) uri = ""; else uri += "&";
uri += encodeURIComponent(p.param) + "=" + encodeURIComponent(p.value);
}
}
try {
if(post) r.open("POST",this.url,this.async); else if(uri != null) {
var question = this.url.split("?").length <= 1;
r.open("GET",this.url + (question?"?":"&") + uri,this.async);
uri = null;
} else r.open("GET",this.url,this.async);
} catch( e1 ) {
me.req = null;
this.onError(e1.toString());
return;
}
if(!Lambda.exists(this.headers,function(h) {
return h.header == "Content-Type";
}) && post && this.postData == null) r.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
var $it1 = this.headers.iterator();
while( $it1.hasNext() ) {
var h1 = $it1.next();
r.setRequestHeader(h1.header,h1.value);
}
r.send(uri);
if(!this.async) onreadystatechange(null);
}
,onData: function(data) {
}
,onError: function(msg) {
}
,onStatus: function(status) {
}
,__class__: haxe.Http
};
and this is the code WITH the fix:
haxe.Http = function(url) {
this.url = url;
this.headers = new List();
this.params = new List();
this.async = true;
};
$hxClasses["haxe.Http"] = haxe.Http;
haxe.Http.__name__ = ["haxe","Http"];
haxe.Http.prototype = {
setParameter: function(param,value) {
this.params = Lambda.filter(this.params,function(p) {
return p.param != param;
});
this.params.push({ param : param, value : value});
return this;
}
,request: function(post) {
var me = this;
me.responseData = null;
var r = this.req = js.Browser.createXMLHttpRequest();
var onreadystatechange = function(_) {
console.log('in onreadystatechange function');
//if(r.readyState != 4) return;
console.log(r.responseText);
console.log('r.status: ' + r.status);
me.req = null;
me.onData(me.responseData = r.responseText);
};
if(typeof XDomainRequest != "undefined") {
console.log('XDomainRequest');
r.onload = onreadystatechange;
}
var uri = this.postData;
try {
console.log('calling r.open with url: ' + this.url);
r.open("GET",this.url);
} catch( e1 ) {
me.req = null;
this.onError(e1.toString());
return;
}
//r.send(uri);
//do it, wrapped in timeout to fix ie9
setTimeout(function () {
r.send();
}, 0);
//if(!this.async) onreadystatechange(null);
}
,onData: function(data) {
}
,onError: function(msg) {
}
,onStatus: function(status) {
}
,__class__: haxe.Http
};
Note that this only implements the IE9 fix without doing the if statements to keep support for other browsers. But it's really easy. the IF statement is simply
if(typeof XDomainRequest != "undefined") return new XDomainRequest();
Basically, I know what the issue is, I just have no idea how I can change these things within the core of Haxe itself.
Thanks.
https://github.com/HaxeFoundation/haxe/pull/3449
BAM! Got it working in a local version of my haxe. Fixed for all browsers and sent a pull request. :D
Good job on this one!
You have to contact the Haxe mailing list directly, most thinking heads are over there rather than here :)
https://groups.google.com/d/forum/haxelang

Convert Url/Path to Json with Node.js

I recently built a little node program able to console.log all the files of a precise path.
The result I get from this function looks like this for instance :
/Volumes/TimeCapsule/movies/movie1
movie1.mp4
/Volumes/TimeCapsule/movies/movie2
movie2.mp4
/Volumes/TimeCapsule/movies/movie3
movie3.mp4
Now my question is: how can I manage to convert each of this path to JSON so I could be able for instance to display all the files of the movie folder in a single html page ?
I would like to have something like this :
{ "Volumes": {
"TimeCapsule": {
"Movies":{
"Title": "Movie1"
"Title": "Movie2"
"Title": "Movie3"
}
}
}
}
Thank you in advance.
By the way here is my walk function :
var fs = require('fs');
var walk = function (currentPath) {
console.log(currentPath);
var files = fs.readdirSync(currentPath); //Returns array of filename in currenpath
for (var i in files) {
var currentFile = currentPath + '/' + files[i];
var stats = fs.statSync(currentFile);
if (stats.isFile()) {
console.log(currentFile.replace(/^.*[\\\/]/, '')););
}
else if (stats.isDirectory()) {
walk(currentFile);
}
}
};
OK, here we go:
var fs = require( "fs" );
function walk( path, arr ) {
var ret = {};
arr = Array.isArray( arr ) ? arr : [];
fs.readdirSync( path ).forEach(function( item ) {
var current = path + "/" + item;
var stats = fs.statSync( current );
if ( stats.isFile() ) {
arr.push( current );
} else if ( stats.isDirectory() ) {
walk( current, arr );
}
});
arr.forEach(function( item ) {
var i, len;
item.split( "/" ).reduce(function( obj, path, i, parts ) {
if ( ( i + 1 ) === parts.length ) {
obj.Title = path;
} else {
obj[ path ] = obj[ path ] || {};
return obj[ path ];
}
}, ret);
});
return ret;
}
this was not tested, but maybe it give you some ideas on how to do it.
Here is what I really wanted, I even added a path section so I can have an access to the path of each single file :
var fs = require('fs'),
path = require('mypath')
function walk(path) {
var stats = fs.lstatSync(mypath),
info = {
path: mypath,
Title: path.basename(mypath)
};
if (stats.isDirectory()) {
info.type = "folder";
info.children = fs.readdirSync(filename).map(function(child) {
return walk(mypath + '/' + child);
});
} else {
info.type = "file";
}
return info;
}
console.log(walk('/Users/maximeheckel/Desktop'));
Thank you for your help.

json stringify in IE 8 gives run time error Object property or method not supported

/* Problem description- I am using json stringify method to convert an javascript array to string in json notation.However I get an error message that 'Object property or method not supported' at line
hidden.value = JSON.stringify(jsonObj);
This should work as stringify is supported in IE8.Please advise
Full code below */
function getgridvalue() {
var exportLicenseId;
var bolGrossQuantity;
var bolNetQuantity;
var totalBolGrossQty =0 ;
var totalBolNetQty =0;
var jsonObj = []; //declare array
var netQtyTextBoxValue = Number(document.getElementById("<%= txtNetQty.ClientID %>").value);
var atLeastOneChecked = false;
var gridview = document.getElementById("<%= ExporterGrid.ClientID %>"); //Grab a reference to the Grid
for (i = 1; i < gridview.rows.length; i++) //Iterate through the rows
{
if (gridview.rows[i].cells[0].getElementsByTagName("input")[0] != null && gridview.rows[i].cells[0].getElementsByTagName("input")[0].type == "checkbox")
{
if (gridview.rows[i].cells[0].getElementsByTagName("input")[0].checked)
{
atLeastOneChecked = true;
exportLicenseId = gridview.rows[i].cells[8].getElementsByTagName("input")[0].value;
bolNetQuantity = gridview.rows[i].cells[5].getElementsByTagName("input")[0].value;
if (bolNetQuantity == "") {
alert('<%= NetQuantityMandatory %>');
return false;
}
if (!isNumber(bolNetQuantity)) {
alert('<%= NetQuantityNumber %>');
return false;
}
totalBolNetQty += Number(bolNetQuantity);
jsonObj.push({ ExportLicenseId: Number(exportLicenseId), BolNetQuantity: Number(bolNetQuantity) });
}
}
}
if (gridview.rows.length > 2 && !atLeastOneChecked)
{
alert('<%= SelectMsg %>');
return false;
}
if (totalBolNetQty != 0 && netQtyTextBoxValue != totalBolNetQty)
{
alert('<%= NetQuantitySum %>');
return false;
}
var hidden = document.getElementById('HTMLHiddenField');
// if (!this.JSON) {
// this.JSON = {};
// }
var JSON = JSON || {};
if (hidden != null) {
hidden.value = JSON.stringify(jsonObj);
}
}
Use the F12 Developer Tools to check the browser mode. The JSON object exists, but has no methods in IE7 mode. Use the json2 library as a fallback.
json2.js