Add log level to logger

This is mostly so I can avoid the request logging which pushes out other
logs pretty fast.
This commit is contained in:
Asher 2019-01-22 12:44:03 -06:00 committed by Kyle Carberry
parent 36a2d26148
commit a33d69232a
No known key found for this signature in database
GPG Key ID: A0409BDB6B0B3EDB
2 changed files with 59 additions and 30 deletions

View File

@ -1,3 +1,13 @@
/**
* Log level.
*/
export enum Level {
Debug = 0,
Info = 1,
Warn = 2,
Error = 3,
}
/** /**
* A field to log. * A field to log.
*/ */
@ -221,12 +231,12 @@ export class ServerFormatter extends Formatter {
public fields(fields: Array<Field<any>>): void { public fields(fields: Array<Field<any>>): void {
const obj = {} as any; const obj = {} as any;
this.format += "\u001B[38;2;140;140;140m" this.format += "\u001B[38;2;140;140;140m";
fields.forEach((field) => { fields.forEach((field) => {
obj[field.identifier] = field.value; obj[field.identifier] = field.value;
}); });
this.args.push(JSON.stringify(obj)); this.args.push(JSON.stringify(obj));
console.log(...this.flush()); console.log(...this.flush()); // tslint:disable-line no-console
} }
} }
@ -236,6 +246,8 @@ export class ServerFormatter extends Formatter {
*/ */
export class Logger { export class Logger {
public level = Level.Debug;
private readonly nameColor?: string; private readonly nameColor?: string;
private muted: boolean; private muted: boolean;
@ -265,48 +277,56 @@ export class Logger {
* Outputs information. * Outputs information.
*/ */
public info(msg: string, ...fields: FieldArray): void { public info(msg: string, ...fields: FieldArray): void {
this.handle({ if (this.level <= Level.Info) {
type: "info", this.handle({
message: msg, type: "info",
fields, message: msg,
tagColor: "#008FBF", fields,
}); tagColor: "#008FBF",
});
}
} }
/** /**
* Outputs a warning. * Outputs a warning.
*/ */
public warn(msg: string, ...fields: FieldArray): void { public warn(msg: string, ...fields: FieldArray): void {
this.handle({ if (this.level <= Level.Warn) {
type: "warn", this.handle({
message: msg, type: "warn",
fields, message: msg,
tagColor: "#FF9D00", fields,
}); tagColor: "#FF9D00",
});
}
} }
/** /**
* Outputs a debug message. * Outputs a debug message.
*/ */
public debug(msg: string, ...fields: FieldArray): void { public debug(msg: string, ...fields: FieldArray): void {
this.handle({ if (this.level <= Level.Debug) {
type: "debug", this.handle({
message: msg, type: "debug",
fields, message: msg,
tagColor: "#84009E", fields,
}); tagColor: "#84009E",
});
}
} }
/** /**
* Outputs an error. * Outputs an error.
*/ */
public error(msg: string, ...fields: FieldArray): void { public error(msg: string, ...fields: FieldArray): void {
this.handle({ if (this.level <= Level.Error) {
type: "error", this.handle({
message: msg, type: "error",
fields, message: msg,
tagColor: "#B00000", fields,
}); tagColor: "#B00000",
});
}
} }
/** /**

View File

@ -1,4 +1,4 @@
import { field, logger } from "@coder/logger"; import { field, logger, Level } from "@coder/logger";
import { ServerMessage, SharedProcessActiveMessage } from "@coder/protocol/src/proto"; import { ServerMessage, SharedProcessActiveMessage } from "@coder/protocol/src/proto";
import { Command, flags } from "@oclif/command"; import { Command, flags } from "@oclif/command";
import * as fs from "fs"; import * as fs from "fs";
@ -20,6 +20,7 @@ export class Entry extends Command {
host: flags.string({ char: "h", default: "0.0.0.0" }), host: flags.string({ char: "h", default: "0.0.0.0" }),
open: flags.boolean({ char: "o", description: "Open in browser on startup" }), open: flags.boolean({ char: "o", description: "Open in browser on startup" }),
port: flags.integer({ char: "p", default: 8080, description: "Port to bind on" }), port: flags.integer({ char: "p", default: 8080, description: "Port to bind on" }),
logLevel: flags.enum({ char: "l", options: [ "debug", "info", "warn", "error" ]}),
version: flags.version({ char: "v" }), version: flags.version({ char: "v" }),
// Dev flags // Dev flags
@ -50,6 +51,15 @@ export class Entry extends Command {
const { args, flags } = this.parse(Entry); const { args, flags } = this.parse(Entry);
if (flags.logLevel) {
switch (flags.logLevel) {
case "debug": logger.level = Level.Debug; break;
case "info": logger.level = Level.Info; break;
case "warn": logger.level = Level.Warn; break;
case "error": logger.level = Level.Error; break;
}
}
if (flags["bootstrap-fork"]) { if (flags["bootstrap-fork"]) {
const modulePath = flags["bootstrap-fork"]; const modulePath = flags["bootstrap-fork"];
if (!modulePath) { if (!modulePath) {
@ -57,8 +67,7 @@ export class Entry extends Command {
process.exit(1); process.exit(1);
} }
requireModule(modulePath); return requireModule(modulePath);
return;
} }
const dataDir = flags["data-dir"] || path.join(os.homedir(), ".vscode-online"); const dataDir = flags["data-dir"] || path.join(os.homedir(), ".vscode-online");
@ -70,7 +79,7 @@ export class Entry extends Command {
logger.info("Initializing", field("data-dir", dataDir), field("working-dir", workingDir)); logger.info("Initializing", field("data-dir", dataDir), field("working-dir", workingDir));
const sharedProcess = new SharedProcess(dataDir); const sharedProcess = new SharedProcess(dataDir);
logger.info("Starting shared process...", field("socket", sharedProcess.socketPath)); logger.info("Starting shared process...", field("socket", sharedProcess.socketPath));
const sendSharedProcessReady = (socket: WebSocket) => { const sendSharedProcessReady = (socket: WebSocket): void => {
const active = new SharedProcessActiveMessage(); const active = new SharedProcessActiveMessage();
active.setSocketPath(sharedProcess.socketPath); active.setSocketPath(sharedProcess.socketPath);
const serverMessage = new ServerMessage(); const serverMessage = new ServerMessage();