merge upstream 2025-02-03
This commit is contained in:
commit
a4e86758c1
264 changed files with 15775 additions and 4919 deletions
|
|
@ -92,27 +92,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
|
||||
|
|
@ -249,7 +287,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'],
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue