npm i babel-plugin-transform-class-properties
"babel-plugin-transform-class-properties": "^6.24.1",
"babel": {
"presets": [
"@vue/app",
[
"@babel/preset-env",
{
"useBuiltIns": "entry"
}
]
],
"plugins":["transform-class-properties"]
},
```js
import Report from '@/utils/sentry'
let sentry = Report.getInstance(Vue, {
env: process.env.ENV,
sys: process.env.VUE_APP_NAME
})
Vue.prototype.$log = sentry
Vue.prototype.$info = sentry.info.bind(sentry)
Vue.prototype.$warn = sentry.warn.bind(sentry)
Vue.prototype.$error = sentry.error.bind(sentry)
import Raven from 'raven-js'
import RavenVue from 'raven-js/plugins/vue'
class Report {
static dsn = 'https://xxxx@xxxx.ingest.sentry.io/5627029'
constructor(Vue, options = {}) {
if (process.env.NODE_ENV === 'production') {
}
this.Vue = Vue
this.options = options
}
static getInstance(Vue, options) {
if (!(this.instance instanceof this)) {
this.instance = new this(Vue, options)
this.instance.install()
this.instance.registerError()
}
return this.instance
}
install() {
Raven.config(Report.dsn, {
release: '1.0.0',
environment: 'production'
})
.addPlugin(RavenVue, this.Vue)
.install()
Raven.setUserContext({
user: this.options.user || ''
})
Raven.setTagsContext({ environment: this.options.env || '', system: this.options.sys })
}
setUser(user) {
Raven.setUserContext({
user: user
})
}
registerError() {
window.onerror = (msg, url, line, col, error) => {
console.log('onerror')
if (msg !== 'Script error.' && !url) {
return true
}
setTimeout(() => {
let data = {}
col = col || (window.event && window.event.errorCharacter) || 0
data.url = url
data.line = line
data.col = col
data.error = error
if (error && error.stack) {
data.msg = error.stack.toString()
}
this.log(data)
}, 0)
return true
}
window.addEventListener('unhandledrejection', err => {
console.log('unhandledrejection')
setTimeout(() => {
this.log(JSON.stringify(err))
}, 0)
})
}
info(data, options) {
this.log(data, 'info', options)
}
warn(data, options) {
this.log(data, 'warning', options)
}
error(data, options) {
this.log(data, 'error', options)
}
log(data = null, type = 'error', options = {}) {
let func = console.log
if (type === 'warning') {
func = console.warn
}
if (type === 'error') {
func = console.error
}
if (Array.isArray(data)) func(...data)
else func(data)
Raven.captureBreadcrumb({
message: data,
category: 'manual message'
})
Raven.captureMessage(data)
if (data instanceof Error) {
Raven.captureException(data, {
level: type,
logger: 'manual exception',
tags: { options: options }
})
} else {
Raven.captureException('error', {
level: type,
logger: 'manual data',
extra: {
data: data,
options: this.options,
date: new Date()
}
})
}
}
}
export default Report