Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Include logged categories in log file header #2876

Merged
merged 1 commit into from Oct 14, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
18 changes: 10 additions & 8 deletions src/shared/logging.ts
Expand Up @@ -2,6 +2,7 @@ import { EventEmitter } from "events";
import * as fs from "fs";
import * as os from "os";
import * as path from "path";
import { platformEol } from "./constants";
import { LogCategory, LogSeverity } from "./enums";
import { IAmDisposable, Logger, LogMessage, SomeError, SpawnedProcess } from "./interfaces";
import { errorString } from "./utils";
Expand Down Expand Up @@ -104,14 +105,18 @@ export function logToConsole(logger: EmittingLogger): void {
});
}

export function captureLogs(logger: EmittingLogger, file: string, header: string, maxLogLineLength: number, logCategories?: LogCategory[], excludeLogCategories = false): ({ dispose: () => Promise<void> | void }) {
export function captureLogs(logger: EmittingLogger, file: string, header: string, maxLogLineLength: number, logCategories: LogCategory[], excludeLogCategories = false): ({ dispose: () => Promise<void> | void }) {
if (!file || !path.isAbsolute(file))
throw new Error("Path passed to logTo must be an absolute path");
const time = (detailed = false) => detailed ? `[${(new Date()).toTimeString()}] ` : `[${(new Date()).toLocaleTimeString()}] `;
let logStream: fs.WriteStream | undefined = fs.createWriteStream(file);
if (header)
logStream.write(header);
logStream.write(`${(new Date()).toDateString()} ${time(true)}Log file started${os.EOL}`);

const categoryNames = logCategories.map((c) => LogCategory[c]);
logStream.write(`Logging Categories:${platformEol} ${categoryNames.join(", ")}${platformEol}${platformEol}`);

logStream.write(`${(new Date()).toDateString()} ${time(true)}Log file started${platformEol}`);
let fileLogger: IAmDisposable | undefined = logger.onLog((e) => {
if (!logStream)
return;
Expand All @@ -120,12 +125,9 @@ export function captureLogs(logger: EmittingLogger, file: string, header: string
// - We don't have a category filter; or
// - The category filter includes this category; or
// - The log is WARN/ERROR (they get logged everywhere).
const shouldLog = !logCategories
|| (
excludeLogCategories
? logCategories.indexOf(e.category) === -1
: logCategories.indexOf(e.category) !== -1
)
const shouldLog = (excludeLogCategories
? logCategories.indexOf(e.category) === -1
: logCategories.indexOf(e.category) !== -1)
|| e.severity === LogSeverity.Warn
|| e.severity === LogSeverity.Error;
if (!shouldLog)
Expand Down
2 changes: 1 addition & 1 deletion src/test/helpers.ts
Expand Up @@ -194,7 +194,7 @@ function setupTestLogging(): boolean {
// For debugger tests, the analyzer log is just noise, so we filter it out.
const excludeLogCategories = process.env.BOT && process.env.BOT.indexOf("debug") !== -1
? [LogCategory.Analyzer]
: undefined;
: [];
const testLogger = captureLogs(emittingLogger, logPath, extApi.getLogHeader(), 20000, excludeLogCategories, true);

deferUntilLast(async (testResult?: "passed" | "failed") => {
Expand Down