Skip to content

Commit

Permalink
feat: init version (#1)
Browse files Browse the repository at this point in the history
  • Loading branch information
fengmk2 committed Jul 8, 2016
1 parent d57f6d8 commit c6611cd
Show file tree
Hide file tree
Showing 42 changed files with 3,227 additions and 8 deletions.
2 changes: 0 additions & 2 deletions .eslintignore

This file was deleted.

10 changes: 9 additions & 1 deletion .eslintrc
@@ -1,3 +1,11 @@
{
"extends": "eslint-config-egg"
"extends": "eslint-config-egg",
"rules": {
"no-console": "off",
"no-magic-numbers": "off",
"generator-star-spacing": "off",
"prefer-template": "off",
"max-len": "off",
"dot-notation": "off"
}
}
5 changes: 3 additions & 2 deletions .gitignore
@@ -1,2 +1,3 @@
node_modules/
coverage/
node_modules
coverage
.logs
92 changes: 92 additions & 0 deletions benchmark/index.js
@@ -0,0 +1,92 @@
'use strict';

const Benchmark = require('benchmark');
const benchmarks = require('beautify-benchmark');
const path = require('path');
const Logger = require('..').EggLogger;
const ContextLogger = require('..').EggContextLogger;

const loggerWithJSON = new Logger({
file: path.join(__dirname, '../.logs', 'withjson.log'),
jsonFile: path.join(__dirname, '../.logs', 'withjson.json.log'),
consoleLevel: 'NONE',
encoding: 'utf8',
});

const loggerWithoutJSON = new Logger({
file: path.join(__dirname, '../.logs', 'withoutjson.log'),
encoding: 'utf8',
consoleLevel: 'NONE',
});

const ctx = {
userId: '2088102117755972',
logonId: 'money02@alitest.com',
tracer: {
traceId: 'c0a8016714537221365031005',
},
starttime: Date.now(),
ip: '127.0.0.1',
method: 'GET',
url: '/',
};
const contextLogger = new ContextLogger(ctx, loggerWithoutJSON);
const contextLoggerWithJSON = new ContextLogger(ctx, loggerWithJSON);

const err = new Error('test error');
err.data = { foo: 'bar' };
err.code = 'MOCK';

const suite = new Benchmark.Suite();

suite
.add('logger.error(err)', () => {
loggerWithoutJSON.error(err);
})
.add('logger.info(message)', () => {
loggerWithoutJSON.info('hello, %s', 'world');
})
.add('logger.write(message)', () => {
loggerWithoutJSON.write('hello, world');
})
.add('contextLogger.info(message)', () => {
contextLogger.info('hello, %s', 'world');
})
.add('logger.error(err) with JSON', () => {
loggerWithJSON.error(err);
})
.add('logger.info(message) with JSON', () => {
loggerWithJSON.info('hello, %s', 'world');
})
.add('logger.write(message) with JSON', () => {
loggerWithJSON.write('hello, world');
})
.add('contextLogger.info(message) with JSON', () => {
contextLoggerWithJSON.info('hello, %s', 'world');
})
.on('cycle', event => {
benchmarks.add(event.target);
})
.on('start', () => {
loggerWithJSON.disable('console');
loggerWithoutJSON.disable('console');
console.log('\n node version: %s, date: %s\n Starting...', process.version, Date());
})
.on('complete', () => {
benchmarks.log();
process.exit(0);
})
.run({ async: false });

// node version: v4.4.7, date: Fri Jul 08 2016 23:52:31 GMT+0800 (CST)
// Starting...
// 8 tests completed.
//
// logger.error(err) x 75,716 ops/sec ±6.78% (68 runs sampled)
// logger.info(message) x 146,234 ops/sec ±5.14% (76 runs sampled)
// logger.write(message) x 321,949 ops/sec ±8.95% (69 runs sampled)
// contextLogger.info(message) x 96,073 ops/sec ±11.27% (64 runs sampled)
// logger.error(err) with JSON x 32,696 ops/sec ±11.21% (70 runs sampled)
// logger.info(message) with JSON x 53,863 ops/sec ±9.74% (64 runs sampled)
// logger.write(message) with JSON x 196,964 ops/sec ±4.60% (80 runs sampled)
// contextLogger.info(message) with JSON x 55,410 ops/sec ±5.81% (78 runs sampled)
50 changes: 50 additions & 0 deletions benchmark/map-has.js
@@ -0,0 +1,50 @@
'use strict';

const Benchmark = require('benchmark');
const benchmarks = require('beautify-benchmark');

const map = new Map();
map.set('a', 1);
map.set('b', 2);
map.set('c', 3);
map.set('d', 4);
map.set('e', 5);

const suite = new Benchmark.Suite();

suite
.add('map.has().get()', () => {
if (map.has('a')) {
return map.get('a');
}
})
.add('map.has()', () => {
if (map.has('a')) {
return;
}
})
.add('map.get()', () => {
const val = map.get('a');
if (val) {
return val;
}
})
.on('cycle', event => {
benchmarks.add(event.target);
})
.on('start', () => {
console.log('\n node version: %s, date: %s\n Starting...', process.version, Date());
})
.on('complete', () => {
benchmarks.log();
process.exit(0);
})
.run({ async: false });

// node version: v4.4.7, date: Fri Jul 08 2016 23:53:40 GMT+0800 (CST)
// Starting...
// 3 tests completed.
//
// map.has().get() x 21,385,534 ops/sec ±2.52% (84 runs sampled)
// map.has() x 33,696,905 ops/sec ±3.71% (82 runs sampled)
// map.get() x 29,315,815 ops/sec ±6.76% (78 runs sampled)
56 changes: 56 additions & 0 deletions benchmark/values.js
@@ -0,0 +1,56 @@
'use strict';

const Benchmark = require('benchmark');
const benchmarks = require('beautify-benchmark');

const map = new Map();
for (let i = 0, l = 5; i < l; i++) {
map.set(i.toString(), i);
}

const obj = {};
for (let i = 0, l = 5; i < l; i++) {
obj[i.toString()] = i;
}

const suite = new Benchmark.Suite();

suite
.add('map.values()', () => {
const arr = [];
for (const val of map.values()) {
arr.push(val);
}
})
.add('obj for in', () => {
const arr = [];
for (const key in obj) {
arr.push(obj[key]);
}
})
.add('Object.keys', () => {
const arr = [];
const keys = Object.keys(obj);
for (let i = 0, l = keys.length; i < l; i++) {
arr.push(obj[keys[i]]);
}
})
.on('cycle', event => {
benchmarks.add(event.target);
})
.on('start', () => {
console.log('\n node version: %s, date: %s\n Starting...', process.version, Date());
})
.on('complete', () => {
benchmarks.log();
process.exit(0);
})
.run({ async: false });

// node version: v4.4.7, date: Fri Jul 08 2016 23:54:36 GMT+0800 (CST)
// Starting...
// 3 tests completed.
//
// map.values() x 794,226 ops/sec ±7.04% (78 runs sampled)
// obj for in x 539,445 ops/sec ±2.08% (86 runs sampled)
// Object.keys x 2,064,693 ops/sec ±1.35% (86 runs sampled)
18 changes: 18 additions & 0 deletions index.js
@@ -0,0 +1,18 @@
'use strict';

module.exports.Logger = require('./lib/logger');

module.exports.Transport = require('./lib/transports/transport');
module.exports.FileBufferTransport = require('./lib/transports/file_buffer');
module.exports.FileTransport = require('./lib/transports/file');
module.exports.ConsoleTransport = require('./lib/transports/console');

module.exports.EggLogger = require('./lib/egg/logger');
module.exports.EggErrorLogger = require('./lib/egg/error_logger');
module.exports.EggConsoleLogger = require('./lib/egg/console_logger');
module.exports.EggCustomLogger = require('./lib/egg/custom_logger');
module.exports.EggContextLogger = require('./lib/egg/context_logger');
module.exports.EggLoggers = require('./lib/egg/loggers');

module.exports.levels = require('./lib/level');
Object.assign(module.exports, module.exports.levels);
49 changes: 49 additions & 0 deletions lib/egg/console_logger.js
@@ -0,0 +1,49 @@
'use strict';

const Logger = require('../logger');
const ConsoleTransport = require('../transports/console');
const FileTransport = require('../transports/file');
const utils = require('../utils');

/**
* Terminal Logger, send log to console.
*/
class ConsoleLogger extends Logger {

/**
* @constructor
* @param {Object} options
* - {String} [errorFile] - error file
* if specify this file, logger will redirect error log to the file.
* - {String} [encoding] - log string encoding, default is 'utf8'
*/
constructor(options) {
super(options);

this.set('console', new ConsoleTransport({
level: this.options.level,
formatter: utils.consoleFormatter,
}));

if (this.options.errorFile) {
const fileTransport = new FileTransport({
file: this.options.errorFile,
level: 'ERROR', // 只输出 error 级别的到文件
encoding: this.options.encoding,
formatter: utils.defaultFormatter,
});
this.set('file', fileTransport);
}
}

get defaults() {
return {
errorFile: '',
encoding: 'utf8',
level: process.env.NODE_ENV === 'production' ? 'INFO' : 'WARN',
};
}

}

module.exports = ConsoleLogger;
54 changes: 54 additions & 0 deletions lib/egg/context_logger.js
@@ -0,0 +1,54 @@
'use strict';

/**
* Request context Logger, itself isn't a {@link Logger}.
*/
class ContextLogger {

/**
* @constructor
* @param {Context} ctx - egg Context instance
* @param {Logger} logger - Logger instance
*/
constructor(ctx, logger) {
this.ctx = ctx;
this._logger = logger;
}

get paddingMessage() {
const ctx = this.ctx;

// Auto record necessary request context infomation, e.g.: user id, request spend time
// format: '[$logonId/$userId/$ip/$traceId/$use_ms $method $url]'
const userId = ctx.userId || '-';
const logonId = ctx.logonId || '-';
const traceId = ctx.tracer && ctx.tracer.traceId || '-';
const use = ctx.starttime ? Date.now() - ctx.starttime : 0;
return '[' +
logonId + '/' +
userId + '/' +
ctx.ip + '/' +
traceId + '/' +
use + 'ms ' +
ctx.method + ' ' +
ctx.url +
']';
}
}

[ 'error', 'warn', 'info', 'debug' ].forEach(level => {
const LEVEL = level.toUpperCase();
ContextLogger.prototype[level] = function() {
const meta = {
formatter: contextFormatter,
paddingMessage: this.paddingMessage,
};
this._logger.log(LEVEL, arguments, meta);
};
});

module.exports = ContextLogger;

function contextFormatter(meta) {
return meta.date + ' ' + meta.level + ' ' + meta.pid + ' ' + meta.paddingMessage + ' ' + meta.message;
}
10 changes: 10 additions & 0 deletions lib/egg/custom_logger.js
@@ -0,0 +1,10 @@
'use strict';

const Logger = require('./logger');

/**
* Custom Logger
*/
class CustomLogger extends Logger {}

module.exports = CustomLogger;
31 changes: 31 additions & 0 deletions lib/egg/error_logger.js
@@ -0,0 +1,31 @@
'use strict';

const Logger = require('./logger');
const levels = require('../level');
const utils = require('../utils');


/**
* Error Logger, only print `ERROR` level log.
* level and consoleLevel should >= `ERROR` level.
*/
class ErrorLogger extends Logger {
constructor(options) {
options = options || {};
options.level = getDefaultLevel(options.level);
options.consoleLevel = getDefaultLevel(options.consoleLevel);
super(options);
}
}

module.exports = ErrorLogger;

function getDefaultLevel(level) {
level = utils.normalizeLevel(level);

if (level === undefined) {
return levels.ERROR;
}

return level > levels.ERROR ? level : levels.ERROR;
}

0 comments on commit c6611cd

Please sign in to comment.