Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make it work in Node #10

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 29 additions & 12 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ var defaultColors = {
brightBlack: '#747369'
}

var process = require('process') // browser exclude
var isBrowser = typeof window !== 'undefined'

module.exports = Nanologger

function Nanologger (name, opts) {
Expand All @@ -47,7 +50,7 @@ function Nanologger (name, opts) {
try {
this.logLevel = window.localStorage.getItem('logLevel') || 'info'
} catch (e) {
this.logLevel = 'info'
this.logLevel = (process.env && process.env.LOG_LEVEL) || 'info'
}

this._logLevel = levels[this.logLevel]
Expand Down Expand Up @@ -106,38 +109,52 @@ Nanologger.prototype._print = function (level) {
var args = [ null ]
var msg = '%c%s ' + emoji + ' %c%s'

args.push(color(this._colors.brightBlack), time)
args.push(color(this._colors.magenta), name)
if (isBrowser) {
args.push(color(this._colors.brightBlack), time)
} else {
args[0] = time
args.push(emoji + ' ')
}
if (isBrowser) args.push(color(this._colors.magenta))
args.push(name)

for (var i = 1, len = arguments.length; i < len; i++) {
var arg = arguments[i]
if (typeof arg === 'string') {
if (i === 1) {
// first string argument is in color
msg += ' %c%s'
args.push(color(msgColor))
if (isBrowser) {
msg += ' %c%s'
args.push(color(msgColor))
}
args.push(arg)
} else if (/ms$/.test(arg)) {
// arguments finishing with 'ms', grey out
msg += ' %c%s'
args.push(color(this._colors.brightBlack))
if (isBrowser) {
msg += ' %c%s'
args.push(color(this._colors.brightBlack))
}
args.push(arg)
} else {
// normal colors
msg += ' %c%s'
args.push(color(this._colors.white))
if (isBrowser) {
msg += ' %c%s'
args.push(color(this._colors.white))
}
args.push(arg)
}
} else if (typeof arg === 'number') {
msg += ' %c%d'
args.push(color(this._colors.magenta))
if (isBrowser) {
msg += ' %c%d'
args.push(color(this._colors.magenta))
}
args.push(arg)
} else {
objs.push(arg)
}
}

args[0] = msg
if (isBrowser) args[0] = msg
objs.forEach(function (obj) {
args.push(obj)
})
Expand Down
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
"start": "node .",
"test": "standard && npm run deps"
},
"browser": {
"process": false
},
"dependencies": {
"xtend": "^4.0.1"
},
Expand Down