console.log is not displayed in heroku logs - heroku

I have the program heroku_test.ts
console.log('test')
I can run it with the following command:
heroku run ts-node heroku_test.ts
In the same console window I see it output 'test'
But when I look at heroku logs, there is no 'test' there, it just says "Starting process with command ts-node heroku_test.ts"
Why heroku logs do not contain console.log output?
EDIT: the question is different from how to show all console.log from node.js in heroku? because I can see all the logs except any console.log output.

First install winston :
https://github.com/winstonjs/winston
as follows :
npm install winston --save
Then change your heroku_test.js to the following :
const winston = require('winston')
winston.log('info', '-------Hello log files!------------', {
someKey: 'some-value'
})
Then run
heroku run node heroku_test.js
Then check the logs :
heroku logs --tail
and you should see the above in the heroku logs

you can try some opensource libs, e.g.,
https://www.npmjs.com/package/heroku-logger

Related

Invalid testing environment specified: chrome

When run the this code got below error-
Invalid testing path
Like the error message states.
You have
'default', 'chrome-local'
environments available to you in your configuration file.
You will need to run the command npx nightwatch -e chrome-local like the user 'AutomatedTester' said.

Heroku release phase bundle not found

I have the following release phase in my Procfile:
release: bundle exec rake db:migrate
It works great when I merge PR's into my staging and production apps, but it fails when running on a new review app. The Heroku docs say that the release phase is run after a successful build, so I don't know why it can't find bundle.
This is my output
heroku releases:output 9 --app my-app-pr-253
/bin/sh: 1: bundle: not found
For Heroku's review apps, you must specify all buildpacks and ENV vars you need in the app.json file. You can either manually create one, or have Heroku generate one for you.
https://devcenter.heroku.com/articles/github-integration-review-apps#app-json
Confirm that in your app.json you have specified
1) The required buildpacks https://devcenter.heroku.com/changelog-items/670. Since you are using bundle I'm guessing heroku/ruby will be one. Below is an example.
"buildpacks": [
{
"url": "https://github.com/heroku/heroku-buildpack-ruby.git"
},
2) Also make sure you specify any config variables that you want to inherit from the app off which your review app is being built. https://devcenter.heroku.com/articles/app-json-schema#env Missing one of these could also be causing a build to fail.
If neither of these work, try checking the logs for your heroku app. Watch the ones in the Heroku GUI during the build. Also try to tail the logs in the CLI.
heroku logs -t -a <review_app_name>
I figured out my problem. It was a silly typo:
"buildpacks": [
{
"url": "heroku/ruby",
"url": "https://github.com/guillaume-tgl/heroku-buildpack-ghostscript.git"
}
]
should have been:
"buildpacks": [
{ "url": "heroku/ruby"},
{ "url": "https://github.com/guillaume-tgl/heroku-buildpack-ghostscript.git" }
]

How do I see logs on parse-server?

Parse had a nice development command utility where you could read and stream logs.
Heroku has that, but it shows only Heroku logs, not Parse logs.
Is there some way to see a console.log or console.error statement now that we're all switching over to parse-server?
If you use PM2, it is really easy to see logs.
For my project, I have parse-server and parse-dashboard running on my server.
Here is the PM2 config I use for them:
{
"apps": [
{
"script": "parse-server",
"args": "config/server.json",
"log_file": "logs/server.log",
"error_file": "logs/server-error.log",
"log_date_format" : "YYYY-MM-DD HH:mm:ss Z",
"instances": 1,
"watch": true,
"ignore_watch": ["cloud", "logs"],
"env": {
"VERBOSE": "1"
}
},
{
"script": "parse-dashboard",
"args": "--config config/dashboard.json",
"log_file": "logs/dashboard.log",
"error_file": "logs/dashboard-error.log",
"log_date_format" : "YYYY-MM-DD HH:mm:ss Z",
"instances": 1,
"watch": true,
"ignore_watch": ["cloud", "logs"]
}
]
}
In my case, it is the "VERBOSE": "1" argument that allows me to see all the queries executed by parse-server.
If you want to see the logs of both parse-server and parse-dashboard, you then only have to type pm2 logs.
In my configuration, parse-server and parse-dashboard are installed globally (npm install -g parse-server and npm install -g parse-dashboard).
If you have Heroku CLI installed you can run these lines inside your project
heroku logs
for the last 100 lines or
heroku logs --tail
to show logs in real time
The latest versions of Parse dashboard have Logs page out of the box
I dont know about how you can see logs on Heroku, but i am able to see logs locally. What you need to do is when your run your application using command => node app.js then you will be able to see all console.log(""); statements in the console.
If you want you check this link . How to setup Parse on local machine.
https://www.webniraj.com/2016/01/31/parse-com-setting-up-the-open-source-parse-api-server/.
I hope this helps.Thanks
here are 2 ways, in case of selfhosted parse-server:
#1 view logs by url
create a symbolic link of your log-folder to /public/logs
add this middleware:
app.get(/^\/logs*/,(req,res,next) => {
if( req.url == '/logs/' || req.url == '/logs' )
return res.redirect('/logs/parse-server.info.'+new Date().toISOString().slice(0,10))
if( req.url.match(/parse-server\./) )
res.set('content-type','text/plain') // lets hint the browser for a logfile
next()
})
// *TODO* please run basic-auth middleware on /logs url
BOOM...now surfing to '/logs' will always redirect to the latest log-url.
You can just modify the dates to go back in time.
#2 view realtime logs
see this package https://www.npmjs.com/package/express-logio
You can use request.log.info() and request.log.error() and they will be show in the parse dashboard
Cloud Code function
Log on Parse dashboard

