-
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.
* Move logging to its own file * Catch validation errors when creating * Add post hooks for APIs * Add pre and post hooks for API * Add messaging plugins * Allow permissions to be promises * Update mongoose
- Loading branch information
1 parent
ee81c30
commit 76bc8c3
Showing
11 changed files
with
1,329 additions
and
387 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,6 @@ | ||
export * from "./mongooseRestFramework"; | ||
export * from "./expressServer"; | ||
export * from "./passport"; | ||
export * from "./models/messaging"; | ||
export * from "./logger"; | ||
export * from "./models/messaging"; |
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,91 @@ | ||
import fs from "fs"; | ||
import winston from "winston"; | ||
|
||
// Setup a default console logger. | ||
export const logger = winston.createLogger({ | ||
level: "debug", | ||
transports: [ | ||
new winston.transports.Console({ | ||
debugStdout: true, | ||
level: "debug", | ||
format: winston.format.combine(winston.format.colorize(), winston.format.simple()), | ||
}), | ||
], | ||
}); | ||
|
||
export interface LoggingOptions { | ||
level?: "debug" | "info" | "warn" | "error"; | ||
transports?: winston.transport[]; | ||
disableFileLogging?: boolean; | ||
disableConsoleLogging?: boolean; | ||
logDirectory?: string; | ||
} | ||
|
||
export function setupLogging(options?: LoggingOptions) { | ||
logger.clear(); | ||
if (!options?.disableConsoleLogging) { | ||
logger.add( | ||
new winston.transports.Console({ | ||
debugStdout: !options?.level || options?.level === "debug", | ||
level: options?.level ?? "debug", | ||
format: winston.format.combine(winston.format.colorize(), winston.format.simple()), | ||
}) | ||
); | ||
} | ||
if (!options?.disableFileLogging) { | ||
const logDirectory = options?.logDirectory ?? "./log"; | ||
if (!fs.existsSync(logDirectory)) { | ||
fs.mkdirSync(logDirectory); | ||
} | ||
|
||
const FILE_LOG_DEFAULTS = { | ||
colorize: false, | ||
compress: true, | ||
dirname: logDirectory, | ||
format: winston.format.simple(), | ||
// 30 days of retention | ||
maxFiles: 30, | ||
// 50MB max file size | ||
maxSize: 1024 * 1024 * 50, | ||
// Only readable by server user | ||
options: {mode: 0o600}, | ||
}; | ||
|
||
logger.add( | ||
new winston.transports.Stream({ | ||
...FILE_LOG_DEFAULTS, | ||
level: "error", | ||
handleExceptions: true, | ||
// Use stream so we can open log in append mode rather than overwriting. | ||
stream: fs.createWriteStream("error.log", {flags: "a"}), | ||
}) | ||
); | ||
|
||
logger.add( | ||
new winston.transports.Stream({ | ||
...FILE_LOG_DEFAULTS, | ||
level: "info", | ||
// Use stream so we can open log in append mode rather than overwriting. | ||
stream: fs.createWriteStream("out.log", {flags: "a"}), | ||
}) | ||
); | ||
if (!options?.level || options?.level === "debug") { | ||
logger.add( | ||
new winston.transports.Stream({ | ||
...FILE_LOG_DEFAULTS, | ||
level: "debug", | ||
// Use stream so we can open log in append mode rather than overwriting. | ||
stream: fs.createWriteStream("debug.log", {flags: "a"}), | ||
}) | ||
); | ||
} | ||
} | ||
|
||
if (options?.transports) { | ||
for (const transport of options.transports) { | ||
logger.add(transport); | ||
} | ||
} | ||
|
||
logger.debug("Logger set up complete"); | ||
} |
Oops, something went wrong.