sentry日志 3.1 sentry是什么?
sentry是一个开源的实时错误报告工具,支持web前后端,移动应用以及游戏,支持Python, OC,Java,Go, Node, Django等主流编程语言和框架,还提供了Github、Slack、Trello等常见开发工具的集成。
3.2 文档地址
官网地址:https://sentry.io/welcome/
github地址:https://github.com/getsentry/sentry
整体项目是用django开发的后端+django的后端模板渲染的前端。
3.3 安装方法
这边用官方的docker-compose的方式进行安装,关于其他安装方法,随便搜,毕竟知名呀,如果你以前没用过,说明要赶紧的了哈。
官方的一个方式:
步骤一:
git clone代码.仓库地址: https://github.com/getsentry/onpremise.git,然后下载完代码后进入这个目录进行操作。
git clone https://github.com/getsentry/onpremise.git 步骤二:
docker volume create --name=sentry-data && docker volume create --name=sentry-postgres 步骤三: 把他的环境配置文件拷贝一份,修改。
cp -n .env.example .env 步骤四: 执行build
docker-compose build 步骤五: 产生一个key。
docker-compose run --rm web config generate-secret-key 会提示给我们一个key的。
然后将这个key,修改vim .env然后填进去。
步骤六: 数据库orm的执行,生成数据库表和数据库初始数据
docker-compose run --rm web upgrade 然后他会提示我们创建一个账号,那这时大家输入一个账号。
步骤七: 运行
docker-compose up -d 步骤八: 打开我们的网页访问: http://127.0.0.1:9000,看到以下界面:
然后我们输入账号密码后,中间有一个配置页面跳过,然后我们进入后就能看到如下界面:
image.png image.png 然后直接进入实战吧。
四、Node基础应用篇
4.1.1 新建项目
我们打开sentry网站,如下图,点击新建一个项目:
image.png
然后我们选择nodejs image.png
然后我们就到了这个页面: image.png image.png 4.1.2 新建代码工程
我们选择一个自己的目录,然后创建一个文件夹。
mkdir -p hello_world && cd hello_world 然后我们执行下面命令来初始化一个npm项目
npm init -y 然后我们新建一个index.js文件,并且安装@sentry/node这个库
npm install @sentry/node -s 然后index.js的代码如下:
const Sentry = require('@sentry/node'); Sentry.init({ dsn: 'http://[email protected]:9000//3' }); 4.1.3 构建异常
我们修改index.js文件
const Sentry = require('@sentry/node'); Sentry.init({ dsn: 'http://[email protected]:9000//3' });
throw new Error("hello") 然后运行这个代码.
node ./index.js 4.1.4 查看异常
然后我们点开刚刚项目页面会看到异常已经报告上来了。
点击查看详情: image.png image.png 4.1.5 解决问题
然后根据异常,我们就需要去解决代码里面这个异常。比如代码修改成下面这样:
const Sentry = require('@sentry/node'); Sentry.init({ dsn: 'http://[email protected]:9000//3' }); console.log("hello world") 然后再执行,问题就不上报了。
然后同时我们把刚刚这个问题给resolve掉。
image.png image.png
最终确保我们项目页面异常是空的,否则虽然报错了,但是问题又不改,那这个平台的价值就没了。
五、Node + Typescript + sourceMap篇
上面我们介绍了Node.js的基础使用方法,下面我们介绍一下Node + typescript + sourcemap的情况。 前两步会跟上面差不多。
5.1.1 新建项目
我们打开sentry网站,点击新建一个nodejs项目
image.png image.png
然后我们就到了这个页面: image.png image.png 5.1.2 新建代码工程
我们选择一个自己的目录,然后创建一个文件夹。
mkdir -p hello_world && cd hello_world 然后我们执行下面命令来初始化一个npm项目,这步开始跟基础篇不一样,然后tsc --init会给我们生成一个tsconfig.json文件
npm init -y && tsc --init 修改tsconfig.json文件内容:
{ "compilerOptions": { "target": "es2017", "module": "commonjs", "sourceMap": true, "outDir": "./dist", "baseUrl": ".", "inlineSources": true, "strict": true, "esModuleInterop": true } } 然后我们新建一个index.js文件,并且安装@sentry/node这个库
npm install @sentry/node -s 然后index.ts的代码如下: 此处代码也不一样了
import * as Sentry from '@sentry/node' import * as Integrations from '@sentry/integrations'; Sentry.init({ dsn: 'http://[email protected]:9000//3', release: 'dev' ,integrations: [ new Integrations.RewriteFrames() ]}); 5.1.3 构建异常
为了后续根据js和sourcemap还原ts文件,还需要安装@sentry/integrations
npm install @sentry/integrations -s 然后修改index.ts文件
import * as Sentry from '@sentry/node' import * as Integrations from '@sentry/integrations'; Sentry.init({ dsn: 'http://[email protected]:9000//3', release: 'dev' ,integrations: [ new Integrations.RewriteFrames() ]});
throw new Error("hello123") 然后我们可以首先运行ts版本的:
ts-node ./index.ts 然后我们来看平台上确实查看到了这个报错,虽然下面有报错,这个报错原因是没有node_modules:
image.png image.png 5.1.4 编译typescript代码
我们除了本地环境以外,测试、线上都只会跑js版本。因为会将ts代码编译js。 执行编译
tsc -p ./tsconfig.json 然后会生成js和sourcemap文件。
5.1.5 上传代码准备
上传这个我们需要做一个准备,在工程目录下,我们创建一个文件: 文件名: .sentryclirc
[auth] token=fc8e6e2e3d12427fb005105694ac576593578d0f667946b0adbffc855c6cf39f
[defaults] url=http://127.0.0.1:9000/ org=sentry project=hello url的话,是sentry网站的地址 org:项目有个org的,拷贝过来 project:项目名字 token创建方法:
image.png image.png
新建一个token,并且主要勾选权限。 image.png image.png 5.1.6 安装上传工具
上传工具:
npm install @sentry/cli -g 5.1.7 上传js和sourcemap
首先我们创建一个release,取名: dev
sentry-cli releases new dev 然后执行上传:
sentry-cli releases files dev upload-sourcemaps --rewrite dist 上传完毕:
image.png image.png 5.1.8 测试异常
然后我们跑编译后的,模拟测试服和线上服。 运行方式:
node ./dist/index.js 然后我们去平台上查看:
image.png image.png 5.1.9 解决问题
然后根据异常,我们就需要去解决代码里面这个异常。比如代码修改成下面这样:
import * as Sentry from '@sentry/node' import * as Integrations from '@sentry/integrations';
Sentry.init({ dsn: 'http://[email protected]:9000//3', release: 'dev3' ,integrations: [ new Integrations.RewriteFrames() ]});
console.log("hello world") 然后再执行,问题就不上报了。
然后同时我们把刚刚这个问题给resolve掉。
image.png image.png
最终确保我们项目页面异常是空的,否则虽然报错了,但是问题又不改,那这个平台的价值就没了。 六、其他的使用指南
文章目录其实很长,暂时只整理了Node相关的一些使用,比如react,vue,angular这类前端项目也可以用,electron这类的跨平台的也可以用。
关于Sentry这样的错误监控,在项目开发中挺有用的,有些公司采用自建,有些是用这类开源项目二次开发,所以还没学会的同学,赶紧试试吧。
七、结论
本文其实也只是讲解了sentry这块的错误监控的一小块,例如react这类前端项目里面也可以用。还有邮件报警等等。