Skip to content
Snippets Groups Projects
Commit e3dde1d7 authored by BITARD Michaël's avatar BITARD Michaël Committed by SAFINE LAGET Anis
Browse files

chore(api): configure le niveau de log en INFO par défaut et changeable via...

chore(api): configure le niveau de log en INFO par défaut et changeable via une variable d'environnement (!1585)
parent 043ec743
No related branches found
No related tags found
1 merge request!1585chore(api): configure le niveau de log en INFO par défaut et changeable via une variable d'environnement
Pipeline #454038 passed
...@@ -14,6 +14,8 @@ const JWT_ALGORITHMS = ['HS256', 'HS384', 'HS512', 'RS256', 'RS384', 'RS512', 'E ...@@ -14,6 +14,8 @@ const JWT_ALGORITHMS = ['HS256', 'HS384', 'HS512', 'RS256', 'RS384', 'RS512', 'E
const httpsProxyValidator = z.string().url().optional().transform(urlToSplit) const httpsProxyValidator = z.string().url().optional().transform(urlToSplit)
const logLevelValidator = z.enum(['DEBUG', 'INFO', 'WARNING', 'ERROR'])
export type LogLevel = z.infer<typeof logLevelValidator>
const configValidator = caminoConfigValidator.extend({ const configValidator = caminoConfigValidator.extend({
API_HOST: z.string(), API_HOST: z.string(),
KEYCLOAK_API_CLIENT_ID: z.string(), KEYCLOAK_API_CLIENT_ID: z.string(),
...@@ -47,6 +49,7 @@ const configValidator = caminoConfigValidator.extend({ ...@@ -47,6 +49,7 @@ const configValidator = caminoConfigValidator.extend({
API_MAILJET_SECRET: z.string(), API_MAILJET_SECRET: z.string(),
API_MAILJET_CONTACTS_LIST_ID: z.coerce.number(), API_MAILJET_CONTACTS_LIST_ID: z.coerce.number(),
API_MAILJET_EXPLOITANTS_GUYANE_LIST_ID: z.coerce.number(), API_MAILJET_EXPLOITANTS_GUYANE_LIST_ID: z.coerce.number(),
LOG_LEVEL: logLevelValidator.default('INFO'),
HTTPS_PROXY: httpsProxyValidator, HTTPS_PROXY: httpsProxyValidator,
}) })
......
import { newDateFormated } from './logger'
import { test, expect } from 'vitest'
test('newDateFormated', () => {
expect(newDateFormated(new Date('2020-06-02T13:35:11.366Z'))).toMatchInlineSnapshot(`"2020-06-02 13:35:11"`)
expect(newDateFormated(new Date('2020-06-02T01:02:03.123Z'))).toMatchInlineSnapshot(`"2020-06-02 01:02:03"`)
})
/* eslint no-console: 0 */ /* eslint no-console: 0 */
import { exhaustiveCheck } from 'camino-common/src/typescript-tools'
import { LogLevel } from '.'
const numberToDoubleCharString = (param: number): string => param.toString(10).padStart(2, '0') const numberToDoubleCharString = (param: number): string => param.toString(10).padStart(2, '0')
// TODO 2022-07-13: move to common? const newDateFormated = (date = new Date()): string => {
export const newDateFormated = (date = new Date()): string => {
const year = date.getFullYear() const year = date.getFullYear()
const month = numberToDoubleCharString(date.getMonth() + 1) const month = numberToDoubleCharString(date.getMonth() + 1)
const day = numberToDoubleCharString(date.getDate()) const day = numberToDoubleCharString(date.getDate())
...@@ -13,10 +15,27 @@ export const newDateFormated = (date = new Date()): string => { ...@@ -13,10 +15,27 @@ export const newDateFormated = (date = new Date()): string => {
return year + '-' + month + '-' + day + ' ' + hour + ':' + minute + ':' + seconds return year + '-' + month + '-' + day + ' ' + hour + ':' + minute + ':' + seconds
} }
export const consoleOverride = (color = true) => { export const consoleOverride = (logLevel: LogLevel, color = true): void => {
console.info = (...args) => console.log(newDateFormated(), color ? ' [\x1b[32minfo\x1b[0m]' : ' [info]', ...args) console.info = () => {}
console.warn = (...args) => console.log(newDateFormated(), color ? ' [\x1b[33mwarn\x1b[0m]' : ' [warn]', ...args) console.warn = () => {}
console.error = (...args) => console.log(newDateFormated(), color ? '[\x1b[31merror\x1b[0m]' : '[error]', ...args) console.error = () => {}
// TODO 2022-07-13 Not used in the application... console.debug = () => {}
console.debug = (...args) => console.log(newDateFormated(), color ? '[\x1b[36mdebug\x1b[0m]' : '[debug]', ...args)
switch (logLevel) {
// @ts-ignore fallthrough
case 'DEBUG':
console.debug = (...args) => console.log(newDateFormated(), color ? '[\x1b[36mdebug\x1b[0m]' : '[debug]', ...args)
// @ts-ignore fallthrough
case 'INFO':
console.info = (...args) => console.log(newDateFormated(), color ? ' [\x1b[32minfo\x1b[0m]' : ' [info]', ...args)
// @ts-ignore fallthrough
case 'WARNING':
console.warn = (...args) => console.log(newDateFormated(), color ? ' [\x1b[33mwarn\x1b[0m]' : ' [warn]', ...args)
// @ts-ignore fallthrough
case 'ERROR':
console.error = (...args) => console.log(newDateFormated(), color ? '[\x1b[31merror\x1b[0m]' : '[error]', ...args)
break
default:
exhaustiveCheck(logLevel)
}
} }
...@@ -45,7 +45,7 @@ const pool = new pg.Pool({ ...@@ -45,7 +45,7 @@ const pool = new pg.Pool({
idleTimeoutMillis: 60000, idleTimeoutMillis: 60000,
}) })
consoleOverride() consoleOverride(config().LOG_LEVEL)
filesInit() filesInit()
databaseInit(pool) databaseInit(pool)
.then(() => { .then(() => {
......
...@@ -19,7 +19,7 @@ if (isNotNullNorUndefined(config().CAMINO_STAGE)) { ...@@ -19,7 +19,7 @@ if (isNotNullNorUndefined(config().CAMINO_STAGE)) {
const logger = new Console.Console({ stdout: output, stderr: output }) const logger = new Console.Console({ stdout: output, stderr: output })
// eslint-disable-next-line no-console // eslint-disable-next-line no-console
console.log = logger.log console.log = logger.log
consoleOverride(false) consoleOverride(config().LOG_LEVEL, false)
} }
// Le pool ne doit être qu'aux entrypoints : le daily, le monthly, et l'application. // Le pool ne doit être qu'aux entrypoints : le daily, le monthly, et l'application.
......
...@@ -27,7 +27,7 @@ if (isNotNullNorUndefined(config().CAMINO_STAGE)) { ...@@ -27,7 +27,7 @@ if (isNotNullNorUndefined(config().CAMINO_STAGE)) {
const logger = new Console.Console({ stdout: output, stderr: output }) const logger = new Console.Console({ stdout: output, stderr: output })
// eslint-disable-next-line no-console // eslint-disable-next-line no-console
console.log = logger.log console.log = logger.log
consoleOverride(false) consoleOverride(config().LOG_LEVEL, false)
} }
const tasks = async () => { const tasks = async () => {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment