Skip to content

Commit

Permalink
option to skip local file log on log setup
Browse files Browse the repository at this point in the history
  • Loading branch information
rafawalter committed Nov 25, 2020
1 parent a5d0190 commit 8701326
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 28 deletions.
9 changes: 9 additions & 0 deletions src/lib/logger/console_and_file_logger.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,12 @@ Deno.test({
sanitizeResources: false,
sanitizeOps: false,
})

Deno.test({
name: 'should have option to skip local file log',
async fn() {
await ConsoleAndFileLogger.setup(true)
},
sanitizeResources: false,
sanitizeOps: false,
})
64 changes: 36 additions & 28 deletions src/lib/logger/console_and_file_logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,38 +5,25 @@ import Logger from "./logger.ts";
export default class ConsoleAndFileLogger implements Logger {
private static config: Config;

public static async setup(): Promise<string[]> {
public static async setup(skipLocalFileLog = false): Promise<string[]> {

const logFiles = [];
const handlers = [];
const logFiles: string[] = [];
const handlers: any = {};

const logFileWithTimestamp = Deno.makeTempFileSync({
prefix: `levain-${ConsoleAndFileLogger.logTag(new Date())}-`,
suffix: ".log",
});
logFiles.push(logFileWithTimestamp);
const logFileHandler = this.getLogFileHandler(logFileWithTimestamp);
handlers.push(logFileHandler)
handlers['console'] = this.getConsoleHandler()
handlers['logFileWithTimestamp'] = this.getTimestampFileLogHandler(logFiles)
if (!skipLocalFileLog) {
handlers['fixedFile'] = this.getLocalFileLogHandler(logFiles)
}

const fixedLogFile = `levain.log`
logFiles.push(fixedLogFile)
const fileHandler = this.getLogFileHandler(fixedLogFile, {mode: 'w'});
handlers.push(logFileHandler)
const handlerNames = Object.keys(handlers)

await log.setup({
handlers: {
fileWithTimestamp: logFileHandler,
fixedFile: fileHandler,
console: new log.handlers.ConsoleHandler("INFO", {
formatter: this.getFormatter()
}),
},

handlers,
loggers: {
// configure default logger available via short-hand methods above
default: {
level: "DEBUG",
handlers: ["console", "fixedFile", "fileWithTimestamp"],
handlers: handlerNames,
}
},
});
Expand All @@ -47,7 +34,7 @@ export default class ConsoleAndFileLogger implements Logger {
return logFiles
}

private static getLogFileHandler(logFile: string, options = {}) {
public static getLogFileHandler(logFile: string, options = {}) {
const fullOptions = {
filename: logFile,
formatter: this.getFormatter(),
Expand All @@ -56,7 +43,7 @@ export default class ConsoleAndFileLogger implements Logger {
return new AutoFlushLogFileHandler("DEBUG", fullOptions);
}

private static getFormatter(): (logRecord: any) => string {
public static getFormatter(): (logRecord: any) => string {
return logRecord => {
let msg = ConsoleAndFileLogger.hidePassword(logRecord.msg);
return `${ConsoleAndFileLogger.logTag(logRecord.datetime)} ${logRecord.levelName} ${msg}`;
Expand All @@ -67,7 +54,7 @@ export default class ConsoleAndFileLogger implements Logger {
ConsoleAndFileLogger.config = config;
}

private static logTag(dt: Date): string {
public static logTag(dt: Date): string {
let logTag: string = "";
logTag += dt.getFullYear() + "";
logTag += (dt.getMonth() < 10 ? "0" : "") + dt.getMonth();
Expand All @@ -79,7 +66,7 @@ export default class ConsoleAndFileLogger implements Logger {
return logTag;
}

private static hidePassword(msg: string): string {
public static hidePassword(msg: string): string {
if (!ConsoleAndFileLogger.config?.password) {
return msg;
}
Expand All @@ -90,6 +77,27 @@ export default class ConsoleAndFileLogger implements Logger {
info(text: string): void {
log.info(text)
}

public static getConsoleHandler() {
return new log.handlers.ConsoleHandler("INFO", {
formatter: this.getFormatter()
})
}

public static getTimestampFileLogHandler(logFiles: string[]) {
const logFileWithTimestamp = Deno.makeTempFileSync({
prefix: `levain-${ConsoleAndFileLogger.logTag(new Date())}-`,
suffix: ".log",
});
logFiles.push(logFileWithTimestamp);
return this.getLogFileHandler(logFileWithTimestamp);
}

public static getLocalFileLogHandler(logFiles: string[]) {
const localLogFile = `levain.log`
logFiles.push(localLogFile)
return this.getLogFileHandler(localLogFile, {mode: 'w'});
}
}

class AutoFlushLogFileHandler extends log.handlers.FileHandler {
Expand Down

0 comments on commit 8701326

Please sign in to comment.