node.js log module bunyan change timezone - javascript

I'm using this logging module bunyan.js which is included in the framwork restify.js. The module does outprint a time in the log file/console, however, I want to change the time to UTC/GMT, not sure if it's possible wihtout modifying the module code?

If you don't want to use local time anywhere else in your process, one way to achieve what you want is to change the timezone for the process. Either by writing this statement at the startup of you application:
process.env.TZ = 'UTC'
Or by starting it with a environment variable from the command line, like this:
TZ=UTC node main.js

I was also facing the same issue and resolved it by adding a custom attribute, localtime, while creating the logger using bunyan.createLogger method like this:
var init = function () {
log = bunyan.createLogger({
name: 'myLogs',
src: true,
localtime: new Date().toString();
});
};
By doing this, I get an extra field in my log called localtime with the appropriate time as per my timezone.
Hope this helps.

Related

Javascript macOS Error: Named parameters must be passed as an object

I'm trying to implement automation for the task manager Things 3 on macOS using JXA (Javascript for macOS automation) and I'm running it to a strange error. I have spent countless hours trying to fix this myself and with the help of others. I am trying to use the following method described in the documentation:
As following:
// Set TaskApp as the app to use
var TaskApp = Application('Things3');
// Get the ToDo from toDos
var task = TaskApp.toDos["test555342343"]
// Get today as a date
var today = new Date()
// Schedule the task for Today
task.schedule(task.id(), {for: today})
This returns the error:
Error: Error: Named parameters must be passed as an object.
When using another method (like show) the specefier works as expected:
Example:
// Set TaskApp as the app to use
var TaskApp = Application('Things3');
// Get the ToDo from toDos
var task = TaskApp.toDos["test555342343"]
// Bring Things3 to the Forground
TaskApp.activate()
// Show the task
task.show(task.id())
Will show the selected todo. Creating a task with with a deadline set to the date object also produces the correct result (a task with the deadline of date).
This documentation can only be found when you have things3 installed on macOS and you reference the Script Library. I have added the documentation as PDF. The described error also apply's to the move method. Instead of parsing a date you parse a List Object to it which will resolve in the same error.
Link to documentation PDF → Link
The Things documentation is wrong: schedule is a method of Application, not of ToDo, which is why it asks for a todo specifier (it wouldn’t need that one if it was a property of a ToDo object already). The working code hence is:
// Set TaskApp as the app to use
var TaskApp = Application('Things3')
// Get the ToDo from toDos
var task = TaskApp.toDos["test555342343"]
// Get today as a date
var today = new Date()
// Schedule the task for Today
TaskApp.schedule(task, {for: today})
Note that task already is a ToDo specifier; you don’t need the detour through task.id(), which converts the ToDo specifier into an ID String before letting the schedule method cast that back to a specifier.

Difference between require('module')() and const mod = require('module') mod() in node/express

