diff --git a/.config/example.yml b/.config/example.yml index 944e51965..f7bd14292 100644 --- a/.config/example.yml +++ b/.config/example.yml @@ -57,6 +57,18 @@ db: # host: localhost # rejectUnauthorized: false + # You can enable different different logging levels by setting the value of logging to any of the values listed below + # * 'error' - logs all failed queries and errors + # * 'slow' - logs slow queries + # * 'query' - logs all queries + # * 'schema' - logs the schema build process + # * 'info' - logs internal orm informative messages + # * 'log' - logs internal orm log messages + # You can set multiple log level by specifying them as an array i.e ['log', 'info'] + # You can set disable all log levels by specifying an empty array: [] + # You can set enable all log levels by specifying the special value: 'all' + logging: ['error', 'slow'] + # ┌─────────────────────┐ #───┘ Redis configuration └───────────────────────────────────── diff --git a/packages/backend/src/config/types.ts b/packages/backend/src/config/types.ts index b9f76519e..a8566cb31 100644 --- a/packages/backend/src/config/types.ts +++ b/packages/backend/src/config/types.ts @@ -1,6 +1,17 @@ /** * ユーザーが設定する必要のある情報 */ + +export type TypeORMLoggingOptions = + | "error" + | "slow" + | "query" + | "schema" + | "info" + | "log"; + +export type TypeORMLoggingConfig = TypeORMLoggingOptions | TypeORMLoggingOptions[] | "all"; + export type Source = { repository_url?: string; feedback_url?: string; @@ -9,6 +20,7 @@ export type Source = { port: number; disableHsts?: boolean; db: { + logging?: TypeORMLoggingConfig; host: string; port: number; db: string; diff --git a/packages/backend/src/db/postgre.ts b/packages/backend/src/db/postgre.ts index 3e3f3a289..e1db91fc2 100644 --- a/packages/backend/src/db/postgre.ts +++ b/packages/backend/src/db/postgre.ts @@ -76,8 +76,22 @@ import { OAuthApp } from "@/models/entities/oauth-app.js"; import { OAuthToken } from "@/models/entities/oauth-token.js"; import { HtmlNoteCacheEntry } from "@/models/entities/html-note-cache-entry.js"; import { HtmlUserCacheEntry } from "@/models/entities/html-user-cache-entry.js"; +import { TypeORMLoggingOptions } from "@/config/types.js"; const sqlLogger = dbLogger.createSubLogger("sql", "gray", false); +const isLogEnabled = (level: TypeORMLoggingOptions): boolean => { + const logLevel = config.db.logging; + return Array.isArray(logLevel) + ? logLevel.includes(level) + : logLevel === level || logLevel?.trim()?.toLowerCase() === "all"; +}; +const isLoggingEnabled = () => { + const logLevel = config.db.logging; + return Array.isArray(logLevel) + ? logLevel.length > 0 + : logLevel != null; +} + class MyCustomLogger implements Logger { private highlight(sql: string) { return highlight.highlight(sql, { @@ -87,27 +101,28 @@ class MyCustomLogger implements Logger { } public logQuery(query: string, parameters?: any[]) { - sqlLogger.info(this.highlight(query).substring(0, 100)); + if (isLogEnabled("query")) + sqlLogger.info(this.highlight(query).substring(0, 100)); } public logQueryError(error: string, query: string, parameters?: any[]) { - sqlLogger.error(this.highlight(query)); + if (isLogEnabled("error")) sqlLogger.error(this.highlight(query)); } public logQuerySlow(time: number, query: string, parameters?: any[]) { - sqlLogger.warn(this.highlight(query)); + if (isLogEnabled("slow")) sqlLogger.warn(this.highlight(query)); } public logSchemaBuild(message: string) { - sqlLogger.info(message); + if (isLogEnabled("schema")) sqlLogger.info(message); } public log(message: string) { - sqlLogger.info(message); + if (isLogEnabled("log")) sqlLogger.info(message); } public logMigration(message: string) { - sqlLogger.info(message); + if (isLogEnabled("info")) sqlLogger.info(message); } } @@ -182,7 +197,7 @@ export const entities = [ ...charts, ]; -const log = process.env.LOG_SQL === "true"; +const log = isLoggingEnabled() || process.env.LOG_SQL === "true"; export const db = new DataSource({ type: "postgres", @@ -213,7 +228,7 @@ export const db = new DataSource({ } : false, logging: log, - logger: log ? new MyCustomLogger() : undefined, + logger: new MyCustomLogger(), maxQueryExecutionTime: 300, entities: entities, migrations: ["../../migration/*.js"],