How to use Node's debug module (Windows)?

I am trying to figure out what is wrong with my sessions (using express-session), and I found that it uses the debug module. However, I can't seem to enable the debug messages. It says, debugging needs to be enabled through the DEBUG environment variable, but I can't seem to get it to run.
The tutorial in the README has this picture:
Under Windows I get "DEBUG is not a command for the command-line".
So I tried setting the environment variable explicitly using:
process.env.DEBUG = "*";
and still nothing.
What am I doing wrong?
As Traveling Tech Guy suggested in the comment, in a Windows command prompt, the proper syntax is:
set DEBUG=* & npm start
Obviously you can replace npm start with whatever command you need to launch your Node.js app. Just be sure to use the set command and don't forget the & between that command and the one launching your Node.js app!
If you are used to powershell, I recommend this setup in your package.json, then you can just run npm start so you don't type all that out every time.
"scripts": {
"start": "#powershell $env:DEBUG='*,-express:router*' ; node app.js"
},
Install debug package with npm inside the node application
npm install debug
Open a powershell and then
$Env:DEBUG="name_to_call"
node path_to_js_to_execute.js
Inside your pgm
var debug = require('debug')('name_to_call');
Firstly you need to install the debug module using
"npm install debug --save"
you will see that the following lane has been added to you package.json (which has all the npm modules charged to your proyect)
then you need to add this to import the module to the file in which you want to run the debug
var debug = require('debug')('name_to_call');
now we have to just put the message we want to write, for example hello
var debug = require('debug')('name_to_call');
debug('Hello');
(try pasting the code above directly to a file)
Now we just need to start the windows server with DEBUG, to do so we are going to use the npm package cross-env that will make easier to set an ENV variable across any operating system (platform agnostic)
npm install cross-env
Change the package.json and add the following under the scripts section
"start-server": "cross-env DEBUG=name_to_call node server.js"
Now to start the server just run the following in a command line (from the directory in which your project is) and you are good to go
npm run start-server
In Babun a windows shell, I run,
npm
npm install debug --save
babun
DEBUG=http node app
app.js
var app = express();
var debug = require('debug')('http');
var http = require('http').Server(app);
var server = http.listen(app.get('port'), function() {
debug('listening on port ' + server.address().port);
});
works like a charme for me.
Windows Command:
set DEBUG=* & node ./app.js
PowerShell:
$env:DEBUG='*'; node app.js
Terminal/WSL (Ubuntu):
DEBUG=* node ./app.js

How to set Heroku config var with contents of a file

To set config vars for a Heroku app, you do this:
$ heroku config:set GITHUB_USERNAME=joesmith
How would I set a config var with the contents of a file?
Take a look at the heroku-config plugin, which adds a heroku config:push command to push key-value pairs in a file named .env to the app.
It also has a heroku config:pull command to do the opposite and works very well with foreman for running the app locally with the config in .env.
https://github.com/xavdid/heroku-config
Example
heroku config:push --file=.env.production
I know this is too late but still it will be helpful for future users who land here.
I also wanted a quick solution to add variable to heroku app but copy-paste is so boring.. So wrote a script to read values from the .env file and set it at the requested app - all things passed as an option:
https://gist.github.com/md-farhan-memon/e90e30cc0d67d0a0cd7779d6adfe62d1
Usage
./bulk_add_heroku_config_variables.sh -f='/path/to/your/.environment/file' -s='bsc-server-name' -k='YOUR_CONFIG_KEY1 YOUR_CONFIG_KEY2'
A simple pure-python solution using honcho and invoke:
from honcho.environ import parse
from invoke import run
def push_env(file='.env'):
"""Push .env key/value pairs to heroku"""
with open(file, 'r') as f:
env = parse(f.read())
cmd = 'heroku config:set ' + ' '.join(
f'{key}={value}' for key, value in env.items())
run(cmd)
The idea here is that you will get the same configuration as if you ran the project locally using honcho. Then I use invoke to run this task easily from the command line (using #task and c.run) but I've adapted it here to stand alone.

Resources