feat(#27): Set up consola logger, make use of log levels in backend and frontend
This commit is contained in:
48
legalconsenthub/shared/utils/logger.ts
Normal file
48
legalconsenthub/shared/utils/logger.ts
Normal file
@@ -0,0 +1,48 @@
|
||||
import { createConsola } from 'consola'
|
||||
import type { ConsolaInstance } from 'consola'
|
||||
|
||||
export type AppLogLevel = 'silent' | 'error' | 'warn' | 'info' | 'debug' | 'trace' | 'verbose' | 'log'
|
||||
|
||||
export function createLogger(options: { level?: string; tag?: string; fancy?: boolean } = {}): ConsolaInstance {
|
||||
const level = resolveConsolaLevel(options.level)
|
||||
|
||||
const logger = createConsola({
|
||||
level
|
||||
})
|
||||
|
||||
return options.tag ? logger.withTag(options.tag) : logger
|
||||
}
|
||||
|
||||
export function resolveConsolaLevel(level?: string): number {
|
||||
const normalized = (level ?? '').toString().trim().toLowerCase()
|
||||
|
||||
// Consola levels (from docs):
|
||||
// 0: Fatal and Error
|
||||
// 1: Warnings
|
||||
// 2: Normal logs
|
||||
// 3: Informational logs
|
||||
// 4: Debug logs
|
||||
// 5: Trace logs
|
||||
// -999: Silent
|
||||
// +999: Verbose logs
|
||||
switch (normalized as AppLogLevel) {
|
||||
case 'silent':
|
||||
return -999
|
||||
case 'error':
|
||||
return 0
|
||||
case 'warn':
|
||||
return 1
|
||||
case 'log':
|
||||
return 2
|
||||
case 'info':
|
||||
return 3
|
||||
case 'debug':
|
||||
return 4
|
||||
case 'trace':
|
||||
return 5
|
||||
case 'verbose':
|
||||
return 999
|
||||
default:
|
||||
return 3
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user