Skip to content

Commit

Permalink
feat: enable sql logger (#460)
Browse files Browse the repository at this point in the history
And fix registry error response cause data return null bug
  • Loading branch information
fengmk2 authored May 6, 2023
1 parent a647317 commit 51cd044
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 10 deletions.
12 changes: 12 additions & 0 deletions app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,18 @@ export default class CnpmcoreAppHook {
this.app.binaryHTML = '';
}

async configWillLoad() {
const app = this.app;
// https://github.com/eggjs/tegg/blob/master/plugin/orm/app.ts#L37
// store query sql to log
app.config.orm.logger = {
...app.config.orm.logger,
logQuery(sql: string, duration: number) {
app.getLogger('sqlLogger').info('[%s] %s', duration, sql);
},
};
}

// https://eggjs.org/zh-cn/basics/app-start.html
async didReady() {
// ready binary.html and replace registry
Expand Down
15 changes: 15 additions & 0 deletions app/core/service/PackageSyncerService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -425,6 +425,21 @@ export class PackageSyncerService extends AbstractService {
}

const { url, data, headers, res, status } = registryFetchResult;
/* c8 ignore next 13 */
if (status >= 500 || !data) {
// GET https://registry.npmjs.org/%40modern-js%2Fstyle-compiler?t=1683348626499&cache=0, status: 522
// registry will response status 522 and data will be null
// > TypeError: Cannot read properties of null (reading 'readme')
task.error = `request manifests response error, status: ${status}, data: ${JSON.stringify(data)}`;
logs.push(`[${isoNow()}] ❌ response headers: ${JSON.stringify(headers)}`);
logs.push(`[${isoNow()}] ❌ Synced ${fullname} fail, ${task.error}, log: ${logUrl}`);
logs.push(`[${isoNow()}] ❌❌❌❌❌ ${fullname} ❌❌❌❌❌`);
this.logger.info('[PackageSyncerService.executeTask:fail-request-error] taskId: %s, targetName: %s, %s',
task.taskId, task.targetName, task.error);
await this.taskService.retryTask(task, logs.join('\n'));
return;
}

let readme = data.readme || '';
if (typeof readme !== 'string') {
readme = JSON.stringify(readme);
Expand Down
16 changes: 7 additions & 9 deletions config/config.default.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,16 +102,8 @@ export default (appInfo: EggAppConfig) => {
user: process.env.MYSQL_USER || 'root',
password: process.env.MYSQL_PASSWORD,
charset: 'utf8mb4',
logger: {},
};
/* c8 ignore next 8 */
if (process.env.DEBUG_LOCAL_SQL) {
config.orm.logger = {
// TODO: try to save SQL log into ctx logger or app logger
logQuery(sql: string, duration: number) {
console.log('[sql-debug] [%sms] %s', duration, sql);
},
};
}

config.redis = {
client: {
Expand Down Expand Up @@ -188,5 +180,11 @@ export default (appInfo: EggAppConfig) => {
defaultViewEngine: 'nunjucks',
};

config.customLogger = {
sqlLogger: {
file: 'sql.log',
},
};

return config;
};
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
},
"scripts": {
"contributor": "git-contributor",
"dev": "DEBUG_LOCAL_SQL=true egg-bin dev",
"dev": "egg-bin dev",
"lint": "eslint --cache --ext .ts .",
"lint:fix": "eslint --cache --ext .ts --fix .",
"test": "npm run lint:fix && npm run test-local",
Expand Down

0 comments on commit 51cd044

Please sign in to comment.