Skip to content

Commit

Permalink
add logger for debugging purposes
Browse files Browse the repository at this point in the history
  • Loading branch information
mpfeil committed Aug 12, 2024
1 parent b2bfdd1 commit bda0e2d
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 10 deletions.
1 change: 1 addition & 0 deletions config/default.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ const defaults = {
slack_url: '',
mattermost_url: '',
management_role: 'admin',
logLevel: 'info',
routes: {
boxes: '/boxes',
users: '/users',
Expand Down
31 changes: 22 additions & 9 deletions packages/api/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,27 @@

'use strict';

const
{ db } = require('@sensebox/opensensemap-api-models'),
const { db } = require('@sensebox/opensensemap-api-models'),
restify = require('restify'),
{ fullResponse, queryParser, jsonBodyParser, pre: { sanitizePath } } = restify.plugins,
{
fullResponse,
queryParser,
jsonBodyParser,
pre: { sanitizePath }
} = restify.plugins,
config = require('config'),
{ preRequest, preCors, Honeybadger, getVersion, postToMattermost } = require('./lib/helpers/apiUtils'),
{
preRequest,
preCors,
Honeybadger,
getVersion,
postToMattermost
} = require('./lib/helpers/apiUtils'),
routes = require('./lib/routes'),
log = require('pino')({ name: 'opensensemap-api' });
{ stdLogger, debugLogger } = require('./logger');

const server = restify.createServer({
name: `opensensemap-api (${getVersion})`,
log,
onceNext: true,
strictNext: false,
});
Expand All @@ -40,19 +49,23 @@ server.use(fullResponse());
server.use(queryParser());
server.use(jsonBodyParser());

if (config.get('logLevel') === 'debug') {
server.use(debugLogger);
}

db.connect()
.then(function () {
// attach Routes
routes(server);

// start the server
server.listen(Number(config.get('port')), function () {
log.info(`${server.name} listening at ${server.url}`);
stdLogger.logger.info(`${server.name} listening at ${server.url}`);
postToMattermost(`openSenseMap API started. Version: ${getVersion}`);
});
})
.catch(function (err) {
log.fatal(err, 'Couldn\'t connect to MongoDB. Exiting...');
stdLogger.logger.fatal(err, 'Couldn\'t connect to MongoDB. Exiting...');
process.exit(1);
});

Expand All @@ -71,7 +84,7 @@ server.on('InternalServer', function (req, res, err, callback) {
query: req.query,
_userParams: req._userParams
});
log.error(err);
stdLogger.logger.error(err);
// and notify
Honeybadger.notify(err);

Expand Down
21 changes: 21 additions & 0 deletions packages/api/logger.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
'use strict';

const config = require('config');
const pino = require('pino-http');


module.exports = {
stdLogger: pino({ name: 'opensensemap-api' }),
debugLogger: pino({
name: 'opensensemap-api-debug',
serializers: {
req (req) {
if (config.get('logLevel') === 'debug') {
req.body = req.raw.body;
}

return req;
}
}
})
};
1 change: 1 addition & 0 deletions packages/api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
"moment": "^2.29.4",
"ms": "^2.1.3",
"pino": "^9.3.2",
"pino-http": "^10.2.0",
"restify": "11.1.0",
"restify-errors": "^8.0.2",
"simple-statistics": "^7.7.0",
Expand Down
17 changes: 16 additions & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2944,6 +2944,16 @@ pino-abstract-transport@v1.0.0:
readable-stream "^4.0.0"
split2 "^4.0.0"

pino-http@^10.2.0:
version "10.2.0"
resolved "https://registry.yarnpkg.com/pino-http/-/pino-http-10.2.0.tgz#157b799e84ee4f6fe5a862fa3758f10d25376fed"
integrity sha512-am03BxnV3Ckx68OkbH0iZs3indsrH78wncQ6w1w51KroIbvJZNImBKX2X1wjdY8lSyaJ0UrX/dnO2DY3cTeCRw==
dependencies:
get-caller-file "^2.0.5"
pino "^9.0.0"
pino-std-serializers "^7.0.0"
process-warning "^3.0.0"

pino-std-serializers@^6.0.0:
version "6.1.0"
resolved "https://registry.yarnpkg.com/pino-std-serializers/-/pino-std-serializers-6.1.0.tgz#307490fd426eefc95e06067e85d8558603e8e844"
Expand Down Expand Up @@ -2988,7 +2998,7 @@ pino@^8.8.0:
sonic-boom "^3.1.0"
thread-stream "^2.0.0"

pino@^9.3.2:
pino@^9.0.0, pino@^9.3.2:
version "9.3.2"
resolved "https://registry.yarnpkg.com/pino/-/pino-9.3.2.tgz#a530d6d28f1d954b6f54416a218cbb616f52f901"
integrity sha512-WtARBjgZ7LNEkrGWxMBN/jvlFiE17LTbBoH0konmBU684Kd0uIiDwBXlcTCW7iJnA6HfIKwUssS/2AC6cDEanw==
Expand Down Expand Up @@ -3037,6 +3047,11 @@ process-warning@^2.0.0:
resolved "https://registry.yarnpkg.com/process-warning/-/process-warning-2.1.0.tgz#1e60e3bfe8183033bbc1e702c2da74f099422d1a"
integrity sha512-9C20RLxrZU/rFnxWncDkuF6O999NdIf3E1ws4B0ZeY3sRVPzWBMsYDE2lxjxhiXxg464cQTgKUGm8/i6y2YGXg==

process-warning@^3.0.0:
version "3.0.0"
resolved "https://registry.yarnpkg.com/process-warning/-/process-warning-3.0.0.tgz#96e5b88884187a1dce6f5c3166d611132058710b"
integrity sha512-mqn0kFRl0EoqhnL0GQ0veqFHyIN1yig9RHh/InzORTUiZHFRAur+aMtRkELNwGs9aNwKS6tg/An4NYBPGwvtzQ==

process-warning@^4.0.0:
version "4.0.0"
resolved "https://registry.yarnpkg.com/process-warning/-/process-warning-4.0.0.tgz#581e3a7a1fb456c5f4fd239f76bce75897682d5a"
Expand Down

0 comments on commit bda0e2d

Please sign in to comment.