/
logger.ts
171 lines (129 loc) · 3.99 KB
/
logger.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
/* eslint-disable no-console */
import _ from 'lodash'
interface Table {
name: string
data: object
columns: any
}
interface Group {
name: string
items?: any
label?: boolean
expand?: boolean
table?: boolean
}
export const logger = {
log (...args: unknown[]) {
console.log(...args)
},
logError (...args: unknown[]) {
console.error(...args)
},
clearLog () {
console.clear?.()
},
logFormatted (consoleProps: any) {
if (_.isEmpty(consoleProps)) return
this._logValues(consoleProps)
this._logArgs(consoleProps)
this._logGroups(consoleProps)
this._logTables(consoleProps)
},
_logValues (consoleProps: any) {
const formattedLog = this._formatted(_.omit(consoleProps, 'args', 'groups', 'table'))
_.each(formattedLog, (value, key) => {
// don't log empty strings
// trim([]) returns '' but we want to log empty arrays, so account for that
if (_.isString(value) && _.trim(value) === '') return
// Skip trim if we know value is an object
if (typeof value !== 'object' && _.trim(value) === '' && !_.isArray(value)) return
this.log(`%c${key}`, 'font-weight: bold', value)
})
},
_formatted (consoleProps: any) {
const maxKeyLength = this._getMaxKeyLength(consoleProps)
return _.reduce(consoleProps, (memo, value, key) => {
const append = ': '
key = _.capitalize(key + append).padEnd(maxKeyLength + append.length, ' ')
memo[key] = value
return memo
}, {})
},
_getMaxKeyLength (obj: object) {
const lengths = Object.keys(obj).map((x) => x.length)
return Math.max(...lengths)
},
_logArgs (consoleProps: any) {
const args = this._getArgs(consoleProps)
if (!args) return
this.log(`%cArgs:`, 'font-weight: bold')
args.forEach((arg, index) => {
this.log(`%c [${index}]:`, 'font-weight: bold', arg)
})
},
_getArgs (consoleProps: any) {
const args = _.result<unknown[]>(consoleProps, 'args')
if (!args) return
return args
},
_logGroups (consoleProps: any) {
const groups = this._getGroups(consoleProps)
_.each(groups, (group) => {
if (group.expand) {
console.group(group.name)
} else {
console.groupCollapsed(group.name)
}
_.each(group.items, (value, key) => {
if (group.label === false) {
this.log(value)
} else {
this.log(`%c${key}`, 'color: blue', value)
}
})
this._logGroups(group)
console.groupEnd()
})
},
_getGroups (consoleProps: any): Group[] | undefined {
const groups = _.result<Group[]>(consoleProps, 'groups')
if (!groups) return
return _.map(groups, (group) => {
group.items = this._formatted(group.items || {})
return group
})
},
_logTables (consoleProps: any) {
const logTable = ({ name, data, columns }) => {
let tableData = data
if (Cypress.isBrowser('webkit')) {
// WebKit will hang when we attempt to log element references
// within a table. We replace the element with a simplified display
// string in this case.
// https://bugs.webkit.org/show_bug.cgi?id=244100
const getSimplifiedElementDisplay = (element: Element) => {
let display = element.tagName.toLowerCase()
if (element.id) {
display += `#${element.id}`
}
element.classList.forEach((className) => {
display += `.${className}`
})
return display
}
tableData = data.map((rowObj) => {
return Object.entries(rowObj).reduce((acc: any, value) => {
acc[value[0]] = _.isElement(value[1]) ? getSimplifiedElementDisplay(value[1] as Element) : value[1]
return acc
}, {})
})
}
console.group(name)
console.table(tableData, columns)
console.groupEnd()
}
_.each(_.sortBy(consoleProps.table, (val, key) => key), (getTableData: () => Table) => {
return logTable(getTableData())
})
},
}