beautiful-log
Advanced tools
Comparing version 1.2.1 to 1.3.0
53
index.js
@@ -13,2 +13,8 @@ "use strict"; | ||
const COLOR_FN_MAP = new Map(); | ||
const stdout = console.log; | ||
const stderr = console.error; | ||
let tagPlaceholder = ""; | ||
for (let i = 0; i < TAG_WIDTH; i++) { | ||
tagPlaceholder += " "; | ||
} | ||
COLOR_FN_MAP.set("black", (x) => "\x1b[30m" + x + "\x1b[39m"); | ||
@@ -26,14 +32,11 @@ COLOR_FN_MAP.set("red", (x) => "\x1b[31m" + x + "\x1b[39m"); | ||
const INDENT_WIDTH = 4; | ||
let INDENT = 0; | ||
let oneIndent = ""; | ||
for (let i = 0; i < INDENT_WIDTH; i++) { | ||
oneIndent += " "; | ||
} | ||
let fullIndent = ""; | ||
function print(str, printfn, color) { | ||
let caller = stack()[2]; | ||
let tag = sprintf_js_1.sprintf(TAG_FORMAT, caller.getFunctionName() || "anonymous", caller.getFileName().split("/").pop(), caller.getLineNumber()); | ||
let oneIndent = ""; | ||
for (let i = 0; i < INDENT_WIDTH; i++) { | ||
oneIndent += " "; | ||
} | ||
let fullIndent = ""; | ||
for (let i = 0; i < INDENT; i++) { | ||
fullIndent += oneIndent; | ||
} | ||
str = str.replace(/\n/g, "\n" + tagPlaceholder + fullIndent); | ||
let toPrint = sprintf_js_1.sprintf(MSG_FORMAT, tag, fullIndent, str); | ||
@@ -54,23 +57,23 @@ printfn(color(toPrint)); | ||
function verbose(...args) { | ||
print(args.map(inspect).join(" "), console.log, getColorFn("gray")); | ||
print(args.map(inspect).join(" "), stdout, getColorFn("gray")); | ||
} | ||
exports.verbose = verbose; | ||
function log(...args) { | ||
print(args.map(inspect).join(" "), console.log, colorize); | ||
print(args.map(inspect).join(" "), stdout, colorize); | ||
} | ||
exports.log = log; | ||
function info(...args) { | ||
print(args.map(inspect).join(" "), console.info, getColorFn("blue")); | ||
print(args.map(inspect).join(" "), stdout, getColorFn("blue")); | ||
} | ||
exports.info = info; | ||
function warn(...args) { | ||
print(args.map(inspect).join(" "), console.warn, getColorFn("yellow")); | ||
print(args.map(inspect).join(" "), stdout, getColorFn("yellow")); | ||
} | ||
exports.warn = warn; | ||
function error(...args) { | ||
print(args.map(inspect).join(" "), console.error, getColorFn("red")); | ||
print(args.map(inspect).join(" "), stderr, getColorFn("red")); | ||
} | ||
exports.error = error; | ||
function ok(...args) { | ||
print(args.map(inspect).join(" "), console.error, getColorFn("green")); | ||
print(args.map(inspect).join(" "), stdout, getColorFn("green")); | ||
} | ||
@@ -82,3 +85,5 @@ exports.ok = ok; | ||
} | ||
INDENT += amount; | ||
for (let i = 0; i < amount; i++) { | ||
fullIndent += oneIndent; | ||
} | ||
} | ||
@@ -90,3 +95,4 @@ exports.indent = indent; | ||
} | ||
INDENT -= amount; | ||
let rem = INDENT_WIDTH * amount; | ||
fullIndent = fullIndent.substring(rem); | ||
} | ||
@@ -107,10 +113,9 @@ exports.unindent = unindent; | ||
} | ||
line(2); | ||
stdout(text); | ||
line(); | ||
line(); | ||
console.log(text); | ||
line(); | ||
} | ||
exports.divider = divider; | ||
function timestamp() { | ||
print(moment().format("YYYY-MM-DD HH:mm:ss"), console.log, colorize); | ||
print(moment().format("YYYY-MM-DD HH:mm:ss"), stdout, colorize); | ||
} | ||
@@ -120,6 +125,6 @@ exports.timestamp = timestamp; | ||
if (FORMATS.has(format)) { | ||
print(sprintf_js_1.sprintf(FORMATS.get(format), ...args), console.log, colorize); | ||
print(sprintf_js_1.sprintf(FORMATS.get(format), ...args), stdout, colorize); | ||
} | ||
else { | ||
print(sprintf_js_1.sprintf(format, ...args), console.log, colorize); | ||
print(sprintf_js_1.sprintf(format, ...args), stdout, colorize); | ||
} | ||
@@ -133,3 +138,3 @@ } | ||
for (let i = 0; i < count; i++) { | ||
console.log(); | ||
stdout("\n"); | ||
} | ||
@@ -136,0 +141,0 @@ } |
63
index.ts
@@ -16,2 +16,11 @@ "use strict"; | ||
const stdout = console.log; | ||
const stderr = console.error; | ||
let tagPlaceholder: string = ""; | ||
for (let i = 0; i < TAG_WIDTH; i++) { | ||
tagPlaceholder += " "; | ||
} | ||
COLOR_FN_MAP.set("black", (x) => "\x1b[30m" + x + "\x1b[39m"); | ||
@@ -31,4 +40,11 @@ COLOR_FN_MAP.set("red", (x) => "\x1b[31m" + x + "\x1b[39m"); | ||
const INDENT_WIDTH: number = 4; | ||
let INDENT: number = 0; | ||
let oneIndent: string = ""; | ||
for (let i = 0; i < INDENT_WIDTH; i++) { | ||
oneIndent += " "; | ||
} | ||
let fullIndent: string = ""; | ||
function print(str: string, printfn: (s: string) => void, color: (s: string) => string): void { | ||
@@ -41,14 +57,4 @@ let caller = stack()[2]; | ||
let oneIndent: string = ""; | ||
str = str.replace(/\n/g, "\n" + tagPlaceholder + fullIndent); | ||
for (let i = 0; i < INDENT_WIDTH; i++) { | ||
oneIndent += " "; | ||
} | ||
let fullIndent: string = ""; | ||
for (let i = 0; i < INDENT; i++) { | ||
fullIndent += oneIndent; | ||
} | ||
let toPrint: string = sprintf(MSG_FORMAT, tag, fullIndent, str); | ||
@@ -70,23 +76,23 @@ | ||
export function verbose(...args: any[]): void { | ||
print(args.map(inspect).join(" "), console.log, getColorFn("gray")); | ||
print(args.map(inspect).join(" "), stdout, getColorFn("gray")); | ||
} | ||
export function log(...args: any[]): void { | ||
print(args.map(inspect).join(" "), console.log, colorize); | ||
print(args.map(inspect).join(" "), stdout, colorize); | ||
} | ||
export function info(...args: any[]): void { | ||
print(args.map(inspect).join(" "), console.info, getColorFn("blue")); | ||
print(args.map(inspect).join(" "), stdout, getColorFn("blue")); | ||
} | ||
export function warn(...args: any[]): void { | ||
print(args.map(inspect).join(" "), console.warn, getColorFn("yellow")); | ||
print(args.map(inspect).join(" "), stdout, getColorFn("yellow")); | ||
} | ||
export function error(...args: any[]): void { | ||
print(args.map(inspect).join(" "), console.error, getColorFn("red")); | ||
print(args.map(inspect).join(" "), stderr, getColorFn("red")); | ||
} | ||
export function ok(...args: any[]): void { | ||
print(args.map(inspect).join(" "), console.error, getColorFn("green")); | ||
print(args.map(inspect).join(" "), stdout, getColorFn("green")); | ||
} | ||
@@ -99,3 +105,5 @@ | ||
INDENT += amount; | ||
for (let i = 0; i < amount; i++) { | ||
fullIndent += oneIndent; | ||
} | ||
} | ||
@@ -108,3 +116,5 @@ | ||
INDENT -= amount; | ||
let rem = INDENT_WIDTH * amount; | ||
fullIndent = fullIndent.substring(rem); | ||
} | ||
@@ -129,10 +139,9 @@ | ||
line(2); | ||
stdout(text); | ||
line(); | ||
line(); | ||
console.log(text); | ||
line(); | ||
} | ||
export function timestamp(): void { | ||
print(moment().format("YYYY-MM-DD HH:mm:ss"), console.log, colorize); | ||
print(moment().format("YYYY-MM-DD HH:mm:ss"), stdout, colorize); | ||
} | ||
@@ -142,5 +151,5 @@ | ||
if (FORMATS.has(format)) { | ||
print(sprintf(FORMATS.get(format), ...args), console.log, colorize); | ||
print(sprintf(FORMATS.get(format), ...args), stdout, colorize); | ||
} else { | ||
print(sprintf(format, ...args), console.log, colorize); | ||
print(sprintf(format, ...args), stdout, colorize); | ||
} | ||
@@ -155,3 +164,3 @@ } | ||
for (let i = 0; i < count; i++) { | ||
console.log(); | ||
stdout("\n"); | ||
} | ||
@@ -158,0 +167,0 @@ } |
{ | ||
"name": "beautiful-log", | ||
"version": "1.2.1", | ||
"version": "1.3.0", | ||
"description": "Because logging should be beautiful.", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
788795
17672