Skip to content

Commit

Permalink
feat: 增加app.logger.errorAndSentry,记录error并在prod环境下发送sentry;
Browse files Browse the repository at this point in the history
增加app.logger.errorAndSentry,记录error并在prod环境下发送sentry;
  • Loading branch information
Imfdj committed Apr 21, 2021
1 parent 0094c3c commit b355184
Show file tree
Hide file tree
Showing 7 changed files with 27 additions and 34 deletions.
20 changes: 11 additions & 9 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const dayjs = require('dayjs');
const path = require('path');
const fs = require('fs');
const { permissionsToRedis } = require('./app-boot-hook-do');
const Sentry = require('@sentry/node');

class AppBootHook {
constructor(app) {
Expand Down Expand Up @@ -45,6 +46,15 @@ class AppBootHook {

// 例如:从数据库加载数据到内存缓存
// this.app.cacheData = await this.app.model.query(QUERY_CACHE_SQL);

Sentry.init({
dsn: 'http://[email protected]/4',
});
// logger记录error并在prod环境下发送错误到Sentry
this.app.logger.errorAndSentry = (msg, ...arg) => {
this.app.config.env === 'prod' ? Sentry.captureException(msg) : null;
this.app.logger.error(msg, ...arg);
};
this.app.lodash = lodash;
this.app.uuidv4 = uuidv4;
this.app.dayjs = dayjs;
Expand All @@ -54,16 +64,8 @@ class AppBootHook {
// 资源数据缓存到redis
await permissionsToRedis(this.app);

Sequelize.addHook('beforeValidate', (permission, options) => {
// 做些什么
});
Sequelize.addHook('afterBulkUpdate', options => {
console.log(2222222);
console.log(options);
});
Sequelize.addHook('afterSave', (permission, options) => {
console.log(333333);
console.log(permission);
// 做些什么
});
}

Expand Down
7 changes: 4 additions & 3 deletions app/controller/v1/users.js
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ class RoleController extends Controller {
ctx.request.body.password = key.decrypt(ctx.request.body.password, 'utf8');
} catch (e) {
ctx.helper.body.UNAUTHORIZED({ ctx });
return ctx.logger.error(e);
return app.logger.errorAndSentry(e);
}
}

Expand Down Expand Up @@ -257,7 +257,7 @@ class RoleController extends Controller {
// 如果验证方式是jwt,否则为session
if (app.config.verification_mode === 'jwt') {
const res = await service.users.logout();
if (res !== 'OK') ctx.logger.error(res);
if (res !== 'OK') app.logger.error(res);
ctx.helper.body.SUCCESS({ ctx });
} else {
ctx.session = null;
Expand Down Expand Up @@ -427,6 +427,7 @@ class RoleController extends Controller {
const {
ctx,
service,
app,
app: {
config: { github },
},
Expand Down Expand Up @@ -484,7 +485,7 @@ class RoleController extends Controller {
}
} catch (e) {
ctx.helper.body.UNAUTHORIZED({ ctx, res: e.res });
ctx.logger.error(e);
app.logger.errorAndSentry(e);
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions app/extend/helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ module.exports = {
});
});
} catch (e) {
app.logger.error(e);
app.logger.errorAndSentry(e);
}
},
/**
Expand All @@ -51,7 +51,7 @@ module.exports = {
// 存入redis,接收到ACK则删除,否则在 this.app.config.socketRedisExp 时间内多次重发
app.redis.setex(redisKeys.socketBaseSocketId(_message.id), app.config.socketRedisExp, JSON.stringify(emitData));
} catch (e) {
app.logger.error(e);
app.logger.errorAndSentry(e);
}
},
};
Expand Down
4 changes: 2 additions & 2 deletions app/io/middleware/connection.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ module.exports = app => {
attributes: ['project_id'],
});
userProjects.forEach(item => {
const roomName = `${ app.config.socketProjectRoomNamePrefix }${ item.project_id }`;
const roomName = `${app.config.socketProjectRoomNamePrefix}${item.project_id}`;
socket.join(roomName, () => {
// setTimeout(() => {
// nsp.to(roomName).emit(`a new user has joined the room: ${ socket.id }`);
Expand All @@ -45,7 +45,7 @@ module.exports = app => {
});
});
} catch (err) {
logger.error(err);
app.logger.errorAndSentry(err);
socket.emit('disconnect', 'disconnect!');
socket.disconnect();
}
Expand Down
16 changes: 1 addition & 15 deletions app/middleware/error_handler.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,5 @@
'use strict';
const Sentry = require('@sentry/node');
// const Tracing = require("@sentry/tracing");
Sentry.init({
// dsn: "https://[email protected]/5553058",
dsn: 'http://[email protected]/4',

// We recommend adjusting this value in production, or using tracesSampler
// for finer control
// tracesSampleRate: 1.0,
});
// const transaction = Sentry.startTransaction({
// op: "test",
// name: "My First Test Transaction",
// });
module.exports = (option, app) => {
return async function(ctx, next) {
try {
Expand Down Expand Up @@ -49,8 +36,7 @@ module.exports = (option, app) => {
};
ctx.helper.body.VALIDATION_FAILED({ ctx, res });
} else {
app.config.env === 'prod' ? Sentry.captureException(err) : null;
// transaction.finish();
app.logger.errorAndSentry(err);
}
}
};
Expand Down
4 changes: 4 additions & 0 deletions app/model/messages.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ module.exports = app => {
// 当此用户在线,则发送消息
if (clients.includes(receiver_id.toString())) {
const socket = nsp.sockets[receiver_id];
if (!socket) {
app.logger.error(nsp.sockets);
app.logger.error(receiver_id);
}
ctx.helper.sendMessageToSocket(socket, newMessage, 'create:message');
}
});
Expand Down
6 changes: 3 additions & 3 deletions app/service/users.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ class UserService extends Service {
return res_user;
} catch (e) {
await transaction.rollback();
ctx.logger.error(e);
app.logger.errorAndSentry(e);
}
}
return false;
Expand Down Expand Up @@ -299,7 +299,7 @@ class UserService extends Service {
}

async githubLogin(payload) {
const { ctx } = this;
const { ctx, app } = this;
const { login, id, avatar_url, name, company, location, email } = payload;
let user = await ctx.model.Users.findOne({
where: {
Expand Down Expand Up @@ -353,7 +353,7 @@ class UserService extends Service {
});
} catch (e) {
await transaction.rollback();
ctx.logger.error(e);
app.logger.errorAndSentry(e);
return {
__code_wrong: 40000,
};
Expand Down

0 comments on commit b355184

Please sign in to comment.