Added logger

This commit is contained in:
Douglas Barone 2020-12-18 14:33:34 -04:00
parent 7f19378145
commit 706660f73e
8 changed files with 67 additions and 15 deletions

View File

@ -6,6 +6,7 @@ import bcrypt from 'bcrypt'
import { replacePassword } from '../lib/activeDirectory/passwordUtils'
import prisma from '../prisma'
import { logInfo, logSuccess } from '../lib/logger'
class ResetToken {
/**
@ -37,6 +38,11 @@ class ResetToken {
}
})
logInfo({
tags: ['token', 'user'],
message: `User ${creatorUsername} created a reset-token for user ${username}`
})
return {
...hashedToken,
token
@ -98,6 +104,11 @@ class ResetToken {
}
})
logSuccess({
tags: ['token', 'user'],
message: `User ${user.sAMAccountName} used a reset token`
})
return true
}
}

View File

@ -202,7 +202,7 @@ class User {
User.upsertUser(this.username)
client.unbind(() => {
logSuccess({
message: `Client unbinded. Password updated for user ${this.username}`,
message: `User ${this.username} updated his password`,
data: result,
tags: ['ldap']
})
@ -284,11 +284,32 @@ class User {
*/
static async login(username, password) {
try {
await ad.authenticate('ifms\\' + username, password)
logInfo({
tags: ['user', 'login'],
message: `User ${username} is trying to login`
})
await ad.authenticate(`ifms\\${username}`, password)
logSuccess({
tags: ['user', 'login'],
message: `User ${username} logged successfully`
})
} catch (err) {
throw (await ad.checkBinding())
? new Error('Usuário ou senha inválidos.')
: new Error('Problemas técnicos ao autenticar. Tente mais tarde.')
if (await ad.checkBinding()) {
logWarning({
tags: ['user', 'login', 'password'],
message: `User ${username} tried to login but failed`,
data: err
})
throw new Error('Usuário ou senha inválidos.')
} else {
logError({
tags: ['user', 'login', 'password'],
message: `Can't bind with AD.`
})
throw new Error('Problemas técnicos ao autenticar. Tente mais tarde.')
}
}
try {

View File

@ -21,7 +21,7 @@ cron.schedule('*/1 * * * *', async () => {
const devices = await updateDevicesInfo()
logSuccess({
logInfo({
tags: ['cron', 'wifiDevices'],
message: `updateDevicesInfo updated ${devices} devices`
})
@ -36,7 +36,7 @@ cron.schedule('*/1 * * * *', async () => {
mappings = await updateUserIdMappings()
logSuccess({
logInfo({
tags: ['cron', 'user-id'],
message: `updateUserIdMappings updated ${mappings} user-id mappings`
})
@ -51,11 +51,11 @@ cron.schedule(
message: `User.importAllUsers started`
})
await User.importAllUsers()
const users = await User.importAllUsers()
logSuccess({
logInfo({
tags: ['cron', 'user'],
message: `User.importAllUsers finished`
message: `User.importAllUsers imported ${users} users`
})
},
{}

View File

@ -22,6 +22,6 @@ server.listen().then(options => {
})
logInfo({
tags: ['server'],
message: `Endpoint: ${options.url}graphql - WebSocket: ${options.subscriptionsUrl}`
message: `Endpoint: ${options.url}graphql | WebSocket: ${options.subscriptionsUrl}`
})
})

View File

@ -1,6 +1,8 @@
import prisma from '../prisma'
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: {
@ -48,7 +50,7 @@ function logError({ tags, message, data }) {
async function deleteOldLogs() {
return prisma.log.deleteMany({
where: { timestamp: { lt: subDays(new Date(), 30) } }
where: { timestamp: { lt: subDays(new Date(), DAYS_TO_KEEP) } }
})
}

View File

@ -5,7 +5,7 @@ import prisma from '../prisma'
import https from 'https'
import { subMinutes } from 'date-fns'
import qs from 'qs'
import { logError } from './logger'
import { logError, logSuccess } from './logger'
const DEBOUNCE_TIME_IN_MS = 5000
const TIMEOUT_IN_MINUTES = '3'
@ -71,6 +71,12 @@ async function updateUserIdMappings() {
httpsAgent
})
logSuccess({
tags: ['user-id', 'paloalto'],
message: `Updated ${wifiDevices.length} user-id mappings`,
data: wifiDevices
})
return wifiDevices.length
} catch (e) {
logError({

View File

@ -5,7 +5,7 @@ import { getOnlineWifiDevices as getOnlineCiscoDevices } from './ciscoController
import prisma from '../prisma'
import { pubsub, USER_PRESENCE_UPDATED } from '../pubsub'
import { logError } from './logger'
import { logError, logSuccess } from './logger'
const DEBOUNCE_TIME_MS = 10000
const RECENT_THRESHOLD_IN_MINUTES = 3
@ -109,6 +109,12 @@ async function updateDevicesInfo() {
userPresenceUpdated: onlineDevices.length
})
logSuccess({
tags: ['wifiDevices'],
message: `Updated ${onlineDevices.length} devices`,
data: onlineDevices
})
return onlineDevices.length
} catch (e) {
logError({

View File

@ -4,6 +4,7 @@ import { ResetToken } from '../classes/ResetToken'
import { updateDevicesInfo } from '../lib/wifiDevices'
import { updateUserIdMappings } from '../lib/paloalto'
import { logInfo, logSuccess } from '../lib/logger'
const Mutation = {
async login(_, { data }) {
@ -14,7 +15,12 @@ const Mutation = {
return auth.updatePassword(data.oldPassword, data.newPassword)
},
async replacePassword(_, { data }) {
async replacePassword(_, { data }, { auth }) {
logInfo({
tags: ['replacePassword', 'user'],
message: `User ${auth.sAMAccountName} is replacing the password for user ${data.username}`
})
return replacePassword(data.username, data.newPassword)
},