Skip to content

Commit 6e77a43

Browse files
ThYpHo0njohnnyreilly
authored andcommitted
#249 Add explicit info logger to avoid writing info logs to stderr (#313)
* Add explicit info logger to avoid writing info logs to stderr * Add a loaderOption for the logLevel and logging to different logLevels * Fix tsc compile error * Add a new loaderOption to specify the info logging output. * Change documentation text according to review advise * Doing uppercase on loaderOptions logLevel just once * Bugfix the default value of logLevel must be string
1 parent ee428c1 commit 6e77a43

File tree

2 files changed

+55
-7
lines changed

2 files changed

+55
-7
lines changed

README.md

+10
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,16 @@ different dependencies in your application will be lost. You should also
139139
set the `isolatedModules` TypeScript option if you plan to ever make use
140140
of this.
141141

142+
##### logInfoToStdOut *(boolean) (default=false)*
143+
144+
This is important if you read from stdout or stderr and for proper error handling.
145+
The default value ensures that you can read from stdout e.g. via pipes or you use webpack -j to generate json output.
146+
147+
##### logLevel *(string) (default=info)*
148+
149+
Can be `info`, `warn` or `error` which limits the log output to the specified log level.
150+
Beware of the fact that errors are written to stderr and everything else is written to stderr (or stdout if logInfoToStdOut is true).
151+
142152
##### silent *(boolean) (default=false)*
143153

144154
If true, no console.log messages will be emitted. Note that most error

index.ts

+45-7
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ var Console = require('console').Console;
1515
var semver = require('semver')
1616
require('colors');
1717

18-
const console = new Console(process.stderr);
18+
const stderrConsole = new Console(process.stderr);
19+
const stdoutConsole = new Console(process.stdout);
1920

2021
var pushArray = function(arr, toPush) {
2122
Array.prototype.splice.apply(arr, [0, 0].concat(toPush));
@@ -25,8 +26,16 @@ function hasOwnProperty(obj, property) {
2526
return Object.prototype.hasOwnProperty.call(obj, property)
2627
}
2728

29+
enum LogLevel {
30+
INFO = 1,
31+
WARN = 2,
32+
ERROR = 3
33+
}
34+
2835
interface LoaderOptions {
2936
silent: boolean;
37+
logLevel: string;
38+
logInfoToStdOut: boolean;
3039
instance: string;
3140
compiler: string;
3241
configFileName: string;
@@ -149,8 +158,34 @@ function findConfigFile(compiler: typeof typescript, searchPath: string, configF
149158
function ensureTypeScriptInstance(loaderOptions: LoaderOptions, loader: any): { instance?: TSInstance, error?: WebpackError } {
150159

151160
function log(...messages: string[]): void {
161+
logToConsole(stdoutConsole, messages);
162+
}
163+
164+
function logToConsole(logConsole:any, messages: string[]): void {
152165
if (!loaderOptions.silent) {
153-
console.log.apply(console, messages);
166+
console.log.apply(logConsole, messages);
167+
}
168+
}
169+
170+
function logInfo(...messages: string[]): void {
171+
if (LogLevel[loaderOptions.logLevel] <= LogLevel.INFO) {
172+
if(loaderOptions.logInfoToStdOut) {
173+
logToConsole(stdoutConsole, messages);
174+
} else {
175+
logToConsole(stderrConsole, messages);
176+
}
177+
}
178+
}
179+
180+
function logError(...messages: string[]): void {
181+
if (LogLevel[loaderOptions.logLevel] <= LogLevel.ERROR) {
182+
logToConsole(stderrConsole, messages);
183+
}
184+
}
185+
186+
function logWarning(...messages: string[]): void {
187+
if (LogLevel[loaderOptions.logLevel] <= LogLevel.WARN) {
188+
logToConsole(stderrConsole, messages);
154189
}
155190
}
156191

@@ -180,11 +215,11 @@ function ensureTypeScriptInstance(loaderOptions: LoaderOptions, loader: any): {
180215
compilerCompatible = true;
181216
}
182217
else {
183-
log(`${motd}. This version is incompatible with ts-loader. Please upgrade to the latest version of TypeScript.`.red);
218+
logError(`${motd}. This version is incompatible with ts-loader. Please upgrade to the latest version of TypeScript.`.red);
184219
}
185220
}
186221
else {
187-
log(`${motd}. This version may or may not be compatible with ts-loader.`.yellow);
222+
logWarning(`${motd}. This version may or may not be compatible with ts-loader.`.yellow);
188223
}
189224

190225
var files = <TSFiles>{};
@@ -211,8 +246,8 @@ function ensureTypeScriptInstance(loaderOptions: LoaderOptions, loader: any): {
211246
error?: typescript.Diagnostic;
212247
};
213248
if (configFilePath) {
214-
if (compilerCompatible) log(`${motd} and ${configFilePath}`.green)
215-
else log(`ts-loader: Using config file at ${configFilePath}`.green)
249+
if (compilerCompatible) logInfo(`${motd} and ${configFilePath}`.green)
250+
else logInfo(`ts-loader: Using config file at ${configFilePath}`.green)
216251

217252
// HACK: relies on the fact that passing an extra argument won't break
218253
// the old API that has a single parameter
@@ -227,7 +262,7 @@ function ensureTypeScriptInstance(loaderOptions: LoaderOptions, loader: any): {
227262
}
228263
}
229264
else {
230-
if (compilerCompatible) log(motd.green)
265+
if (compilerCompatible) logInfo(motd.green)
231266

232267
configFile = {
233268
config: {
@@ -556,13 +591,16 @@ function loader(contents) {
556591

557592
var options = objectAssign<LoaderOptions>({}, {
558593
silent: false,
594+
logLevel: 'INFO',
595+
logInfoToStdOut: false,
559596
instance: 'default',
560597
compiler: 'typescript',
561598
configFileName: 'tsconfig.json',
562599
transpileOnly: false,
563600
compilerOptions: {}
564601
}, configFileOptions, queryOptions);
565602
options.ignoreDiagnostics = arrify(options.ignoreDiagnostics).map(Number);
603+
options.logLevel = options.logLevel.toUpperCase();
566604

567605
// differentiate the TypeScript instance based on the webpack instance
568606
var webpackIndex = webpackInstances.indexOf(this._compiler);

0 commit comments

Comments
 (0)