Output debug package logs inside a file - node.js

I'm using debug npm module to log stuff, is there a way to log into a file programmatically?
Right now I'm doing DEBUG=* node myApp.js > abc.log, how can I log into abc.log by simply running DEBUG=* node myApp.js, while also outputting in stderr?
I didn't find any package doing this.

The package doesn't seem to provide a builtin feature to do this, but it provides you with a hook to customise how logs are emitted.
There is an example in the Readme here.
Note: the example is a bit confusing because it shows you how to replace writing on stdout with ... writing on stdout using the console !
So what you should at the startup of the application:
Open a stream that writes to a file. Tutorial here if you need help on this
Override the log.log() as explained in the doc to write to your file instead of using console.log().

Related

pm2 logging pm2.log to JSON

I searched but didn't find it, is it possible to change pm2 logs (specifically pm2.log file, not apps logs) to log in JSON format?
Yes, with apps logs its possible via ecosystem file, but about pm2 itself?
No, But you can log pm2 start into a .log format file
pm2 start server.js --output="/path/to/file.log" --error="/path/to/file.log"
# or
pm2 start server.js --log="/path/to/file.log"
Be aware that writing output and error logs to the same file may increase your effort to search for error entries within the log file.
More detailed information about pm2 log
More detailed information about pm2 log [official site]
example convert .log file to json

how can i find my generated files logstash?

I'm beginner with ELK stack , so I configured logstash , and when I want to search with ElasticSearch I have no results , so I'm supposed to get a result , because I do my parse on grokdebug and it works very well .
I do my research as follows:
"http://localhost:9200/logstash-2016.03.14/_search?q=*"
I wanted to know if I can see my logstash files generated and if it generated the results or not?
knowing that I tried searching elastic search on a JSON file and it works.
The problem is at logstash .
thanks
Logstash does not generate any file (except for its configuration).
To debug your logstash instance, you can :
Use the --verbose flag and/or --debug
Use the -l "file.log" to output logs in file.log (default to stdout)
Use the stdout output plugin et see the results
Also, did you use the elasticsearch output plugin?

How can I get source code of nodejs from running app

I have accidentally delete source code of nodejs application, but this application is running, so how can I get source code back from running app?
I hope source code has been cached in some directory.
I was able to recover the full file by attaching the debugger (as TGrif suggested).
To actually recover the code:
Use setBreakpoint('app.js', 10), where 10 is a line of the code you know will be ran over again in the running process
Say pause, then next until it's paused on the script you want to recover.
Finally, say list(5000), where 5000 is an arbitrarily long number of lines to list.
You will now have your full script printed out, albeit with line numbers at the front, but you can use a site like this to remove them.
Hope this helps anyone who encounters this unique issue in the future, as this took me a couple hours to figure out.
There is maybe a way to retrieve some of your source code with the Nodejs debugger.
Assuming Linux OS, you need to get the process id of your application:
$ ps -e | grep node
Next you entering your app in debug mode with something like that:
$ kill -s USR1 PID
where PID is the pid of your node app.
Then your start the debug console:
$ node debug -p PID
If you have an app console, you'll see:
Starting debugger agent.
Debugger listening on port 5858
In your debug console you should see a debug prompt and you can get available commands with:
debug> help
I am able to show some of the running app source with the list command:
debug> list(NUMBER_OF_LINE)
where NUMBER_OF_LINE is the number of source code line you want to display.
I'm not sure this is a one shot try for you or not because my source code was not deleted.
Hope you can get some results.

Node.js OpsWorks Layer console logs

I have an Opsworks stack with a Node.js Layer and Node.js Application. I'm wondering if anyone knows where on an ubuntu 14.04LTS instance the console logs from my application are being printed to. I know the opsworks uses monit to run my application but I'm not sure where its outputting the logs to.
Thanks!
So annoyingly enough, the Monit configuration rendered for Node.JS apps on Opsworks doesn't send the output anywhere! Source for this claim. (This surprised me when I learned about it!)
What I recommend doing is overriding that template - see the OpsWorks documentation on overriding templates: essentially all you need to do is copy paste the Monit config from Amazon, but change line 2 to redirect the output to a file, like I do below so:
start program = "/bin/bash -c 'cd <%= #deploy[:deploy_to] %>/current ; source <%= #deploy[:deploy_to] %>/shared/app.env ; /usr/bin/env PATH=$PATH:/usr/local/bin PORT=<%= #deploy[:nodejs][:port] %> NODE_PATH=<%= #deploy[:deploy_to] %>/current/node_modules:<%= #deploy[:deploy_to] %>/current /usr/local/bin/node <%= #monitored_script %> &> <%= #deploy[:deploy_to] %>/current/log/production.log'"
You can find it in the app directory then you will find this path shared/log
for example : /srv/www/my_app/shared/log
I found the logs by doing the following (similar to #RyanWilcox's comment):
I found my "current" deployment in /srv/www/{APP NAME}/current/
listing files, I could see the log directory symlink (log should be
symlinked to something like /srv/www/{APP NAME}/shared/log
I ran into a permissions issue trying to cd to this directory, so I
switched to the super user without a password using the command "sudo
su" and then I could access the directory
finally in the logs directory, I could see the nodejs console logs
for STDERR and STDOUT
... and Bob's father is your father's father.
Unless behavior of console.log was altered, node will output console.log logs to the application's standard output (stdout).
If you are running your node application in a console or using ssh, you should see the logs there.
Otherwise, try redirecting the stdout of your application to a file so you can see it even if you run it without a console, in this way:
node myapp.js > logfile
A preferred way would be to user Forever to make sure you application is constantly on and there you can redirect your output (both stdout and stderr) as follows:
/>forever -o forever.out -e forever.err myapp.js

any body provide me node.js gc log file

Can any body tell me how to redirect output trace_gc to log file in node js instead of console I gave command like this
node -trace_gc -trace_gc_verbose example.js
and also give me one example gc log file for node.js
If you do not need to separate the output of the application itself from the gc trace you can simply redirect the output using:
node -trace_gc -trace_gc_verbose example.js > output.log
Otherwise V8 has a --log_gc option which output to the log file (can be defined with --logfile but this does not seems to output has many information as -trace_gc.
See node --v8-options for all options

Resources