vue+vite+sentry

查看搭建sentry

創(chuàng)建sentry project

圖片.png

創(chuàng)建后在代碼中引入sentry的方式有cdn和npm,建議使用cdn,npm有安全令牌不存在及跨域問題,通過代理可解決跨域問題,但安全令牌問題不知道怎么解決,直接使用cdn引入則沒有任何問題

sourcemap自動上傳

  • 安裝vite-plugin-sentry
npm i vite-plugin-sentry -D

-在vite.config.js中引入

import { defineConfig, loadEnv } from 'vite'
import viteSentry from 'vite-plugin-sentry'
const srcPath = path.resolve(__dirname, "src");
// https://vitejs.dev/config/
export default defineConfig(({ command, mode }) => ({
  plugins: [
    viteSentry({
      url: 'http://ip:9000',
      authToken: '...',  //sentry授權(quán)令牌
      org: 'sentry',
      project: loadEnv(mode, process.cwd()).VITE_SENTRY_PROJECT,
      release: loadEnv(mode, process.cwd()).VITE_SENTRY_VERSION,
      deploy: {
        env: 'production'
      },
      setCommits: {
        auto: true
      },
      sourceMaps: {
        include: ['./dist/assets'],
        ignore: ['node_modules'],
        urlPrefix: '~/assets'
      }
    }),
    vue(),
  ],
  ...
   build: {
    sourcemap: true
  }

-在.env.production中添加sentry配置

VITE_SENTRY_DSN:'',  //項(xiàng)目dsn,從sentry后臺獲取
VITE_SENTRY_PROJECT:'Vue',    //同上圖創(chuàng)建sentry project所填
VITE_SENTRY_VERSION:'product@0.0.1', //自定義的release版本,上傳sourcemap后可在sentry中項(xiàng)目的版本中看到
  • 在main.js中添加init代碼
if (!import.meta.env.DEV) {
        Sentry.init({
            app,
            dsn: import.meta.env.VITE_SENTRY_DSN,
            release: import.meta.env.VITE_SENTRY_VERSION,
            environment: 'production',
            integrations: [new Sentry.Integrations.Vue({ app, attachProps: true })],
            tracesSampler: samplingContext => {
                return 1;  //按需設(shè)置0至1
            }
        })
}
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容