-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: more clear logging using Apex-inspired logger
- Loading branch information
Showing
63 changed files
with
565 additions
and
433 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,58 +1,110 @@ | ||
import BluebirdPromise from "bluebird-lst" | ||
import chalk from "chalk" | ||
import { get as getEmoji } from "node-emoji" | ||
import chalk, { Chalk } from "chalk" | ||
import _debug from "debug" | ||
import WritableStream = NodeJS.WritableStream | ||
|
||
let printer: ((message: string) => void) | null = null | ||
|
||
export const debug = _debug("electron-builder") | ||
|
||
export interface Fields { | ||
[index: string]: any | ||
} | ||
|
||
export function setPrinter(value: ((message: string) => void) | null) { | ||
printer = value | ||
} | ||
|
||
class Logger { | ||
export type LogLevel = "info" | "warn" | "debug" | "notice" | "error" | ||
|
||
export const PADDING = 3 | ||
|
||
export class Logger { | ||
constructor(protected readonly stream: WritableStream) { | ||
} | ||
|
||
warn(message: string): void { | ||
this.log(chalk.yellow(`Warning: ${message}`)) | ||
messageTransformer: ((message: string, level: LogLevel) => string) = it => it | ||
|
||
filePath(file: string) { | ||
const cwd = process.cwd() | ||
return file.startsWith(cwd) ? file.substring(cwd.length + 1) : file | ||
} | ||
|
||
log(message: string): void { | ||
if (printer == null) { | ||
this.stream.write(`${message}\n`) | ||
// noinspection JSMethodCanBeStatic | ||
get isDebugEnabled() { | ||
return debug.enabled | ||
} | ||
|
||
info(messageOrFields: Fields | null | string, message?: string) { | ||
this.doLog(message, messageOrFields, "info") | ||
} | ||
|
||
notice(messageOrFields: Fields | null | string, message?: string): void { | ||
this.doLog(message, messageOrFields, "notice") | ||
} | ||
|
||
warn(messageOrFields: Fields | null | string, message?: string): void { | ||
this.doLog(message, messageOrFields, "warn") | ||
} | ||
|
||
debug(fields: Fields | null, message: string) { | ||
if (debug.enabled) { | ||
this._doLog(message, fields, "debug") | ||
} | ||
} | ||
|
||
private doLog(message: string | undefined, messageOrFields: Fields | null | string, level: LogLevel) { | ||
if (message === undefined) { | ||
this._doLog(messageOrFields as string, null, level) | ||
} | ||
else { | ||
printer(message) | ||
this._doLog(message, messageOrFields as Fields | null, level) | ||
} | ||
} | ||
|
||
task(title: string, _promise: BluebirdPromise<any> | Promise<any>): BluebirdPromise<any> { | ||
const promise = _promise as BluebirdPromise<any> | ||
this.log(title) | ||
return promise | ||
private _doLog(message: string, fields: Fields | null, level: LogLevel) { | ||
const levelIndicator = "•" | ||
const color = LEVEL_TO_COLOR[level] | ||
this.stream.write(`${" ".repeat(PADDING)}${color(levelIndicator)} `) | ||
this.stream.write(Logger.createMessage(this.messageTransformer(message, level), fields, level, color)) | ||
this.stream.write("\n") | ||
} | ||
} | ||
|
||
class TtyLogger extends Logger { | ||
constructor(stream: WritableStream) { | ||
super(stream) | ||
} | ||
static createMessage(message: string, fields: Fields | null, level: LogLevel, color: (it: string) => string): string { | ||
let text = message | ||
|
||
warn(message: string): void { | ||
this.log(`${getEmoji("warning")} ${chalk.yellow(message)}`) | ||
} | ||
} | ||
const fieldPadding = " ".repeat(Math.max(0, 16 - message.length)) | ||
text += fieldPadding | ||
|
||
if (fields != null) { | ||
for (const name of Object.keys(fields)) { | ||
let fieldValue = fields[name] | ||
if (fieldValue != null && typeof fieldValue === "string" && fieldValue.includes("\n")) { | ||
fieldValue = ("\n" + fieldValue) | ||
.replace(/\n/g, `\n${" ".repeat(PADDING)}${fieldPadding}`) | ||
} | ||
|
||
text += ` ${color(name)}=${Array.isArray(fieldValue) ? JSON.stringify(fieldValue) : fieldValue}` | ||
} | ||
} | ||
|
||
const logger = (process.stdout as any).isTTY ? new TtyLogger(process.stdout) : new Logger(process.stdout) | ||
return text | ||
} | ||
|
||
export function warn(message: string) { | ||
logger.warn(message) | ||
log(message: string): void { | ||
if (printer == null) { | ||
this.stream.write(`${message}\n`) | ||
} | ||
else { | ||
printer(message) | ||
} | ||
} | ||
} | ||
|
||
export function log(message: string) { | ||
logger.log(message) | ||
const LEVEL_TO_COLOR: { [index: string]: Chalk } = { | ||
info: chalk.blue, | ||
notice: chalk.yellow, | ||
warn: chalk.yellow, | ||
debug: chalk.gray, | ||
} | ||
|
||
export function task(title: string, promise: BluebirdPromise<any> | Promise<any>): BluebirdPromise<any> { | ||
return logger.task(title, promise) | ||
} | ||
export const log = new Logger(process.stdout) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.