1 |
|
2 |
|
3 |
|
4 |
|
5 | import { IS_DEBUG } from './../utils/env'
|
6 |
|
7 | class Logger {
|
8 | |
9 |
|
10 |
|
11 |
|
12 | constructor(filename) {
|
13 | this.filename = filename
|
14 | }
|
15 | }
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 | const methods = {
|
22 | log: 'color: green',
|
23 | info: 'color:#6495ED',
|
24 | warn: 'color: #cc33cc',
|
25 | error: 'color: red',
|
26 | }
|
27 |
|
28 |
|
29 |
|
30 | Object.entries(methods).forEach(([method, color]) => {
|
31 | Logger.prototype[method] = function (...args) {
|
32 |
|
33 | if (!IS_DEBUG) {
|
34 | return
|
35 | }
|
36 | args.unshift(`%c[${this.filename}]:`, color)
|
37 | console[method](...args)
|
38 | }
|
39 | })
|
40 |
|
41 |
|
42 |
|
43 |
|
44 | class VueLogger {
|
45 | constructor(filename) {
|
46 | const logger = new Logger(filename)
|
47 | Object.keys(methods).forEach(method => {
|
48 | this[method] = (vm, ...args) => {
|
49 |
|
50 | if (!vm) {
|
51 | return
|
52 | }
|
53 | if (vm._isVue !== true) {
|
54 | logger[method].call(logger, vm, ...args)
|
55 | return
|
56 | }
|
57 |
|
58 | const filePath = vm.$options._parentVnode.componentOptions.Ctor.options.__file
|
59 |
|
60 | const componentName = filePath.slice(filePath.lastIndexOf('/') + 1) || '(unkonwn component name)'
|
61 |
|
62 | const fullPath = vm.$f7Route ? vm.$f7Route.url : '/'
|
63 | logger[method].call(logger, ...args, `@${componentName}`, `#${fullPath}`)
|
64 | }
|
65 | })
|
66 | }
|
67 | }
|
68 |
|
69 | const vueLogger = new VueLogger('component')
|
70 | const componentLogger = {}
|
71 |
|
72 |
|
73 |
|
74 |
|
75 | componentLogger.install = function (Vue) {
|
76 | Object.keys(methods).forEach((method) => {
|
77 | Vue.prototype[method] = function (...args) {
|
78 | vueLogger[method].call({}, this, ...args)
|
79 | }
|
80 | })
|
81 | }
|
82 |
|
83 | export default componentLogger |
\ | No newline at end of file |