forked from monkeytypegame/monkeytype
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
impr(server): store some logs forever (@Miodec) (monkeytypegame#5697)
!nuf
- Loading branch information
Showing
11 changed files
with
107 additions
and
60 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import { Collection, ObjectId } from "mongodb"; | ||
import * as db from "../init/db"; | ||
import Logger from "../utils/logger"; | ||
|
||
type DbLog = { | ||
_id: ObjectId; | ||
type?: string; | ||
timestamp: number; | ||
uid: string; | ||
important?: boolean; | ||
event: string; | ||
message: string | Record<string, unknown>; | ||
}; | ||
|
||
export const getLogsCollection = (): Collection<DbLog> => | ||
db.collection<DbLog>("logs"); | ||
|
||
async function insertIntoDb( | ||
event: string, | ||
message: string | Record<string, unknown>, | ||
uid = "", | ||
important = false | ||
): Promise<void> { | ||
const dbLog: DbLog = { | ||
_id: new ObjectId(), | ||
timestamp: Date.now(), | ||
uid: uid ?? "", | ||
event: event, | ||
message: message, | ||
important: important, | ||
}; | ||
|
||
if (!important) delete dbLog.important; | ||
|
||
Logger.info(`${event}\t${uid}\t${JSON.stringify(message)}`); | ||
|
||
await getLogsCollection().insertOne(dbLog); | ||
} | ||
|
||
export async function addLog( | ||
event: string, | ||
message: string | Record<string, unknown>, | ||
uid = "" | ||
): Promise<void> { | ||
await insertIntoDb(event, message, uid); | ||
} | ||
|
||
export async function addImportantLog( | ||
event: string, | ||
message: string | Record<string, unknown>, | ||
uid = "" | ||
): Promise<void> { | ||
await insertIntoDb(event, message, uid, true); | ||
} | ||
|
||
export async function deleteUserLogs(uid: string): Promise<void> { | ||
await getLogsCollection().deleteMany({ uid }); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.