colorogger
v1.3.6
Published
A simple colorful logger. Both for browser and node-server. Easy to report and record.
Downloads
5
Readme
colorogger
A simple colorful logger. Both for browser and node-server. Easy to report and record.
Demo
server demo
browser demo
Getting Started
install
npm i colorogger
server
const colorogger = require('colorogger');
const logger = new colorogger();
logger.log('log');
logger.info('info');
logger.debug('debug');
logger.warn('warn');
logger.error('error');
logger.success('success');
logger.fail('fail');
logger.tip('tip');
logger.stress('stress');
browser (with webpack)
const colorogger = require('colorogger/lib/client');
const logger = new colorogger();
logger.success('message');
Options
color
Enable colored log.
- type:
Boolean
- default:
true
timeStamp
Enable time stamp.
- type:
Boolean
- default:
true
stringify
Make log stringify and remove breaks.
- type:
Boolean
- default:
false
Enable logger print log to console.
- type:
Boolean
- default:
true
level
Enable logger log to console by level grade.
- type:
Number
- default:
0
default levels grades:
- 0: debug
- 1: log, tip
- 2: info, success
- 3: warn, stress
- 4: error
timeTemplate
Format time stamp.
- type:
String
- default:
'{{YYYY}}/{{MM}}/{{DD}} {{hh}}:{{mm}}:{{ss}}.{{mss}}'
wrapIcon
Format icon log text.
- type:
Function
- default:
(icon) => \
[${icon}]``
wrapTag
Format meta key log text.
- type:
Function
- default:
(tag, key) => \
[${tag}]``
meta
Extra log info.
- type:
Object
- default:
undefined
Example:
const logger = new colorogger({
timeStamp: false,
meta: {
guid: 'guid_abc'
}
});
logger.log('log text');
// console output: [.] [guid_abc] log text
transport
Resive message object.
- type:
Function
- default:
null
Example:
const logger = new colorogger({
meta: {
guid: 'guid_123',
},
transport(msg) {
console.log(msg);
}
});
logger.success('log text');
// msg.content: ['log text']
// msg.flag: 'success'
// msg.grade: 1
// msg.guid: 'guid_123'
// msg.level: log
// msg.time: 1599215203843
Api
config(options)
Set logger config.
- param [Object] options: constructor options
Example:
const logger = new colorogger();
logger.config({
meta: {
guid: 'guid_123',
},
transport(msg) {
console.log(msg);
}
})
logger.log('log text');
// msg.content: ['log text']
// msg.flag: ''
// msg.grade: 1
// msg.guid: 'guid_123'
// msg.level: log
// msg.time: 1599215203843
fork(options?)
Clone instance.
- param [Object] options: constructor options
Example:
const logger = new colorogger({
meta: {
guid: 'guid_123',
},
transport(msg) {
console.log(msg);
}
});
const forkLogger = logger.fork();
forkLogger.log('log text');
// msg.content: ['log text']
// msg.guid: 'guid_123'
forkLogger.success('success text');
// msg.content: ['success text']
// msg.guid: 'guid_123'
method(name, options?)
Extend a method for logger instance.
- param [String] name: method name
- param [Object] options: method options
- param [String] options.level: Method log level. Optional value: ['debug', 'log', 'info', 'warn', 'error'].
- param [String] options.flag: Method flag name, will be appended to transport msg property.
- param [String] options.color: Method color, use it will ignore theme color.
Example:
const logger = new colorogger({
transport(msg) {
console.log(msg);
}
});
logger.method('intro', {
level: 'info',
flag: 'intro',
color: '#ff9501',
});
logger.intro('intro text');
// msg.content: ['intro text']
// msg.flag: 'intro'
// msg.level: 'info'
theme(options)
Custom color setting.
- param [Object] options: theme options
- param [Object] options.colors: Configure the color of the method output log.
- param [String] options.colors[name]: color value
- param [Object] options.icons: icon options
- param [Object] options.icons[name]: Configure the icon for the method output log.
- param [String] options.icons[name].icon: icon string
- param [String] options.icons[name].color: icon color
- param [Object] options.icons[name]: Configure the icon for the method output log.
- param [Object] options.colors: Configure the color of the method output log.
Example:
const logger = new colorogger({
transport(msg) {
console.log(msg);
}
});
logger.method('intro', {
level: 'info',
flag: 'intro',
});
logger.theme({
colors: {
intro: '#ff9501',
},
icons: {
intro: {
icon: 'i',
color: '#00ff00',
}
}
});
logger.intro('intro text');
destroy()
Destroy instance.