Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to to log JS errors from a client into kibana?

I have web application backed end in NodeJS and logstash/elasticsearch/kibana to handle system logs like (access_error.log, messages.log etc).

Right now I need to record all JavaScript client side errors into kibana also. What is the best way to do this?

EDIT: I have to add additional information to this question. As @Jackie Xu provide partial solution to my problem and as follows from my comment:

I'm most interested in realizing server-side error handling. I think it's not effective write each error into file. I'm looking for best practices how to make it more performance.

I need to handle js error records on server-side more effective than just write into file. May you provide some scenarios how could I increase server-side logging performance?

like image 905
Erik Avatar asked Jul 01 '14 03:07

Erik


People also ask

How does Kibana logging work?

Logsedit. The Logs app in Kibana enables you to search, filter, and tail all your logs ingested into Elasticsearch. Instead of having to log into different servers, change directories, and tail individual files, all your logs are available in the Logs app.

What is Logger log in Javascript?

Logger. Writes the string to the logging console. log(format, values) Logger. Writes a formatted string to the logging console, using the format and values provided.


1 Answers

When you say client, I'm assuming here that you mean a logging client and not a web client.

First, make it a habit to log your errors in a common format. Logstash likes consistency, so if you're putting text and JSON in the same output log, you will run into issues. Hint: log in JSON. It's awesome and incredibly flexible.

The overall process will go like this:

  1. Error occurs in your app
  2. Log the error to file, socket, or over a network
  3. Tell logstash how to get (input) that error (i.e. from file, listen over network, etc)
  4. Tell logstash to send (output) the error to Elasticsearch (which can be running on the same machine)

In your app, try using the bunyan logger for node. https://github.com/trentm/node-bunyan

node app index.js

var bunyan = require('bunyan'); var log = bunyan.createLogger({   name: 'myapp',   streams: [{     level: 'info',     stream: process.stdout // log INFO and above to stdout   }, {     level: 'error',     path: '/var/log/myapp-error.log' // log ERROR and above to a file   }] });  // Log stuff like this log.info({status: 'started'}, 'foo bar message');  // Also, in express you can catch all errors like this app.use(function(err, req, res, next) {    log.error(err);    res.send(500, 'An error occurred'); }); 

Then you need to configure logstash to read those JSON log files and send to Elasticsearch/Kibana. Make a file called myapp.conf and try the following:

logstash config myapp.conf

# Input can read from many places, but here we're just reading the app error log input {     file {         type => "my-app"         path => [ "/var/log/myapp/*.log" ]         codec => "json"     }    }  # Output can go many places, here we send to elasticsearch (pick one below) output {    elasticsearch {     # Do this if elasticsearch is running somewhere else     host => "your.elasticsearch.hostname"     # Do this if elasticsearch is running on the same machine     host => "localhost"     # Do this if you want to run an embedded elastic search in logstash     embedded => true      }  } 

Then start/restart logstash as such: bin/logstash agent -f myapp.conf web

Go to elasticsearch on http://your-elasticsearch-host:9292 to see the logs coming in.

like image 97
methai Avatar answered Sep 21 '22 13:09

methai