Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add admin only endpoint to update the api log level at runtime #619

Merged
merged 3 commits into from
Oct 27, 2021
Merged
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
59 changes: 59 additions & 0 deletions api/src/paths/logger.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import { expect } from 'chai';
import { describe } from 'mocha';
import { CustomError } from '../errors/CustomError';
import * as logger from './logger';

describe('logger', () => {
let actualStatus: any = null;
let actualResult: any = null;

const sampleRes = {
status: (status: any) => {
actualStatus = status;

return {
send: (response: any) => {
actualResult = response;
}
};
}
} as any;

const sampleNext = () => {
// do nothing
};

describe('updateLoggerLevel', () => {
it('should throw a 400 error when `level` query param is missing', async () => {
const operation = logger.updateLoggerLevel();

const sampleReq = {
query: {}
} as any;

try {
await operation(sampleReq, sampleRes, sampleNext);

expect.fail();
} catch (error) {
expect((error as CustomError).status).to.equal(400);
expect((error as CustomError).message).to.equal('Missing required query param `level`');
}
});

it('should return 200 on success', async () => {
const operation = logger.updateLoggerLevel();

const sampleReq = {
query: {
level: 'info'
}
} as any;

await operation(sampleReq, sampleRes, sampleNext);

expect(actualStatus).to.eql(200);
expect(actualResult).to.eql(undefined);
});
});
});
65 changes: 65 additions & 0 deletions api/src/paths/logger.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import { RequestHandler } from 'express';
import { Operation } from 'express-openapi';
import { SYSTEM_ROLE } from '../constants/roles';
import { HTTP400 } from '../errors/CustomError';
import { getLogger } from '../utils/logger';

export const GET: Operation = [updateLoggerLevel()];

GET.apiDoc = {
description: "Update the log level for the API's default logger",
tags: ['misc'],
security: [
{
Bearer: [SYSTEM_ROLE.SYSTEM_ADMIN]
}
],
parameters: [
{
in: 'query',
name: 'level',
schema: {
description: 'Log levels, from least logging to most logging',
type: 'string',
enum: ['silent', 'error', 'warn', 'info', 'debug', 'silly']
},
required: true
}
],
responses: {
200: {
description: 'OK'
},
400: {
$ref: '#/components/responses/400'
},
401: {
$ref: '#/components/responses/401'
},
403: {
$ref: '#/components/responses/401'
},
default: {
$ref: '#/components/responses/default'
}
}
};

/**
* Get api version information.
*
* @returns {RequestHandler}
*/
export function updateLoggerLevel(): RequestHandler {
return (req, res) => {
if (!req.query?.level) {
throw new HTTP400('Missing required query param `level`');
}

// Update default logger log level
const defaultLog = getLogger('');
defaultLog.transports[0].level = req.query?.level as string;

res.status(200).send();
};
}
4 changes: 2 additions & 2 deletions api/src/utils/logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,13 +107,13 @@ export const getPrintfFunction = (logLabel: string): ((args: ILoggerMessage) =>
* ...etc
*
* Valid `LOG_LEVEL` values (from least logging to most logging) (default: info):
* error, warn, info, debug
* silent, error, warn, info, debug, silly
*
* @param {string} logLabel common label for the instance of the logger.
* @returns
*/
export const getLogger = function (logLabel: string) {
return winston.loggers.get(logLabel || 'default', {
return winston.loggers.get('default', {
transports: [
new winston.transports.Console({
level: process.env.LOG_LEVEL || 'info',
Expand Down