Skip to content

Commit 3163714

Browse files
authored
Merge pull request #510 from Tresjs/fix/useLogger
fix(useLogger): remove '<empty string>', allow any args/string substitution
2 parents 98109af + 541284f commit 3163714

File tree

1 file changed

+22
-6
lines changed

1 file changed

+22
-6
lines changed

src/composables/useLogger.ts

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,35 @@ export const isProd = import.meta.env.MODE === 'production'
33

44
const logPrefix = '[TresJS ▲ ■ ●] '
55

6+
type OneOrMore<T> = { 0: T } & Array<T>
7+
68
interface LoggerComposition {
7-
logError: (message: string, error?: Error | ErrorEvent) => void
8-
logWarning: (message: string) => void
9+
logError: (...args: OneOrMore<any>) => void
10+
logWarning: (...args: OneOrMore<any>) => void
911
logMessage: (name: string, value: any) => void
1012
}
1113

1214
export function useLogger(): LoggerComposition {
13-
function logError(message: string, error?: Error | ErrorEvent) {
14-
console.error(`${logPrefix} ${message}`, error || '')
15+
function logError(...args: OneOrMore<any>) {
16+
if (typeof args[0] === 'string') {
17+
// NOTE: Don't break console string substitution
18+
args[0] = logPrefix + args[0]
19+
}
20+
else {
21+
args.unshift(logPrefix)
22+
}
23+
console.error(...args)
1524
}
1625

17-
function logWarning(message: string) {
18-
console.warn(`${logPrefix} ${message}`)
26+
function logWarning(...args: OneOrMore<any>) {
27+
if (typeof args[0] === 'string') {
28+
// NOTE: Don't break console string substitution
29+
args[0] = logPrefix + args[0]
30+
}
31+
else {
32+
args.unshift(logPrefix)
33+
}
34+
console.warn(...args)
1935
}
2036

2137
function logMessage(name: string, value: any) {

0 commit comments

Comments
 (0)