Log function created

This commit is contained in:
Douglas Barone 2020-12-18 10:53:04 -04:00
parent cb22daa2d1
commit 4d836c2260
5 changed files with 80 additions and 11 deletions

View File

@ -4,41 +4,48 @@ import { User } from './classes/User'
import { format } from 'date-fns' import { format } from 'date-fns'
import oui from 'oui' import oui from 'oui'
import { log } from './lib/logger'
import { updateUserIdMappings } from './lib/paloalto' import { updateUserIdMappings } from './lib/paloalto'
function logMsg(msg) { function cronLog(message) {
console.log(`[${format(new Date(), 'HH:mm:ss')}] cron: ${msg}`) log({
level: 'INFO',
tags: ['cron'],
message,
data: { message }
})
} }
logMsg('Scheduling tasks...') cronLog('Scheduling tasks...')
cron.schedule('*/1 * * * *', async () => { cron.schedule('*/1 * * * *', async () => {
logMsg('updateDevicesInfo started.') cronLog('updateDevicesInfo started.')
const devices = await updateDevicesInfo() const devices = await updateDevicesInfo()
logMsg(`updateDevicesInfo updated ${devices} devices.`) cronLog(`updateDevicesInfo updated ${devices} devices.`)
let mappings = 0 let mappings = 0
if (devices > 0) { if (devices > 0) {
logMsg('updateUserIdMappings started.') cronLog('updateUserIdMappings started.')
mappings = await updateUserIdMappings() mappings = await updateUserIdMappings()
logMsg(`updateUserIdMappings updated ${mappings} user-id mappings.`) cronLog(`updateUserIdMappings updated ${mappings} user-id mappings.`)
} }
}) })
cron.schedule( cron.schedule(
'0 0 0 * * *', '0 0 0 * * *',
async () => { async () => {
logMsg('User.importAllUsers started') cronLog('User.importAllUsers started')
await User.importAllUsers() await User.importAllUsers()
logMsg('User.importAllUsers finished') cronLog('User.importAllUsers finished')
}, },
{} {}
) )
cron.schedule('0 0 0 * * *', async () => { cron.schedule('0 0 0 * * *', async () => {
await oui.update() await oui.update()
logMsg('Oui updated') cronLog('Oui updated')
}) })

29
server/src/lib/logger.js Normal file
View File

@ -0,0 +1,29 @@
import prisma from '../prisma'
import { format } from 'date-fns'
async function log({ level, tags = [], message = '', data }) {
const logEntry = await prisma.log.create({
data: {
level,
tags,
message,
data
}
})
const color = {
LOW: '\x1b[37m',
INFO: '\x1b[36m',
SUCCESS: '\x1b[32m',
WARNING: '\x1b[33m',
ERROR: '\x1b[31m'
}[logEntry.level]
console.log(
`${color}[${format(logEntry.timestamp, 'HH:mm:ss')}] [${
logEntry.tags
}] \x1b[0m ${logEntry.message}`
)
}
export { log }

View File

@ -2,7 +2,8 @@ import { PubSub } from 'apollo-server'
const USER_PRESENCE_UPDATED = 'USER_PRESENCE_UPDATED' const USER_PRESENCE_UPDATED = 'USER_PRESENCE_UPDATED'
const AUTH_UPDATED = 'AUTH_UPDATED' const AUTH_UPDATED = 'AUTH_UPDATED'
const LOG_UPDATED = 'LOG_UPDATED'
const pubsub = new PubSub() const pubsub = new PubSub()
export { pubsub, USER_PRESENCE_UPDATED, AUTH_UPDATED } export { pubsub, USER_PRESENCE_UPDATED, AUTH_UPDATED, LOG_UPDATED }

View File

@ -143,6 +143,19 @@ const Query = {
apName: userPresence.wifiDevices[0].apName apName: userPresence.wifiDevices[0].apName
})) }))
.slice(0, 200) .slice(0, 200)
},
async logs() {
const logs = await prisma.log.findMany({
orderBy: { timestamp: 'desc' },
take: 500
})
return logs.map(log => ({
...log,
tags: `[${log.tags.join(', ')}]`,
data: JSON.stringify(log.data)
}))
} }
} }

View File

@ -32,6 +32,8 @@ const typeDefs = gql`
): [WifiDevice]! @auth(roles: ["superAdmin"]) ): [WifiDevice]! @auth(roles: ["superAdmin"])
wifiUsers: [User]! @auth(roles: ["superAdmin"]) wifiUsers: [User]! @auth(roles: ["superAdmin"])
logs: [Log]! @auth(roles: ["superAdmin"])
} }
type Mutation { type Mutation {
@ -184,6 +186,23 @@ const typeDefs = gql`
OFFLINE OFFLINE
} }
type Log {
id: ID!
timestamp: String!
level: LogLevel!
tags: String
message: String
data: String
}
enum LogLevel {
LOW
INFO
SUCCESS
WARNING
ERROR
}
input LoginInput { input LoginInput {
username: String! username: String!
password: String! password: String!