Skip to content

Commit 0f1156e

Browse files
GufCabnlg
andauthored
Issue/120 configurable log levels (#127)
* Log level is now configurable globally * Read log configuration from Environment Variables * Added comments and fixed indentation. * Minor cleanup in nestjs.ts * Removed unused code and fixed config. Co-authored-by: nlg <[email protected]>
1 parent 0b30a17 commit 0f1156e

File tree

4 files changed

+28
-6
lines changed

4 files changed

+28
-6
lines changed

src/config/configuration.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { GetLogLevels } from "@helpers/env-variable-helper";
2+
13
export default (): any => {
24
return {
35
port: parseInt(process.env.PORT, 10) || 3000,
@@ -28,5 +30,6 @@ export default (): any => {
2830
chirpstack: {
2931
jwtsecret: process.env.CHIRPSTACK_JWTSECRET || "verysecret",
3032
},
33+
logLevels: process.env.LOG_LEVEL ? GetLogLevels(process.env.LOG_LEVEL) : GetLogLevels('debug')
3134
};
3235
};

src/helpers/env-variable-helper.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import { LogLevel } from '@nestjs/common';
2+
3+
const logLevels: LogLevel[] = ['log', 'error', 'warn', 'debug', 'verbose'];
4+
5+
// Gets a list of log levels equal to, or higher, than the supplied minimum level
6+
export function GetLogLevels(minLevel: string): LogLevel[] {
7+
// If nothing was supplied, use default values
8+
if (!minLevel) {
9+
return undefined;
10+
}
11+
12+
const logIndex = logLevels.indexOf(minLevel as LogLevel);
13+
if (logIndex < 0) {
14+
throw new Error(`Invalid log level: "${minLevel}" supplied in environment`);
15+
}
16+
17+
// We want to return all the HIGHER log levels and they are ranked high->low
18+
const higherLevels = logLevels.slice(0, logIndex + 1);
19+
return higherLevels;
20+
}

src/loaders/nestjs.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,25 +3,27 @@ import {
33
INestApplication,
44
ValidationPipe,
55
Logger as BuiltInLogger,
6+
LogLevel,
67
} from "@nestjs/common";
7-
import { Logger } from "nestjs-pino";
88
import { NestFactory } from "@nestjs/core";
99
import * as compression from "compression";
1010
import { AppModule } from "@modules/app.module";
1111
import { ExpressAdapter } from "@nestjs/platform-express/adapters/express-adapter";
1212
import * as cookieParser from "cookie-parser";
1313
import { Express } from "express";
1414

15+
1516
export async function setupNestJs(
1617
config: {
1718
NEST_PORT: number;
1819
API_PREFIX: string;
1920
CURRENT_VERSION_PREFIX: string;
2021
SWAGGER_PREFIX: string;
22+
LOG_LEVELS: LogLevel[];
2123
},
2224
server: Express
2325
): Promise<INestApplication> {
24-
const app = await NestFactory.create(AppModule, new ExpressAdapter(server));
26+
const app = await NestFactory.create(AppModule, new ExpressAdapter(server), { logger: config.LOG_LEVELS });
2527
app.setGlobalPrefix(config.CURRENT_VERSION_PREFIX);
2628
app.useGlobalPipes(
2729
new ValidationPipe({

src/main.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ async function bootstrap() {
1919
API_PREFIX: "/api",
2020
CURRENT_VERSION_PREFIX: "/api" + "/v1",
2121
SWAGGER_PREFIX: "/api" + "/v1" + "/docs",
22+
LOG_LEVELS: configuration()["logLevels"]
2223
};
2324
const server = express();
2425

@@ -44,9 +45,5 @@ async function bootstrap() {
4445
} catch (err) {
4546
BuiltInLogger.log("Could not setup https, skipping.");
4647
}
47-
48-
//const url = await httpServer.;
49-
// Logger.log(`Swagger on: ${url}${config.SWAGGER_PREFIX}`);
50-
// Logger.log(`Application started on: ${url}${config.CURRENT_VERSION_PREFIX}`);
5148
}
5249
void bootstrap();

0 commit comments

Comments
 (0)