diff options
author | Rigel Kent <sendmemail@rigelk.eu> | 2021-01-25 15:37:41 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-01-25 15:37:41 +0100 |
commit | 2a6cf69cffb83d0fbd73c4a0aabbb94df47b06c8 (patch) | |
tree | 146d9993a7d29cb0d1f35779ac31fc770895e393 | |
parent | ab398a05e9ffaacb8fc713bb2ba9717ac463b34c (diff) | |
download | PeerTube-2a6cf69cffb83d0fbd73c4a0aabbb94df47b06c8.tar.gz PeerTube-2a6cf69cffb83d0fbd73c4a0aabbb94df47b06c8.tar.zst PeerTube-2a6cf69cffb83d0fbd73c4a0aabbb94df47b06c8.zip |
prettify SQL queries during debug (#3635)
* prettify SQL queries during debug
* Use sql-formatter
Co-authored-by: Chocobozzz <me@florianbigard.com>
-rw-r--r-- | package.json | 1 | ||||
-rw-r--r-- | server/helpers/logger.ts | 12 | ||||
-rw-r--r-- | server/initializers/database.ts | 6 | ||||
-rw-r--r-- | yarn.lock | 7 |
4 files changed, 21 insertions, 5 deletions
diff --git a/package.json b/package.json index 26ed6453f..ff03e2be9 100644 --- a/package.json +++ b/package.json | |||
@@ -147,6 +147,7 @@ | |||
147 | "sequelize-typescript": "^2.0.0-beta.1", | 147 | "sequelize-typescript": "^2.0.0-beta.1", |
148 | "sitemap": "^6.1.0", | 148 | "sitemap": "^6.1.0", |
149 | "socket.io": "^3.0.2", | 149 | "socket.io": "^3.0.2", |
150 | "sql-formatter": "^4.0.0-beta.0", | ||
150 | "srt-to-vtt": "^1.1.2", | 151 | "srt-to-vtt": "^1.1.2", |
151 | "tsconfig-paths": "^3.9.0", | 152 | "tsconfig-paths": "^3.9.0", |
152 | "tslib": "^2.0.0", | 153 | "tslib": "^2.0.0", |
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 @@ | |||
1 | // Thanks http://tostring.it/2014/06/23/advanced-logging-with-nodejs/ | 1 | // Thanks http://tostring.it/2014/06/23/advanced-logging-with-nodejs/ |
2 | import { mkdirpSync } from 'fs-extra' | 2 | import { mkdirpSync } from 'fs-extra' |
3 | import { omit } from 'lodash' | ||
3 | import * as path from 'path' | 4 | import * as path from 'path' |
5 | import { format as sqlFormat } from 'sql-formatter' | ||
4 | import * as winston from 'winston' | 6 | import * as winston from 'winston' |
5 | import { FileTransportOptions } from 'winston/lib/winston/transports' | 7 | import { FileTransportOptions } from 'winston/lib/winston/transports' |
6 | import { CONFIG } from '../initializers/config' | 8 | import { CONFIG } from '../initializers/config' |
7 | import { omit } from 'lodash' | ||
8 | import { LOG_FILENAME } from '../initializers/constants' | 9 | import { LOG_FILENAME } from '../initializers/constants' |
9 | 10 | ||
10 | const label = CONFIG.WEBSERVER.HOSTNAME + ':' + CONFIG.WEBSERVER.PORT | 11 | const label = CONFIG.WEBSERVER.HOSTNAME + ':' + CONFIG.WEBSERVER.PORT |
@@ -39,13 +40,20 @@ function getLoggerReplacer () { | |||
39 | } | 40 | } |
40 | 41 | ||
41 | const consoleLoggerFormat = winston.format.printf(info => { | 42 | const consoleLoggerFormat = winston.format.printf(info => { |
42 | const obj = omit(info, 'label', 'timestamp', 'level', 'message') | 43 | const obj = omit(info, 'label', 'timestamp', 'level', 'message', 'sql') |
43 | 44 | ||
44 | let additionalInfos = JSON.stringify(obj, getLoggerReplacer(), 2) | 45 | let additionalInfos = JSON.stringify(obj, getLoggerReplacer(), 2) |
45 | 46 | ||
46 | if (additionalInfos === undefined || additionalInfos === '{}') additionalInfos = '' | 47 | if (additionalInfos === undefined || additionalInfos === '{}') additionalInfos = '' |
47 | else additionalInfos = ' ' + additionalInfos | 48 | else additionalInfos = ' ' + additionalInfos |
48 | 49 | ||
50 | if (info.sql) { | ||
51 | additionalInfos += '\n' + sqlFormat(info.sql, { | ||
52 | language: 'sql', | ||
53 | ident: ' ' | ||
54 | }) | ||
55 | } | ||
56 | |||
49 | return `[${info.label}] ${info.timestamp} ${info.level}: ${info.message}${additionalInfos}` | 57 | return `[${info.label}] ${info.timestamp} ${info.level}: ${info.message}${additionalInfos}` |
50 | }) | 58 | }) |
51 | 59 | ||
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({ | |||
69 | logging: (message: string, benchmark: number) => { | 69 | logging: (message: string, benchmark: number) => { |
70 | if (process.env.NODE_DB_LOG === 'false') return | 70 | if (process.env.NODE_DB_LOG === 'false') return |
71 | 71 | ||
72 | let newMessage = message | 72 | let newMessage = 'Executed SQL request' |
73 | if (isTestInstance() === true && benchmark !== undefined) { | 73 | if (isTestInstance() === true && benchmark !== undefined) { |
74 | newMessage += ' | ' + benchmark + 'ms' | 74 | newMessage += ' in ' + benchmark + 'ms' |
75 | } | 75 | } |
76 | 76 | ||
77 | logger.debug(newMessage) | 77 | logger.debug(newMessage, { sql: message }) |
78 | } | 78 | } |
79 | }) | 79 | }) |
80 | 80 | ||
@@ -7259,6 +7259,13 @@ sprintf-js@~1.0.2: | |||
7259 | resolved "https://registry.yarnpkg.com/sprintf-js/-/sprintf-js-1.0.3.tgz#04e6926f662895354f3dd015203633b857297e2c" | 7259 | resolved "https://registry.yarnpkg.com/sprintf-js/-/sprintf-js-1.0.3.tgz#04e6926f662895354f3dd015203633b857297e2c" |
7260 | integrity sha1-BOaSb2YolTVPPdAVIDYzuFcpfiw= | 7260 | integrity sha1-BOaSb2YolTVPPdAVIDYzuFcpfiw= |
7261 | 7261 | ||
7262 | sql-formatter@^4.0.0-beta.0: | ||
7263 | version "4.0.0-beta.0" | ||
7264 | resolved "https://registry.yarnpkg.com/sql-formatter/-/sql-formatter-4.0.0-beta.0.tgz#4981ecf86268184a61f19745d820a70c947a7d3f" | ||
7265 | integrity sha512-ltWCnnig6UNvnl6MFAoegO13xiET2trcX8Idbq1LBpkCCOn9x/V2Gqs6Md+fGxnpkP8dMtUM1HWHdUYGuEDfSg== | ||
7266 | dependencies: | ||
7267 | argparse "^2.0.1" | ||
7268 | |||
7262 | srt-to-vtt@^1.1.2: | 7269 | srt-to-vtt@^1.1.2: |
7263 | version "1.1.3" | 7270 | version "1.1.3" |
7264 | resolved "https://registry.yarnpkg.com/srt-to-vtt/-/srt-to-vtt-1.1.3.tgz#a9bc16cde5412e000e59ffda469f3e9befed5dde" | 7271 | resolved "https://registry.yarnpkg.com/srt-to-vtt/-/srt-to-vtt-1.1.3.tgz#a9bc16cde5412e000e59ffda469f3e9befed5dde" |