coreybutler / node-windows

Windows support for Node.JS scripts (daemons, eventlog, UAC, etc).
Other
2.81k stars 356 forks source link

timestamp for err.log #259

Open Obscurator opened 4 years ago

Obscurator commented 4 years ago

It would be great if the daemons xx.err.log would have the same timestamps like xx.wrapper.log

mdodge-ecgrow commented 4 years ago

Yes! I was just looking at the error log and was frustrated because I had no idea when those errors occurred.

Shriram-Nagarajan commented 4 years ago

Had the same challenge while working on this. Created a log.js file as a workaround, which I call to generate logs:

const dateTimeMapper = function(formatArr){ let transformedObj = {}; for (let format of formatArr){ transformedObj[format["type"]] = format["value"]; } return transformedObj; }

const getLogPrefix = function(){ let timeFields = ["year", "month", "day", "hour", "minute", "second", "dayPeriod"]; let dateTimeFormat = new Intl.DateTimeFormat('en', { year: 'numeric', month: 'short', day: '2-digit' , hour : '2-digit', minute : '2-digit', second : '2-digit'}); let arr = dateTimeFormat.formatToParts(new Date()).filter(format => ~timeFields.indexOf(format["type"])); let dateTimeMap = dateTimeMapper(arr); if(dateTimeMap){ return dateTimeMap["year"]+"-"+dateTimeMap["month"]+"-"+dateTimeMap["day"]+" "+dateTimeMap["hour"]+":"+dateTimeMap["minute"]+":"+dateTimeMap["second"]+" "+dateTimeMap["dayPeriod"] + " "; } }

exports.log = { info : function(){ arguments[0] = typeof arguments[0] === "string" ? getLogPrefix() + arguments[0] : arguments[0]; console.log.apply(this, arguments); }, warn : function(){ arguments[0] = typeof arguments[0] === "string" ? getLogPrefix() + arguments[0] : arguments[0]; console.warn.apply(this, arguments); }, error : function(){ arguments[0] = typeof arguments[0] === "string" ? getLogPrefix() + arguments[0] : arguments[0]; console.error.apply(this, arguments); } }