prettify SQL queries during debug (#3635)

* prettify SQL queries during debug

* Use sql-formatter

Co-authored-by: Chocobozzz <me@florianbigard.com>
pull/3383/head
Rigel Kent 2021-01-25 15:37:41 +01:00 committed by GitHub
parent ab398a05e9
commit 2a6cf69cff
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 21 additions and 5 deletions

View File

@ -147,6 +147,7 @@
"sequelize-typescript": "^2.0.0-beta.1",
"sitemap": "^6.1.0",
"socket.io": "^3.0.2",
"sql-formatter": "^4.0.0-beta.0",
"srt-to-vtt": "^1.1.2",
"tsconfig-paths": "^3.9.0",
"tslib": "^2.0.0",

View File

@ -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}`
})

View File

@ -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 })
}
})

View File

@ -7259,6 +7259,13 @@ sprintf-js@~1.0.2:
resolved "https://registry.yarnpkg.com/sprintf-js/-/sprintf-js-1.0.3.tgz#04e6926f662895354f3dd015203633b857297e2c"
integrity sha1-BOaSb2YolTVPPdAVIDYzuFcpfiw=
sql-formatter@^4.0.0-beta.0:
version "4.0.0-beta.0"
resolved "https://registry.yarnpkg.com/sql-formatter/-/sql-formatter-4.0.0-beta.0.tgz#4981ecf86268184a61f19745d820a70c947a7d3f"
integrity sha512-ltWCnnig6UNvnl6MFAoegO13xiET2trcX8Idbq1LBpkCCOn9x/V2Gqs6Md+fGxnpkP8dMtUM1HWHdUYGuEDfSg==
dependencies:
argparse "^2.0.1"
srt-to-vtt@^1.1.2:
version "1.1.3"
resolved "https://registry.yarnpkg.com/srt-to-vtt/-/srt-to-vtt-1.1.3.tgz#a9bc16cde5412e000e59ffda469f3e9befed5dde"