Skip to content

Commit

Permalink
chore: add logger for debugging purposes (#887)
Browse files Browse the repository at this point in the history
* update pinojs

* dont return res.send

* add logger for debugging purposes
  • Loading branch information
mpfeil authored Aug 12, 2024
1 parent 29b7acf commit f9dc662
Show file tree
Hide file tree
Showing 6 changed files with 112 additions and 16 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
35 changes: 22 additions & 13 deletions packages/api/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +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'),
pino = require('pino');

// const log = bunyan.createLogger({ name: 'opensensemap-api', serializers: bunyan.stdSerializers });
const log = pino({ name: 'opensensemap-api', sserializers: pino.stdSerializers });
{ stdLogger, debugLogger } = require('./logger');

const server = restify.createServer({
name: `opensensemap-api (${getVersion})`,
log,
onceNext: true,
strictNext: false,
});
Expand All @@ -43,20 +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 @@ -75,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
4 changes: 2 additions & 2 deletions packages/api/lib/controllers/usersController.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ const registerUser = async function registerUser (req, res) {
try {
const { token, refreshToken } = await createToken(newUser);

return res.send(201, {
res.send(201, {
code: 'Created',
message: 'Successfully registered new user',
data: { user: newUser },
Expand Down Expand Up @@ -115,7 +115,7 @@ const signIn = async function signIn (req, res) {
if (await user.checkPassword(password)) {
const { token, refreshToken } = await createToken(user);

return res.send(200, {
res.send(200, {
code: 'Authorized',
message: 'Successfully signed in',
data: { user },
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;
}
}
})
};
3 changes: 2 additions & 1 deletion packages/api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@
"millify": "^6.1.0",
"moment": "^2.29.4",
"ms": "^2.1.3",
"pino": "^8.8.0",
"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
64 changes: 64 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2928,6 +2928,14 @@ pidusage@^3.0.2:
dependencies:
safe-buffer "^5.2.1"

pino-abstract-transport@^1.2.0:
version "1.2.0"
resolved "https://registry.yarnpkg.com/pino-abstract-transport/-/pino-abstract-transport-1.2.0.tgz#97f9f2631931e242da531b5c66d3079c12c9d1b5"
integrity sha512-Guhh8EZfPCfH+PMXAb6rKOjGQEoy0xlAIn+irODG5kgfYV+BQ0rGYYWTIel3P5mmyXqkYkPmdIkywsn6QKUR1Q==
dependencies:
readable-stream "^4.0.0"
split2 "^4.0.0"

[email protected]:
version "1.0.0"
resolved "https://registry.yarnpkg.com/pino-abstract-transport/-/pino-abstract-transport-1.0.0.tgz#cc0d6955fffcadb91b7b49ef220a6cc111d48bb3"
Expand All @@ -2936,11 +2944,26 @@ [email protected]:
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"
integrity sha512-KO0m2f1HkrPe9S0ldjx7za9BJjeHqBku5Ch8JyxETxT8dEFGz1PwgrHaOQupVYitpzbFSYm7nnljxD8dik2c+g==

pino-std-serializers@^7.0.0:
version "7.0.0"
resolved "https://registry.yarnpkg.com/pino-std-serializers/-/pino-std-serializers-7.0.0.tgz#7c625038b13718dbbd84ab446bd673dc52259e3b"
integrity sha512-e906FRY0+tV27iq4juKzSYPbUj2do2X2JX4EzSca1631EB2QJQUqGbDuERal7LCtOpxl6x3+nvo9NPZcmjkiFA==

pino@^8.7.0:
version "8.15.0"
resolved "https://registry.yarnpkg.com/pino/-/pino-8.15.0.tgz#67c61d5e397bf297e5a0433976a7f7b8aa6f876b"
Expand Down Expand Up @@ -2975,6 +2998,23 @@ pino@^8.8.0:
sonic-boom "^3.1.0"
thread-stream "^2.0.0"

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==
dependencies:
atomic-sleep "^1.0.0"
fast-redact "^3.1.1"
on-exit-leak-free "^2.1.0"
pino-abstract-transport "^1.2.0"
pino-std-serializers "^7.0.0"
process-warning "^4.0.0"
quick-format-unescaped "^4.0.3"
real-require "^0.2.0"
safe-stable-stringify "^2.3.1"
sonic-boom "^4.0.1"
thread-stream "^3.0.0"

polygon-clipping@^0.15.3:
version "0.15.3"
resolved "https://registry.yarnpkg.com/polygon-clipping/-/polygon-clipping-0.15.3.tgz#0215840438470ba2e9e6593625e4ea5c1087b4b7"
Expand Down Expand Up @@ -3007,6 +3047,16 @@ 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"
integrity sha512-/MyYDxttz7DfGMMHiysAsFE4qF+pQYAA8ziO/3NcRVrQ5fSk+Mns4QZA/oRPFzvcqNoVJXQNWNAsdwBXLUkQKw==

process@^0.11.10:
version "0.11.10"
resolved "https://registry.yarnpkg.com/process/-/process-0.11.10.tgz#7332300e840161bda3e69a1d1d91a7d4bc16f182"
Expand Down Expand Up @@ -3482,6 +3532,13 @@ sonic-boom@^3.1.0:
dependencies:
atomic-sleep "^1.0.0"

sonic-boom@^4.0.1:
version "4.0.1"
resolved "https://registry.yarnpkg.com/sonic-boom/-/sonic-boom-4.0.1.tgz#515b7cef2c9290cb362c4536388ddeece07aed30"
integrity sha512-hTSD/6JMLyT4r9zeof6UtuBDpjJ9sO08/nmS5djaA9eozT9oOlNdpXSnzcgj4FTqpk3nkLrs61l4gip9r1HCrQ==
dependencies:
atomic-sleep "^1.0.0"

source-map@~0.6.1:
version "0.6.1"
resolved "https://registry.yarnpkg.com/source-map/-/source-map-0.6.1.tgz#74722af32e9614e9c287a8d0bbde48b5e2f1a263"
Expand Down Expand Up @@ -3738,6 +3795,13 @@ thread-stream@^2.0.0:
dependencies:
real-require "^0.2.0"

thread-stream@^3.0.0:
version "3.1.0"
resolved "https://registry.yarnpkg.com/thread-stream/-/thread-stream-3.1.0.tgz#4b2ef252a7c215064507d4ef70c05a5e2d34c4f1"
integrity sha512-OqyPZ9u96VohAyMfJykzmivOrY2wfMSf3C5TtFJVgN+Hm6aj+voFhlK+kZEIv2FBh1X6Xp3DlnCOfEQ3B2J86A==
dependencies:
real-require "^0.2.0"

to-regex-range@^5.0.1:
version "5.0.1"
resolved "https://registry.yarnpkg.com/to-regex-range/-/to-regex-range-5.0.1.tgz#1648c44aae7c8d988a326018ed72f5b4dd0392e4"
Expand Down

0 comments on commit f9dc662

Please sign in to comment.