Skip to content

Commit d119b2e

Browse files
committed
Added fluent logger sample.
1 parent f5b4aa9 commit d119b2e

File tree

4 files changed

+112
-1
lines changed

4 files changed

+112
-1
lines changed

.jshintignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,5 @@ appengine/sails/api/responses/**
88
appengine/webpack/dist/**
99
functions/**
1010
**/node_modules/**
11-
coverage/
11+
coverage/
12+
logging/fluent.js

logging/fluent.js

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
// Copyright 2016, Google, Inc.
2+
// Licensed under the Apache License, Version 2.0 (the "License");
3+
// you may not use this file except in compliance with the License.
4+
// You may obtain a copy of the License at
5+
//
6+
// http://www.apache.org/licenses/LICENSE-2.0
7+
//
8+
// Unless required by applicable law or agreed to in writing, software
9+
// distributed under the License is distributed on an "AS IS" BASIS,
10+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
// See the License for the specific language governing permissions and
12+
// limitations under the License.
13+
14+
'use strict';
15+
16+
var express = require('express');
17+
var app = express();
18+
19+
app.get('*', function (req, res, next) {
20+
return next('oops');
21+
});
22+
23+
// [START fluent]
24+
var structuredLogger = require('fluent-logger').createFluentSender('myapp', {
25+
host: 'localhost',
26+
port: 24224,
27+
timeout: 3.0
28+
});
29+
30+
var report = function (err, req) {
31+
var payload = {
32+
serviceContext: {
33+
service: 'myapp',
34+
},
35+
message: err.stack,
36+
context: {
37+
httpRequest: {
38+
url: req.originalUrl,
39+
method: req.method,
40+
referrer: req.header('Referer'),
41+
userAgent: req.header('User-Agent'),
42+
remoteIp: req.ip,
43+
responseStatusCode: 500,
44+
}
45+
}
46+
};
47+
structuredLogger.emit('errors', payload);
48+
};
49+
50+
// Handle errors (the following uses the Express framework)
51+
app.use(function (err, req, res, next) {
52+
report(err, req);
53+
res.status(500).send(err.response || 'Something broke!');
54+
});
55+
// [END fluent]
56+
57+
module.exports = app;

logging/package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
"export": "node export.js"
1515
},
1616
"dependencies": {
17+
"express": "^4.13.4",
18+
"fluent-logger": "^1.1.0",
1719
"gcloud": "^0.30.3"
1820
}
1921
}

test/logging/fluent.test.js

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
// Copyright 2015-2016, Google, Inc.
2+
// Licensed under the Apache License, Version 2.0 (the "License");
3+
// you may not use this file except in compliance with the License.
4+
// You may obtain a copy of the License at
5+
//
6+
// http://www.apache.org/licenses/LICENSE-2.0
7+
//
8+
// Unless required by applicable law or agreed to in writing, software
9+
// distributed under the License is distributed on an "AS IS" BASIS,
10+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
// See the License for the specific language governing permissions and
12+
// limitations under the License.
13+
14+
'use strict';
15+
16+
var test = require('ava');
17+
var proxyquire = require('proxyquire').noPreserveCache();
18+
var request = require('supertest');
19+
20+
test.cb('should log error', function (t) {
21+
var loggerCalled = false;
22+
23+
var structuredLogger = {
24+
emit: function (name) {
25+
loggerCalled = true;
26+
t.is(name, 'errors');
27+
}
28+
};
29+
30+
var app = proxyquire('../../logging/fluent.js', {
31+
'fluent-logger': {
32+
createFluentSender: function (name, options) {
33+
t.is(name, 'myapp');
34+
t.same(options, {
35+
host: 'localhost',
36+
port: 24224,
37+
timeout: 3.0
38+
});
39+
return structuredLogger;
40+
}
41+
}
42+
});
43+
44+
request(app)
45+
.get('/')
46+
.expect(500)
47+
.expect(function () {
48+
t.is(loggerCalled, true, 'structuredLogger.emit should have been called');
49+
})
50+
.end(t.end);
51+
});

0 commit comments

Comments
 (0)