feat(backend): config(default.yml)からSQLログ全文を出力するか否かを設定可能に (#15268)
* feature(backend): config(default.yml)からSQLログ全文を出力するか否かを設定可能に * disableHighlightやめる * refactor
This commit is contained in:
@@ -89,27 +89,65 @@ export const dbLogger = new MisskeyLogger('db');
|
||||
|
||||
const sqlLogger = dbLogger.createSubLogger('sql', 'gray');
|
||||
|
||||
export type LoggerProps = {
|
||||
disableQueryTruncation?: boolean;
|
||||
enableQueryParamLogging?: boolean;
|
||||
}
|
||||
|
||||
function highlightSql(sql: string) {
|
||||
return highlight.highlight(sql, {
|
||||
language: 'sql', ignoreIllegals: true,
|
||||
});
|
||||
}
|
||||
|
||||
function truncateSql(sql: string) {
|
||||
return sql.length > 100 ? `${sql.substring(0, 100)}...` : sql;
|
||||
}
|
||||
|
||||
function stringifyParameter(param: any) {
|
||||
if (param instanceof Date) {
|
||||
return param.toISOString();
|
||||
} else {
|
||||
return param;
|
||||
}
|
||||
}
|
||||
|
||||
class MyCustomLogger implements Logger {
|
||||
constructor(private props: LoggerProps = {}) {
|
||||
}
|
||||
|
||||
@bindThis
|
||||
private highlight(sql: string) {
|
||||
return highlight.highlight(sql, {
|
||||
language: 'sql', ignoreIllegals: true,
|
||||
});
|
||||
private transformQueryLog(sql: string) {
|
||||
let modded = sql;
|
||||
if (!this.props.disableQueryTruncation) {
|
||||
modded = truncateSql(modded);
|
||||
}
|
||||
|
||||
return highlightSql(modded);
|
||||
}
|
||||
|
||||
@bindThis
|
||||
private transformParameters(parameters?: any[]) {
|
||||
if (this.props.enableQueryParamLogging && parameters && parameters.length > 0) {
|
||||
return parameters.map(stringifyParameter);
|
||||
}
|
||||
|
||||
return undefined;
|
||||
}
|
||||
|
||||
@bindThis
|
||||
public logQuery(query: string, parameters?: any[]) {
|
||||
sqlLogger.info(this.highlight(query).substring(0, 100));
|
||||
sqlLogger.info(this.transformQueryLog(query), this.transformParameters(parameters));
|
||||
}
|
||||
|
||||
@bindThis
|
||||
public logQueryError(error: string, query: string, parameters?: any[]) {
|
||||
sqlLogger.error(this.highlight(query));
|
||||
sqlLogger.error(this.transformQueryLog(query), this.transformParameters(parameters));
|
||||
}
|
||||
|
||||
@bindThis
|
||||
public logQuerySlow(time: number, query: string, parameters?: any[]) {
|
||||
sqlLogger.warn(this.highlight(query));
|
||||
sqlLogger.warn(this.transformQueryLog(query), this.transformParameters(parameters));
|
||||
}
|
||||
|
||||
@bindThis
|
||||
@@ -247,7 +285,12 @@ export function createPostgresDataSource(config: Config) {
|
||||
},
|
||||
} : false,
|
||||
logging: log,
|
||||
logger: log ? new MyCustomLogger() : undefined,
|
||||
logger: log
|
||||
? new MyCustomLogger({
|
||||
disableQueryTruncation: config.logging?.sql?.disableQueryTruncation,
|
||||
enableQueryParamLogging: config.logging?.sql?.enableQueryParamLogging,
|
||||
})
|
||||
: undefined,
|
||||
maxQueryExecutionTime: 300,
|
||||
entities: entities,
|
||||
migrations: ['../../migration/*.js'],
|
||||
|
Reference in New Issue
Block a user