Skip to content

Commit 8825880

Browse files
committed
refactor: use Nextcloud logger
This allows to silence console messages depending on the log level configured in Nextcloud. Signed-off-by: Ferdinand Thiessen <opensource@fthiessen.de>
1 parent d0800ad commit 8825880

6 files changed

Lines changed: 80 additions & 9 deletions

File tree

lib/ProxyBus.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,19 +10,17 @@ import type { IsUndefined } from './types.ts'
1010

1111
import major from 'semver/functions/major.js'
1212
import valid from 'semver/functions/valid.js'
13+
import { logger } from './logger.ts'
1314

1415
export class ProxyBus<E extends GenericEvents = NextcloudEvents>
1516
implements EventBus<E> {
1617
private bus: EventBus<E>
1718

1819
constructor(bus: EventBus<E>) {
1920
if (typeof bus.getVersion !== 'function' || !valid(bus.getVersion())) {
20-
console.warn('Proxying an event bus with an unknown or invalid version')
21+
logger.warn('Proxying an event bus with an unknown or invalid version')
2122
} else if (major(bus.getVersion()) !== major(this.getVersion())) {
22-
console.warn('Proxying an event bus of version '
23-
+ bus.getVersion()
24-
+ ' with '
25-
+ this.getVersion())
23+
logger.warn(`Proxying an event bus of version ${bus.getVersion()} with ${this.getVersion()}`)
2624
}
2725

2826
this.bus = bus

lib/SimpleBus.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ import type { EventBus } from './EventBus.js'
88
import type { EventHandler } from './EventHandler.js'
99
import type { IsUndefined } from './types.ts'
1010

11+
import { logger } from './logger.ts'
12+
1113
export class SimpleBus<E extends GenericEvents = NextcloudEvents>
1214
implements EventBus<E> {
1315
private handlers = new Map<keyof E, EventHandler<E[keyof E]>[]>()
@@ -44,8 +46,8 @@ implements EventBus<E> {
4446
handlers.forEach((h) => {
4547
try {
4648
(h as EventHandler<(typeof event)[0]>)(event[0])
47-
} catch (e) {
48-
console.error('could not invoke event listener', e)
49+
} catch (error) {
50+
logger.error('SimpleBus: Could not invoke event listener', { error })
4951
}
5052
})
5153
}

lib/index.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import type { EventBus } from './EventBus.ts'
88
import type { EventHandler } from './EventHandler.ts'
99
import type { IsUndefined } from './types.ts'
1010

11+
import { logger } from './logger.ts'
1112
import { ProxyBus } from './ProxyBus.ts'
1213
import { SimpleBus } from './SimpleBus.ts'
1314

@@ -34,13 +35,13 @@ function getBus(): EventBus {
3435
// testing or SSR
3536
return new Proxy({} as EventBus, {
3637
get: () => {
37-
return () => console.error('Window not available, EventBus can not be established!')
38+
return () => logger.error('Window not available, EventBus can not be established!')
3839
},
3940
})
4041
}
4142

4243
if (window.OC?._eventBus && typeof window._nc_event_bus === 'undefined') {
43-
console.warn('found old event bus instance at OC._eventBus. Update your version!')
44+
logger.warn('Found old event bus instance at OC._eventBus. Update your version!')
4445
window._nc_event_bus = window.OC._eventBus
4546
}
4647

lib/logger.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
/*!
2+
* SPDX-FileCopyrightText: 2025 Nextcloud GmbH and Nextcloud contributors
3+
* SPDX-License-Identifier: GPL-3.0-or-later
4+
*/
5+
6+
import { getLoggerBuilder } from '@nextcloud/logger'
7+
8+
export const logger = getLoggerBuilder()
9+
.setApp('@nextcloud/event-bus')
10+
.build()

package-lock.json

Lines changed: 59 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
"watch": "npm run dev -- --watch"
3838
},
3939
"dependencies": {
40+
"@nextcloud/logger": "^2.2.1",
4041
"@types/semver": "^7.7.0",
4142
"semver": "^7.7.2"
4243
},

0 commit comments

Comments
 (0)