Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion example/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ const startServer = async function() {
await server.register({
plugin: require('../'),
options: {
serveScript: true
serveScript: true,
endpoint: '/api/errors-logs'
}
});

Expand Down
5 changes: 5 additions & 0 deletions example/views/main.html
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@ <h1>Hello</h1>
$(function(){
var logger = window.hapiLogger || {};

logger.configure({
debug: false,
logEndpoint: 'api/errors-logs'
});

logger.sendLog({test: 'yes'}, function(err, data) {
});

Expand Down
14 changes: 10 additions & 4 deletions lib/browser/script.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@
var HapiBrowserLogger = function() {
var self = this;

this.debug = false;
this.logEndpoint = '/api/browser-log';
this.config = {
debug: false,
logEndpoint: '/api/browser-log'
};

var getAjaxHandler = function() {
var xmlhttp;
Expand All @@ -16,6 +18,10 @@
return xmlhttp;
};

this.configure = function (config) {
Object.keys(config).forEach(prop => this.config[prop] = config[prop]);
};

this.sendLog = function(tags, data, cb) {
if (!Array.isArray(tags)) {
cb = data;
Expand All @@ -26,7 +32,7 @@
data.userAgent = navigator.userAgent;
data.location = window.location.href;

if (this.debug) {
if (this.config.debug) {
console.log('hapi-error-log', tags, data);
if(cb) {
cb(null, data);
Expand All @@ -53,7 +59,7 @@

var dataString = JSON.stringify({ data: data, tags: tags });

ajax.open('POST', this.logEndpoint, true);
ajax.open('POST', this.config.logEndpoint, true);
ajax.setRequestHeader('Content-type', 'application/json');
ajax.send(dataString);
};
Expand Down
24 changes: 24 additions & 0 deletions test/test.browser-log.js
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,30 @@ lab.test('ignore not valid', { timeout: 5000 }, async () => {
code.expect(statements.length).to.equal(1);
});

lab.test('custom logs endpoint', { timeout: 5000 }, async () => {
const statements = [];

server.events.on('log', (logObj) => {
statements.push(logObj.data);
});

await server.register({
plugin: hapiSlow,
options: {
endpoint: '/api/custom-errors-endpoint'
}
});

const response = await server.inject({
method: 'POST',
url: '/api/custom-errors-endpoint',
payload
});

code.expect(response.statusCode).to.equal(200);
code.expect(statements.length).to.equal(1);
});

lab.test('serves script', { timeout: 5000 }, async () => {
await server.register(require('inert'));
await server.register({
Expand Down