From 135d5a89cd1f49516dc65500b56b4a081ec6cb14 Mon Sep 17 00:00:00 2001 From: Ahsan Virani Date: Tue, 1 Feb 2022 11:27:08 +0100 Subject: [PATCH] :zap: Add debug log in healthcheck and enable DB logging via environment (#2744) * :zap: add debug log in healthcheck * :zap: add DB logging * CR --- packages/cli/config/index.ts | 20 ++++++++++++++++++++ packages/cli/src/Db.ts | 23 +++++++++++++++++++++-- packages/cli/src/Server.ts | 4 ++++ 3 files changed, 45 insertions(+), 2 deletions(-) diff --git a/packages/cli/config/index.ts b/packages/cli/config/index.ts index 32d67042fa..08906c7375 100644 --- a/packages/cli/config/index.ts +++ b/packages/cli/config/index.ts @@ -22,6 +22,26 @@ const config = convict({ default: '', env: 'DB_TABLE_PREFIX', }, + logging: { + enabled: { + doc: 'Typeorm logging enabled flag.', + format: 'Boolean', + default: false, + env: 'DB_LOGGING_ENABLED', + }, + options: { + doc: 'Logging level options, default is "error". Possible values: query,error,schema,warn,info,log. To enable all logging, specify "all"', + format: String, + default: 'error', + env: 'DB_LOGGING_OPTIONS', + }, + maxQueryExecutionTime: { + doc: 'Maximum number of milliseconds query should be executed before logger logs a warning. Set 0 to disable long running query warning', + format: Number, + default: 1000, + env: 'DB_LOGGING_MAX_EXECUTION_TIME', + }, + }, postgresdb: { database: { doc: 'PostgresDB Database', diff --git a/packages/cli/src/Db.ts b/packages/cli/src/Db.ts index c8b6a624e0..cf5f368494 100644 --- a/packages/cli/src/Db.ts +++ b/packages/cli/src/Db.ts @@ -3,7 +3,7 @@ /* eslint-disable no-case-declarations */ /* eslint-disable @typescript-eslint/naming-convention */ import { UserSettings } from 'n8n-core'; -import { ConnectionOptions, createConnection, getRepository } from 'typeorm'; +import { ConnectionOptions, createConnection, getRepository, LoggerOptions } from 'typeorm'; import { TlsOptions } from 'tls'; import * as path from 'path'; // eslint-disable-next-line import/no-cycle @@ -104,10 +104,29 @@ export async function init(): Promise { throw new Error(`The database "${dbType}" is currently not supported!`); } + let loggingOption: LoggerOptions = (await GenericHelpers.getConfigValue( + 'database.logging.enabled', + )) as boolean; + + if (loggingOption) { + const optionsString = ( + (await GenericHelpers.getConfigValue('database.logging.options')) as string + ).replace(/\s+/g, ''); + + if (optionsString === 'all') { + loggingOption = optionsString; + } else { + loggingOption = optionsString.split(',') as LoggerOptions; + } + } + Object.assign(connectionOptions, { entities: Object.values(entities), synchronize: false, - logging: false, + logging: loggingOption, + maxQueryExecutionTime: (await GenericHelpers.getConfigValue( + 'database.logging.maxQueryExecutionTime', + )) as string, }); let connection = await createConnection(connectionOptions); diff --git a/packages/cli/src/Server.ts b/packages/cli/src/Server.ts index 2fb3d71077..9ecf248057 100644 --- a/packages/cli/src/Server.ts +++ b/packages/cli/src/Server.ts @@ -615,6 +615,8 @@ class App { // Does very basic health check this.app.get('/healthz', async (req: express.Request, res: express.Response) => { + LoggerProxy.debug('Health check started!'); + const connection = getConnectionManager().get(); try { @@ -635,6 +637,8 @@ class App { status: 'ok', }; + LoggerProxy.debug('Health check completed successfully!'); + ResponseHelper.sendSuccessResponse(res, responseData, true, 200); });