Decouple DB from console.log

This commit is contained in:
Douglas Barone 2020-12-22 11:50:21 -04:00
parent 58bb91220b
commit fc4fa88295

View File

@ -4,14 +4,7 @@ import { format, subDays } from 'date-fns'
const DAYS_TO_KEEP = process.env.NODE_ENV === 'production' ? 30 : 1
async function log({ level, tags = [], message = '', data }) {
const logEntry = await prisma.log.create({
data: {
level,
tags,
message,
data: { ...data, msg: message }
}
})
const now = new Date()
const color = {
LOW: '\x1b[37m',
@ -19,15 +12,26 @@ async function log({ level, tags = [], message = '', data }) {
SUCCESS: '\x1b[32m',
WARNING: '\x1b[33m',
ERROR: '\x1b[31m'
}[logEntry.level]
}[level]
const entryTags = logEntry.tags.length ? ` [${logEntry.tags}] ` : ''
const entryTags = tags.length ? ` [${tags}] ` : ''
console.log(
`${color}[${format(logEntry.timestamp, 'HH:mm:ss')}]${entryTags}\x1b[0m${
logEntry.message
}`
`${color}[${format(now, 'HH:mm:ss')}]${entryTags}\x1b[0m${message}`
)
try {
await prisma.log.create({
data: {
timestamp: now,
level,
tags,
message,
data: { ...data, msg: message }
}
})
} catch (e) {
console.log(e)
}
}
function logLow({ tags, message, data }) {