Logger: improving the logger, and using it in client code.

This commit is contained in:
John Livingston 2024-06-14 15:17:14 +02:00
parent d931a9c144
commit 2739bb823a
No known key found for this signature in database
GPG Key ID: B17B5640CE66CDBC
9 changed files with 29 additions and 19 deletions

View File

@ -94,7 +94,7 @@ export class ChannelConfigurationElement extends LivechatElement {
if (error instanceof ValidationError) { if (error instanceof ValidationError) {
this.validationError = error this.validationError = error
} }
console.warn(`A validation error occurred in saving configuration. ${error.name}: ${error.message}`) this.logger.warn(`A validation error occurred in saving configuration. ${error.name}: ${error.message}`)
this.ptNotifier.error( this.ptNotifier.error(
error.message error.message
? error.message ? error.message

View File

@ -237,7 +237,7 @@ export class ChannelEmojisElement extends LivechatElement {
a.click() a.click()
a.remove() a.remove()
} catch (err: any) { } catch (err: any) {
console.error(err) this.logger.error(err)
this.ptNotifier.error(err.toString()) this.ptNotifier.error(err.toString())
} finally { } finally {
this.actionDisabled = false this.actionDisabled = false

View File

@ -504,7 +504,7 @@ export class DynamicTableFormElement extends LivechatElement {
} }
if (!formElement) { if (!formElement) {
console.warn(`value type '${(propertyValue.constructor.toString())}' is incompatible` + this.logger.warn(`value type '${(propertyValue.constructor.toString())}' is incompatible` +
`with field type '${propertySchema.inputType as string}' for form entry '${propertyName.toString()}'.`) `with field type '${propertySchema.inputType as string}' for form entry '${propertyName.toString()}'.`)
} }
@ -724,13 +724,13 @@ export class DynamicTableFormElement extends LivechatElement {
: undefined : undefined
if (value === undefined) { if (value === undefined) {
console.warn('Could not update property : Target or value was undefined') this.logger.warn('Could not update property : Target or value was undefined')
return return
} }
const rowById = this._rowsById.find(rowById => rowById._id === rowId) const rowById = this._rowsById.find(rowById => rowById._id === rowId)
if (!rowById) { if (!rowById) {
console.warn(`Could not update property : Did not find a property named '${propertyName}' in row '${rowId}'`) this.logger.warn(`Could not update property : Did not find a property named '${propertyName}' in row '${rowId}'`)
return return
} }

View File

@ -94,7 +94,7 @@ export class ImageFileInputElement extends LivechatElement {
this.dispatchEvent(event) this.dispatchEvent(event)
} catch (err) { } catch (err) {
// FIXME: use peertube notifier? // FIXME: use peertube notifier?
console.error(err) this.logger.error(err)
} }
} }
} }

View File

@ -347,7 +347,7 @@ export class TagsInputElement extends LivechatElement {
private readonly _addTag = (value: string | undefined): void => { private readonly _addTag = (value: string | undefined): void => {
if (value === undefined) { if (value === undefined) {
console.warn('Could not add tag : Target or value was undefined') this.logger.warn('Could not add tag : Target or value was undefined')
return return
} }
@ -365,7 +365,7 @@ export class TagsInputElement extends LivechatElement {
private readonly _removeTag = (index: number): void => { private readonly _removeTag = (index: number): void => {
if (index < 0 || index >= this.value.length) { if (index < 0 || index >= this.value.length) {
console.warn('Could not remove tag : index out of range') this.logger.warn('Could not remove tag : index out of range')
return return
} }

View File

@ -4,6 +4,7 @@
import type { RegisterClientOptions } from '@peertube/peertube-types/client' import type { RegisterClientOptions } from '@peertube/peertube-types/client'
import { displayConverseJS } from '../../utils/conversejs' import { displayConverseJS } from '../../utils/conversejs'
import { logger } from '../../utils/logger'
/** /**
* Registers stuff related to "room" page. * Registers stuff related to "room" page.
@ -29,7 +30,7 @@ async function registerRoom (clientOptions: RegisterClientOptions): Promise<void
await displayConverseJS(clientOptions, container, roomKey, 'peertube-fullpage', forceType) await displayConverseJS(clientOptions, container, roomKey, 'peertube-fullpage', forceType)
} catch (err) { } catch (err) {
console.error('[peertube-plugin-livechat] ' + (err as string)) logger.error(err)
// Displaying an error page. // Displaying an error page.
rootEl.innerHTML = '' rootEl.innerHTML = ''
const message = document.createElement('div') const message = document.createElement('div')

View File

@ -212,7 +212,7 @@ async function _insertChatDom (
} }
) )
} catch (err) { } catch (err) {
console.error(err) logger.error(err)
} }
}, },
icon: promoteSVG, icon: promoteSVG,

View File

@ -6,6 +6,7 @@ import type { RegisterClientOptions } from '@peertube/peertube-types/client'
import type { InitConverseJSParams, ChatPeertubeIncludeMode } from 'shared/lib/types' import type { InitConverseJSParams, ChatPeertubeIncludeMode } from 'shared/lib/types'
import { computeAutoColors } from './colors' import { computeAutoColors } from './colors'
import { getBaseRoute } from './uri' import { getBaseRoute } from './uri'
import { logger } from './logger'
// FIXME: better declaration (see builtin.ts) // FIXME: better declaration (see builtin.ts)
declare global { declare global {
@ -143,7 +144,7 @@ async function displayConverseJS (
if (!a) { return } if (!a) { return }
if (a.getAttribute('href') !== '#') { return } if (a.getAttribute('href') !== '#') { return }
console.log('[peertube-plugin-livechat] intercepting a click on href=# in converse root, canceling the event.') logger.log('Intercepting a click on href=# in converse root, canceling the event.')
ev.preventDefault() ev.preventDefault()
}) })

View File

@ -3,19 +3,27 @@
// SPDX-License-Identifier: AGPL-3.0-only // SPDX-License-Identifier: AGPL-3.0-only
interface Logger { interface Logger {
log: (s: string) => void log: (s: any) => void
info: (s: string) => void info: (s: any) => void
error: (s: string) => void error: (s: any) => void
warn: (s: string) => void warn: (s: any) => void
createLogger: (p: string) => Logger createLogger: (p: string) => Logger
} }
function createLogger (prefix: string): Logger { function createLogger (prefix: string): Logger {
return { return {
log: (s: string) => console.log('[' + prefix + '] ' + s), log: (s: any) => {
info: (s: string) => console.info('[' + prefix + '] ' + s), typeof s === 'string' ? console.log('[' + prefix + '] ' + s) : console.log('[' + prefix + ']', s)
error: (s: string) => console.error('[' + prefix + '] ' + s), },
warn: (s: string) => console.warn('[' + prefix + '] ' + s), info: (s: any) => {
typeof s === 'string' ? console.info('[' + prefix + '] ' + s) : console.info('[' + prefix + ']', s)
},
error: (s: any) => {
typeof s === 'string' ? console.error('[' + prefix + '] ' + s) : console.error('[' + prefix + ']', s)
},
warn: (s: any) => {
typeof s === 'string' ? console.warn('[' + prefix + '] ' + s) : console.warn('[' + prefix + ']', s)
},
createLogger: (p: string) => createLogger('peertube-plugin-livechat>' + p) createLogger: (p: string) => createLogger('peertube-plugin-livechat>' + p)
} }
} }