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:
parent
36a2d26148
commit
a33d69232a
@ -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,6 +277,7 @@ export class Logger {
|
|||||||
* Outputs information.
|
* Outputs information.
|
||||||
*/
|
*/
|
||||||
public info(msg: string, ...fields: FieldArray): void {
|
public info(msg: string, ...fields: FieldArray): void {
|
||||||
|
if (this.level <= Level.Info) {
|
||||||
this.handle({
|
this.handle({
|
||||||
type: "info",
|
type: "info",
|
||||||
message: msg,
|
message: msg,
|
||||||
@ -272,11 +285,13 @@ export class Logger {
|
|||||||
tagColor: "#008FBF",
|
tagColor: "#008FBF",
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Outputs a warning.
|
* Outputs a warning.
|
||||||
*/
|
*/
|
||||||
public warn(msg: string, ...fields: FieldArray): void {
|
public warn(msg: string, ...fields: FieldArray): void {
|
||||||
|
if (this.level <= Level.Warn) {
|
||||||
this.handle({
|
this.handle({
|
||||||
type: "warn",
|
type: "warn",
|
||||||
message: msg,
|
message: msg,
|
||||||
@ -284,11 +299,13 @@ export class Logger {
|
|||||||
tagColor: "#FF9D00",
|
tagColor: "#FF9D00",
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Outputs a debug message.
|
* Outputs a debug message.
|
||||||
*/
|
*/
|
||||||
public debug(msg: string, ...fields: FieldArray): void {
|
public debug(msg: string, ...fields: FieldArray): void {
|
||||||
|
if (this.level <= Level.Debug) {
|
||||||
this.handle({
|
this.handle({
|
||||||
type: "debug",
|
type: "debug",
|
||||||
message: msg,
|
message: msg,
|
||||||
@ -296,11 +313,13 @@ export class Logger {
|
|||||||
tagColor: "#84009E",
|
tagColor: "#84009E",
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Outputs an error.
|
* Outputs an error.
|
||||||
*/
|
*/
|
||||||
public error(msg: string, ...fields: FieldArray): void {
|
public error(msg: string, ...fields: FieldArray): void {
|
||||||
|
if (this.level <= Level.Error) {
|
||||||
this.handle({
|
this.handle({
|
||||||
type: "error",
|
type: "error",
|
||||||
message: msg,
|
message: msg,
|
||||||
@ -308,6 +327,7 @@ export class Logger {
|
|||||||
tagColor: "#B00000",
|
tagColor: "#B00000",
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns a sub-logger with a name.
|
* Returns a sub-logger with a name.
|
||||||
|
@ -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();
|
||||||
|
Reference in New Issue
Block a user