Skip to content

Commit b8c266e

Browse files
author
Shane Osbourne
committed
just chalk
1 parent 80dfac4 commit b8c266e

File tree

5 files changed

+2327
-787
lines changed

5 files changed

+2327
-787
lines changed

.jshintrc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,6 @@
1515
"eqnull": true,
1616
"node": true,
1717
"strict": false,
18-
"mocha": true
18+
"mocha": true,
19+
"esversion": 6
1920
}

index.js

Lines changed: 26 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/**
22
* tFunk for colours/compiler
33
*/
4-
var tfunk = require("tfunk");
4+
var chalk = require("chalk");
55

66
/**
77
* Lodash clonedeep & merge
@@ -40,10 +40,10 @@ var defaults = {
4040
*/
4141
prefixes: {
4242
"trace": "[trace] ",
43-
"debug": "{yellow:[debug]} ",
44-
"info": "{cyan:[info]} ",
45-
"warn": "{magenta:[warn]} ",
46-
"error": "{red:[error]} "
43+
"debug": chalk.yellow("[debug] "),
44+
"info": chalk.cyan("[info] "),
45+
"warn": chalk.magenta("[warn] "),
46+
"error": chalk.red("[error] ")
4747
},
4848

4949
/**
@@ -68,7 +68,6 @@ var Logger = function(config) {
6868
this._mute = false;
6969
this.config = _.merge({}, defaults, config);
7070
this.addLevelMethods(this.config.levels);
71-
this.compiler = new tfunk.Compiler(this.config.custom || {}, this.config);
7271
this._memo = {};
7372

7473
return this;
@@ -176,12 +175,7 @@ Logger.prototype.setLevelPrefixes = function (state) {
176175
* @param prefix
177176
*/
178177
Logger.prototype.setPrefix = function (prefix) {
179-
if (typeof prefix === "string") {
180-
this.compiler.prefix = this.compiler.compile(prefix, true);
181-
}
182-
if (typeof prefix === "function") {
183-
this.compiler.prefix = prefix;
184-
}
178+
this.config.prefix = strOrFn(prefix);
185179
};
186180

187181
/**
@@ -200,7 +194,7 @@ Logger.prototype.unprefixed = function (level, msg) {
200194

201195
/**
202196
* @param {Array} args
203-
* @param {String} msg
197+
* @param {()=>String} msg
204198
* @param {String} level
205199
* @param {boolean} [unprefixed]
206200
* @returns {Logger}
@@ -213,13 +207,16 @@ Logger.prototype.logOne = function (args, msg, level, unprefixed) {
213207

214208
args = args.slice(2);
215209

210+
var incomingMessage = typeof msg === "string" ? msg : msg();
211+
216212
if (this.config.useLevelPrefixes && !unprefixed) {
217-
msg = this.config.prefixes[level] + msg;
213+
incomingMessage = this.config.prefixes[level] + incomingMessage;
218214
}
219215

220-
msg = this.compiler.compile(msg, unprefixed);
216+
var prefix = strOrFn(this.config.prefix);
217+
var result = unprefixed ? [incomingMessage] : [prefix, incomingMessage];
221218

222-
args.unshift(msg);
219+
args.unshift(result.join(""));
223220

224221
console.log.apply(console, args);
225222

@@ -265,5 +262,17 @@ Logger.prototype.clone = function (opts) {
265262
return new Logger(config);
266263
};
267264

265+
/**
266+
* @param input
267+
*/
268+
function strOrFn(input) {
269+
if (typeof input === "string") {
270+
return input;
271+
}
272+
if (typeof input === "function") {
273+
return input();
274+
}
275+
throw new Error("unreachable");
276+
}
277+
268278
module.exports.Logger = Logger;
269-
module.exports.compile = tfunk;

0 commit comments

Comments
 (0)