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.
*/
@ -221,12 +231,12 @@ export class ServerFormatter extends Formatter {
public fields(fields: Array<Field<any>>): 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,6 +277,7 @@ export class Logger {
* Outputs information.
*/
public info(msg: string, ...fields: FieldArray): void {
if (this.level <= Level.Info) {
this.handle({
type: "info",
message: msg,
@ -272,11 +285,13 @@ export class Logger {
tagColor: "#008FBF",
});
}
}
/**
* Outputs a warning.
*/
public warn(msg: string, ...fields: FieldArray): void {
if (this.level <= Level.Warn) {
this.handle({
type: "warn",
message: msg,
@ -284,11 +299,13 @@ export class Logger {
tagColor: "#FF9D00",
});
}
}
/**
* Outputs a debug message.
*/
public debug(msg: string, ...fields: FieldArray): void {
if (this.level <= Level.Debug) {
this.handle({
type: "debug",
message: msg,
@ -296,11 +313,13 @@ export class Logger {
tagColor: "#84009E",
});
}
}
/**
* Outputs an error.
*/
public error(msg: string, ...fields: FieldArray): void {
if (this.level <= Level.Error) {
this.handle({
type: "error",
message: msg,
@ -308,6 +327,7 @@ export class Logger {
tagColor: "#B00000",
});
}
}
/**
* Returns a sub-logger with a name.

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 { 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();