Simple AJAX file logging using node.js and winston
JavaScript
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Failed to load latest commit information.
.gitignore
LICENSE
README.md
config.js
gyppo-client.js
helpers.js
package.json
server.js

README.md

Gyppo Server

Simple AJAX file logging using node.js and Winston.

I wrote this as an AJAX backend for logging errors generated by applications running in the browser. I wanted something that was completely independent of my core infrastructure to avoid burdening my application backend with such trivial traffic.

Winston supports more transports than I need. For now I've written this to just use the File transport because that suits my purposes. But there's no reason it can't be extended to use other transports fairly painlessly.

Configuration

Change the defaults (port, user, log file location) in config.js to suit your needs.

Dependencies

The only current dependency is Winston. Install it with npm:

npm install

Usage

Boot your server:

node server.js

Then just POST to the server with params 'message' and optional 'level'.

curl --data "level=error&message=blame%20canada" http://localhost:9000

or

$.ajax({
  url: "http://localhost:9000",
  type: "POST",
  data: { level: "error", message: "blame canada" },
  success: $.noop,
  error: $.noop
});

Winston will log some JSON like this:

{"level":"error","message":"blame canada","timestamp":"2012-11-17T10:43:27.143Z"}

Troubleshooting

Make sure your log file exists and is writable by the user you've specified in config.js

Gyppo Client

The client library is totally optional. It has a jQuery dependency.

The intention of the Gyppo client library is to make it easy to track errors on the client and write them to the Gyppo backend. It also produces some nice meta_data about the client including the user agent and the page and file that produced the error.

Configuration

Include the gyppo-client.js script in your client code.

Invoke the Gyppo library and pass in options to taste:

var logger = new Gyppo({ url: "/gyppo_server_url", default_level: "warn" });

By default Gyppo will report all client errors to the Gyppo backend. If you don't want that, pass log_errors: false when invoking the library.

Usage

logger.log.error("Oh noes");

Will produce something like the following JSON in the logs:

{"ua":"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_2) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.64 Safari/537.11","href":"http://localhost:3000/evil_page.html","level":"error","message":"Oh noes","timestamp":"2012-11-17T13:38:39.964Z"}

An automatically logged error will look something like:

{"ua":"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_2) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.64 Safari/537.11","href":"http://localhost:3000/evil_page.html","level":"error","message":"http://localhost:3000/foo.js:15\n\nUncaught ReferenceError: f is not defined","timestamp":"2012-11-17T13:38:39.964Z"}