Skip to content

Commit 499d69c

Browse files
authored
chore: add timestamp in logging (#1084)
Part of datagouv/data.gouv.fr#2015
1 parent 9a33317 commit 499d69c

2 files changed

Lines changed: 29 additions & 0 deletions

File tree

nuxt.config.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@ export default defineNuxtConfig({
1616
'@nuxt/fonts',
1717
'nuxt-og-image',
1818
],
19+
20+
plugins: [
21+
'~/plugins/logger.ts',
22+
],
1923
devtools: { enabled: true, componentInspector: false },
2024

2125
app: {

plugins/logger.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import { defineNuxtPlugin } from '#imports'
2+
3+
// Module-level variable to ensure one-time initialization
4+
let __logger_plugin_initialized = false
5+
6+
export default defineNuxtPlugin(() => {
7+
if (__logger_plugin_initialized) return
8+
if (process.env.NODE_ENV !== 'production') return
9+
__logger_plugin_initialized = true
10+
11+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
12+
const createLogger = (originalMethod: (...args: any[]) => void) => {
13+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
14+
return function (...args: any[]) {
15+
const timestamp = new Date().toISOString()
16+
originalMethod(`[${timestamp}]`, ...args)
17+
}
18+
}
19+
20+
console.log = createLogger(console.log)
21+
console.error = createLogger(console.error)
22+
console.warn = createLogger(console.warn)
23+
console.info = createLogger(console.info)
24+
console.debug = createLogger(console.debug)
25+
})

0 commit comments

Comments
 (0)