Log function created
This commit is contained in:
parent
cb22daa2d1
commit
4d836c2260
|
@ -4,41 +4,48 @@ import { User } from './classes/User'
|
|||
import { format } from 'date-fns'
|
||||
import oui from 'oui'
|
||||
|
||||
import { log } from './lib/logger'
|
||||
|
||||
import { updateUserIdMappings } from './lib/paloalto'
|
||||
|
||||
function logMsg(msg) {
|
||||
console.log(`[${format(new Date(), 'HH:mm:ss')}] cron: ${msg}`)
|
||||
function cronLog(message) {
|
||||
log({
|
||||
level: 'INFO',
|
||||
tags: ['cron'],
|
||||
message,
|
||||
data: { message }
|
||||
})
|
||||
}
|
||||
|
||||
logMsg('Scheduling tasks...')
|
||||
cronLog('Scheduling tasks...')
|
||||
|
||||
cron.schedule('*/1 * * * *', async () => {
|
||||
logMsg('updateDevicesInfo started.')
|
||||
cronLog('updateDevicesInfo started.')
|
||||
|
||||
const devices = await updateDevicesInfo()
|
||||
|
||||
logMsg(`updateDevicesInfo updated ${devices} devices.`)
|
||||
cronLog(`updateDevicesInfo updated ${devices} devices.`)
|
||||
|
||||
let mappings = 0
|
||||
|
||||
if (devices > 0) {
|
||||
logMsg('updateUserIdMappings started.')
|
||||
cronLog('updateUserIdMappings started.')
|
||||
mappings = await updateUserIdMappings()
|
||||
logMsg(`updateUserIdMappings updated ${mappings} user-id mappings.`)
|
||||
cronLog(`updateUserIdMappings updated ${mappings} user-id mappings.`)
|
||||
}
|
||||
})
|
||||
|
||||
cron.schedule(
|
||||
'0 0 0 * * *',
|
||||
async () => {
|
||||
logMsg('User.importAllUsers started')
|
||||
cronLog('User.importAllUsers started')
|
||||
await User.importAllUsers()
|
||||
logMsg('User.importAllUsers finished')
|
||||
cronLog('User.importAllUsers finished')
|
||||
},
|
||||
{}
|
||||
)
|
||||
|
||||
cron.schedule('0 0 0 * * *', async () => {
|
||||
await oui.update()
|
||||
logMsg('Oui updated')
|
||||
cronLog('Oui updated')
|
||||
})
|
||||
|
|
29
server/src/lib/logger.js
Normal file
29
server/src/lib/logger.js
Normal 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 }
|
|
@ -2,7 +2,8 @@ import { PubSub } from 'apollo-server'
|
|||
|
||||
const USER_PRESENCE_UPDATED = 'USER_PRESENCE_UPDATED'
|
||||
const AUTH_UPDATED = 'AUTH_UPDATED'
|
||||
const LOG_UPDATED = 'LOG_UPDATED'
|
||||
|
||||
const pubsub = new PubSub()
|
||||
|
||||
export { pubsub, USER_PRESENCE_UPDATED, AUTH_UPDATED }
|
||||
export { pubsub, USER_PRESENCE_UPDATED, AUTH_UPDATED, LOG_UPDATED }
|
||||
|
|
|
@ -143,6 +143,19 @@ const Query = {
|
|||
apName: userPresence.wifiDevices[0].apName
|
||||
}))
|
||||
.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)
|
||||
}))
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -32,6 +32,8 @@ const typeDefs = gql`
|
|||
): [WifiDevice]! @auth(roles: ["superAdmin"])
|
||||
|
||||
wifiUsers: [User]! @auth(roles: ["superAdmin"])
|
||||
|
||||
logs: [Log]! @auth(roles: ["superAdmin"])
|
||||
}
|
||||
|
||||
type Mutation {
|
||||
|
@ -184,6 +186,23 @@ const typeDefs = gql`
|
|||
OFFLINE
|
||||
}
|
||||
|
||||
type Log {
|
||||
id: ID!
|
||||
timestamp: String!
|
||||
level: LogLevel!
|
||||
tags: String
|
||||
message: String
|
||||
data: String
|
||||
}
|
||||
|
||||
enum LogLevel {
|
||||
LOW
|
||||
INFO
|
||||
SUCCESS
|
||||
WARNING
|
||||
ERROR
|
||||
}
|
||||
|
||||
input LoginInput {
|
||||
username: String!
|
||||
password: String!
|
||||
|
|
Loading…
Reference in New Issue
Block a user