Repository navigation
Logging improvements suggestion for tsserver #14755
Copy link
Copy link
Closed
Labels
DeclinedThe issue was declined as something which matches the TypeScript visionThe issue was declined as something which matches the TypeScript visionSuggestionAn idea for TypeScriptAn idea for TypeScript
Description
TypeScript Version: 2.3.x
Current ts.server.Logger class prints logs in plain text format which is hard to read and debug especially if its include communication traffic.
My suggestion is to convert logs to bunyan like format which will output logs as JSON stream and can be viewed by bunyan or similar tool.
I have created logger which support that in my fork, here is the link for quick presentation of work that i have done and happy to share with pull request if you guys interested.
Here is some usage of Logger class
// Severity Levels
export enum LogLevel {
FATAL = 60,
ERROR = 50,
WARN = 40,
INFO = 30,
DEBUG = 20,
TRACE = 10,
NONE = 0
}
// Logger Interface
interface Logger {
/**
* Logger severity level
*/
level:LogLevel;
/**
* Logger category name
*/
name:string;
// log level methods
fatal(message:string,extra?:any):[number,number];
info(message:string,extra?:any):[number,number];
/**
* @param message Message to log
* @param extra Additional data associated to log
*/
trace(message:string,extra?:any):[number,number];
}
// Factory class
class Logger {
/**
* Initializeing logger settings
* @param level Default log level
* @param useConsole Should use console
* @param useFile Should log to file
*/
init(level:LogLevel,useConsole:boolean,useFile:string):void{ /*...*/ }
/**
* Instantation of Logger
* @param name Logger name / category
*/
get(name:string):Logger{ return new Logger(); }
}
// Usage
// Initialization of logger
// https://github.com/casser/TypeScript/blob/bunyan-logger/src/server/server.ts#L406
Logger.init(LogLevel.TRACE,/*useConsole*/true,/*useFile*/"server.log"); // can be more options
// Instantiation and usage of logger
class MyLoggingTargetClass {
logger:Logger;
constructor(){
//https://github.com/casser/TypeScript/blob/bunyan-logger/src/server/editorServices.ts#L279
this.logger = Logger.get(this.constructor.name) // or Logger.get("Log Name")
}
someMethod(){
// Logging simple message with extra data
// example: https://github.com/casser/TypeScript/blob/bunyan-logger/src/server/session.ts#L542
this.logger.info("Message",{
extra:"Data"
})
// Loggin message with conditional data fields
// This will log message when logger level > info but will add extra
// data {only_when:"Value"} only when logger level > debug, this will allow
// add detailed information when we using high level of verbosity.
// condition format is {key$level:value}
// example: https://github.com/casser/TypeScript/blob/bunyan-logger/src/server/session.ts#L196
this.logger.info("Info message",{
only_when$debug:"Value"
})
// Tracking performance
// Log methods returning hrtime array [sec,milisec] as and id of log
// when this will be passed to next log that will count also duration
// example: https://github.com/casser/TypeScript/blob/bunyan-logger/src/server/session.ts#L1849
let startTime = this.logger.info("Action Started")
someDelayedAction(()=>{
// out
// Action Sarted
// Action End (actionDuration=22.256)
this.logger.info("Action End",{
actionDuration$time:startTime
})
})
}
}Viewing logs can be done by bunyan tool like
// view logs
bunyan ./file.log
// example of tailing logs
tail -f ./file.log | bunyan -o short
// example of filtering logs
tail -f ./file.log | bunyan -o short -c 'this.extra_data_key="value"'Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
DeclinedThe issue was declined as something which matches the TypeScript visionThe issue was declined as something which matches the TypeScript visionSuggestionAn idea for TypeScriptAn idea for TypeScript

Activity
RyanCavanaugh commentedon Sep 18, 2019
I think we agree in principle that the TS Server log format isn't super great for debugging, but the next step is something more wholesale in terms of organization of messages (e.g. making machine-readable nesting) rather than tweaking levels and such. The people who analyze these logs for their day jobs have gotten good enough at regexes in the meantime.