From a33d69232a15fc67b632fce722a8334138af4795 Mon Sep 17 00:00:00 2001 From: Asher Date: Tue, 22 Jan 2019 12:44:03 -0600 Subject: [PATCH] Add log level to logger This is mostly so I can avoid the request logging which pushes out other logs pretty fast. --- packages/logger/src/logger.ts | 72 ++++++++++++++++++++++------------- packages/server/src/cli.ts | 17 +++++++-- 2 files changed, 59 insertions(+), 30 deletions(-) diff --git a/packages/logger/src/logger.ts b/packages/logger/src/logger.ts index 4bc48500..64170307 100644 --- a/packages/logger/src/logger.ts +++ b/packages/logger/src/logger.ts @@ -1,3 +1,13 @@ +/** + * Log level. + */ +export enum Level { + Debug = 0, + Info = 1, + Warn = 2, + Error = 3, +} + /** * A field to log. */ @@ -221,12 +231,12 @@ export class ServerFormatter extends Formatter { public fields(fields: Array>): void { const obj = {} as any; - this.format += "\u001B[38;2;140;140;140m" + this.format += "\u001B[38;2;140;140;140m"; fields.forEach((field) => { obj[field.identifier] = field.value; }); 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 { + public level = Level.Debug; + private readonly nameColor?: string; private muted: boolean; @@ -265,48 +277,56 @@ export class Logger { * Outputs information. */ public info(msg: string, ...fields: FieldArray): void { - this.handle({ - type: "info", - message: msg, - fields, - tagColor: "#008FBF", - }); + if (this.level <= Level.Info) { + this.handle({ + type: "info", + message: msg, + fields, + tagColor: "#008FBF", + }); + } } /** * Outputs a warning. */ public warn(msg: string, ...fields: FieldArray): void { - this.handle({ - type: "warn", - message: msg, - fields, - tagColor: "#FF9D00", - }); + if (this.level <= Level.Warn) { + this.handle({ + type: "warn", + message: msg, + fields, + tagColor: "#FF9D00", + }); + } } /** * Outputs a debug message. */ public debug(msg: string, ...fields: FieldArray): void { - this.handle({ - type: "debug", - message: msg, - fields, - tagColor: "#84009E", - }); + if (this.level <= Level.Debug) { + this.handle({ + type: "debug", + message: msg, + fields, + tagColor: "#84009E", + }); + } } /** * Outputs an error. */ public error(msg: string, ...fields: FieldArray): void { - this.handle({ - type: "error", - message: msg, - fields, - tagColor: "#B00000", - }); + if (this.level <= Level.Error) { + this.handle({ + type: "error", + message: msg, + fields, + tagColor: "#B00000", + }); + } } /** diff --git a/packages/server/src/cli.ts b/packages/server/src/cli.ts index d6050092..9df18f8e 100644 --- a/packages/server/src/cli.ts +++ b/packages/server/src/cli.ts @@ -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 { Command, flags } from "@oclif/command"; import * as fs from "fs"; @@ -20,6 +20,7 @@ export class Entry extends Command { host: flags.string({ char: "h", default: "0.0.0.0" }), open: flags.boolean({ char: "o", description: "Open in browser on startup" }), 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" }), // Dev flags @@ -50,6 +51,15 @@ export class Entry extends Command { 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"]) { const modulePath = flags["bootstrap-fork"]; if (!modulePath) { @@ -57,8 +67,7 @@ export class Entry extends Command { process.exit(1); } - requireModule(modulePath); - return; + return requireModule(modulePath); } 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)); const sharedProcess = new SharedProcess(dataDir); logger.info("Starting shared process...", field("socket", sharedProcess.socketPath)); - const sendSharedProcessReady = (socket: WebSocket) => { + const sendSharedProcessReady = (socket: WebSocket): void => { const active = new SharedProcessActiveMessage(); active.setSocketPath(sharedProcess.socketPath); const serverMessage = new ServerMessage();