From 2a6cf69cffb83d0fbd73c4a0aabbb94df47b06c8 Mon Sep 17 00:00:00 2001 From: Rigel Kent Date: Mon, 25 Jan 2021 15:37:41 +0100 Subject: prettify SQL queries during debug (#3635) * prettify SQL queries during debug * Use sql-formatter Co-authored-by: Chocobozzz --- server/helpers/logger.ts | 12 ++++++++++-- server/initializers/database.ts | 6 +++--- 2 files changed, 13 insertions(+), 5 deletions(-) (limited to 'server') diff --git a/server/helpers/logger.ts b/server/helpers/logger.ts index a4bd41427..f1808849e 100644 --- a/server/helpers/logger.ts +++ b/server/helpers/logger.ts @@ -1,10 +1,11 @@ // Thanks http://tostring.it/2014/06/23/advanced-logging-with-nodejs/ import { mkdirpSync } from 'fs-extra' +import { omit } from 'lodash' import * as path from 'path' +import { format as sqlFormat } from 'sql-formatter' import * as winston from 'winston' import { FileTransportOptions } from 'winston/lib/winston/transports' import { CONFIG } from '../initializers/config' -import { omit } from 'lodash' import { LOG_FILENAME } from '../initializers/constants' const label = CONFIG.WEBSERVER.HOSTNAME + ':' + CONFIG.WEBSERVER.PORT @@ -39,13 +40,20 @@ function getLoggerReplacer () { } const consoleLoggerFormat = winston.format.printf(info => { - const obj = omit(info, 'label', 'timestamp', 'level', 'message') + const obj = omit(info, 'label', 'timestamp', 'level', 'message', 'sql') let additionalInfos = JSON.stringify(obj, getLoggerReplacer(), 2) if (additionalInfos === undefined || additionalInfos === '{}') additionalInfos = '' else additionalInfos = ' ' + additionalInfos + if (info.sql) { + additionalInfos += '\n' + sqlFormat(info.sql, { + language: 'sql', + ident: ' ' + }) + } + return `[${info.label}] ${info.timestamp} ${info.level}: ${info.message}${additionalInfos}` }) diff --git a/server/initializers/database.ts b/server/initializers/database.ts index 128ed5b75..61768234f 100644 --- a/server/initializers/database.ts +++ b/server/initializers/database.ts @@ -69,12 +69,12 @@ const sequelizeTypescript = new SequelizeTypescript({ logging: (message: string, benchmark: number) => { if (process.env.NODE_DB_LOG === 'false') return - let newMessage = message + let newMessage = 'Executed SQL request' if (isTestInstance() === true && benchmark !== undefined) { - newMessage += ' | ' + benchmark + 'ms' + newMessage += ' in ' + benchmark + 'ms' } - logger.debug(newMessage) + logger.debug(newMessage, { sql: message }) } }) -- cgit v1.2.3