0.1.1 • Published 8 years ago

gre v0.1.1

Weekly downloads
7
License
MIT
Repository
github
Last release
8 years ago

gre

日志输出

Install

npm i gre --save

Example 1

var gre = require('gre');
// dev模式
var g = gre.create();

g.log('hello');
g.trace('hello', 'world');
g.debug('hello',  'world', 123);
g.info('hello',  'world', 123, {foo:'bar'});
g.warn('hello', 'world', 123, {foo:'bar'});
g.error('hello', 'world', 123, {foo:'bar'}, [1, 2, 3, 4], Object);

==> Result :
result

Example 2

var gre = require('gre');
// tiny模式 最小化输出
var g = gre.create('tiny');

g.log('hello');
g.trace('hello', 'world');
g.debug('hello',  'world', 123);
g.info('hello',  'world', 123, {foo:'bar'});
g.warn('hello', 'world', 123, {foo:'bar'});
g.error('hello', 'world', 123, {foo:'bar'}, [1, 2, 3, 4], Object);

==> Result :
result

Example 3

var gre = require('gre');
// prod模式
g = gre.create({
    format: 'prod',
    project: path.dirname(__dirname)
})

g.log('hello');
g.trace('hello', 'world');
g.debug('hello',  'world', 123);
g.info('hello',  'world', 123, {foo:'bar'});
g.warn('hello', 'world', 123, {foo:'bar'});
g.error('hello', 'world', 123, {foo:'bar'}, [1, 2, 3, 4], Object);

==> Result :
result

Options

{
    // 格式化字符串
    format: 'dev',
    // 是否彩色输出
    color: true,
    // 项目路径,配置次参数:path输出相对路径,不配置:path输出绝对路径
    project: '',
    // 默认时间格式化字符串
    dateformat: 'yyyy-MM-dd hh:mm:ss'
}

Format String

内置配置

dev: '[:file::line] :message',
tiny: ':message',
prod: '[:time[yyyy-MM-dd hh:mm:ss]][:title][:path::line] :message',
full: '[:time[yyyy-MM-dd hh:mm:ss.SSS]][:title][:path::line] :message',
time: '[:time[hh:mm:ss]][:file::line] :message',
fulltime: '[:time][:file::line] :message'

所有关键字

:time[yyyy-MM-dd hh:mm:ss.SSS] :title :file :path :line :message

自定义关键字 & 自定义日志输出格式

var os = require('os');
var gre = require('gre');

// 自定义关键字
gre.token('platform', () => os.platform())

// 自定义日志输出格式
var g = gre.create('[:time[hh:mm:ss]][:platform][:file::line] :message');

g.log('hello');
g.trace('hello', 'world');
g.debug('hello',  'world', 123);
g.info('hello',  'world', 123, {foo:'bar'});
g.warn('hello', 'world', 123, {foo:'bar'});
g.error('hello', 'world', 123, {foo:'bar'}, [1, 2, 3, 4], Object);

==> Result :
result


npm test