I have two files: server.js and db.js
server.js looks as such:
...
const app = express();
app.use('/db', db());
app.listen(3000, () => {
console.log('Server started on port 3000')
});
...
and db.js as such:
...
function init() {
const db = require('express-pouchdb')(PouchDB, {
mode: 'minimumForPouchDB'
});
return db;
}
...
This works just fine, and I am able to reach the pouchdb http-api from my frontend. But before, I had const PouchDBExpress = require('pouchdb-express'); in the top of db.js, and the first line in init() looked like this; const db = PouchDBExpress(PouchDB, {. This gave an error in one of the internal files in pouchdb saying cannot set property query on req which only has getters (paraphrasing).
So this made me copy the exaples from pouchdb-servers GitHub examples which requires and invokes pouched-express directly, and everthing worked fine. Is there an explanation for this? I'm glad it works now, but I'm sort of confused as to what could cause this.
The only difference between:
require('module')()
and
const mod = require('module');
mod();
is that in the second case, you retain a reference to the module exports object (perhaps for other uses) whereas in the first one you do not.
Both cases load the module and then call the exported object as a function. But, if the module export has other properties or other methods that you need access to then, obviously, you need to retain a reference to it as in the second option.
For us to comment in more detail about the code scenario that you said did not work, you will have to show us that exact code scenario. Describing what is different in words rather than showing the actual code makes it too hard to follow and impossible to spot anything else you may have inadvertently done wrong to cause your problem.
In require('module')(), you don't retain a reference of the module imported.
While in const mod = require('module'); mod(), you retain a reference and can use the same reference later in your code.
This problem might be due to some other reason like -
Are you using a some another global instance of the db, and your code works in the given case as you are making a local instance
Some other code dependent scenario.
Please provide more details for the same

How do you modify a nodejs module variable?

(This is probably a very silly question since I'm just beginning with nodejs, nevertheless I can't understand how this works. Let me know what is missing in my question, I'll correct.)
I'm trying to use the npm package likely.
in my server.js file I have thus written this
var Recommender = require('likely');
in likely.js you can find variables like these:
var DESCENT_STEPS = 5000; // number of iterations to execute gradient descent
var ALPHA = 0.0005; // learning rate, should be small
I would like to modify these variables inside my server.js file.
I believe the way to do that is adding this after the require()
Recommender.DESCENT_STEPS = 9999999999;
but that doesn't seem to change the value that is defined in likely.js and that is actually used by the model. (by running the model I can see it doesn't work since so much steps should take forever and the processing time doesn't change at all)
Can I only do this by modifying likely.js?
You cannot modify them programmatically because likely.js only uses the local variable values instead of the current value of the exported versions of the same variables. So if you wanted to change those values you currently would need to edit likely.js. You might want to submit a pull request to that project's repository that makes the code use the exported value (e.g. module.exports.DESCENT_STEPS) instead.
You need to publicize these variables to be viewable by server.js.
var object = {
DESCENT_STEPS = 5000;
ALPHA = 0.0005;
}
module.exports = object;
Now it can be viewed and modified in server.js.
Recommender.ALPHA = 'new value';

How to inject variable value into JS file from GULP

Hopefully this is a quick question.
What I'm trying to do is to add a timestamp into a Javascript object in a JS file while the file is being built with GULP. Basically, in "file.js", I have an object where I would like to have object.timeStamp that equals the time of the GULP build.
I am currently adding a timestamp to the top of the file using gulp-header, but I have been asked to add the timestamp to a property in the object.
My thought was to inject the value from GULP, but all of the injection plugins I have found so far are for injecting contents of one file into the target file.
Any help would be greatly appreciated.
Are you using any kind of modules? ES6 modules, AMD, CommonJS, ...?
If so, you can generate a config module with Gulp where you can inject any variable you want. It would look something like this:
config.tmpl.js
module.exports = <%= config %>
config gulp task
var gulp = require('gulp');
var template = require('gulp-template');
var rename = require('gulp-rename');
gulp.task('config', function() {
return gulp.src('path/to/config.tmpl.js')
.pipe(template({config: JSON.stringify({
timeStamp: new Date()
})}))
.pipe(rename('config.js'))
.pipe(gulp.dest('path/to/config.js'));
});
and finally, in your JS file
var config = require('path/to/config.js');
var object = {
timeStamp: config.timeStamp
}
O.K., so I figured out a workaround that gets me where I want to be. The first this is using the gulp-header to insert a variable declaration into my Javascript file using this code:
In GULP:
var d = new Date();
.pipe(header("var timeStamp = '" + d +"';"))
Then, in my Javascript file, I set up a property in the object that is a function, and sets the timeStamp property I was looking for by getting the value from the timeStamp variable injected above using the gulp-header, like this:
In the JS File, the header functionality inserts this:
var timeStamp = 'Tue Dec 08 2015 15:15:11 GMT-0800 (PST)';
And then my function, in the JS Object is simply this:
ts: function(){
object.timeStamp = timeStamp;
},
Which, when called, runs and sets the timestamp inside of the object.
It seems like there should be an easier way to do this, but for now, this is working.
If you have any ideas on making it better, I would love to hear them!

Setting an environment variable in javascript

How can I set an environment variable in WSH jscript file that calls another program? Here's the reduced test case:
envtest.js
----------
var oShell = WScript.CreateObject("WScript.Shell");
var oSysEnv = oShell.Environment("SYSTEM");
oSysEnv("TEST_ENV_VAR") = "TEST_VALUE";
oExec = oShell.Run("envtest.bat", 1, true);
envtest.bat
-----------
set
pause
I expect to see TEST_ ENV _VAR in the list of variables, but it's not there. What's wrong?
edit:
If someone can produce a working code sample, I'll mark that as the correct answer. :)
The problem is not in your code, but it is in execution of the process. The complete system variables are assigned to the process which is executed. so, your child process also had the same set of variables.
Your code-sample works good. It adds the variable to the SYSTEM environment.
So, you need to set the variable not only for your system but also for your process.
Here's the code.
var oShell = WScript.CreateObject("WScript.Shell");
var oSysEnv = oShell.Environment("SYSTEM");
oSysEnv("TEST1") = "TEST_VALUE";
var oSysEnv = oShell.Environment("PROCESS");
oSysEnv("TEST1") = "TEST_VALUE";
oExec = oShell.Run("envtest.bat", 1, true);
Once you created the system variable.
It will assign the newly created variable for the current process. So, your child process can get that variable while the "SET" command executed.
Sorry for my bad-english.
There are 4 "collections" (System, User, Volatile, and Process) you probably want Process if you just need a child process to see the variable
You are getting the system environment variables. I suspect you simply don't have permission to modify them; you could try changing this to the user environment variables.
Also I don't know whether the argument to Environment() is case-sensitive or not. MS's documentation uses "System" instead of "SYSTEM". Might make a difference but I don't know for sure.

Categories

Resources