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

refactor: completely rewrite the logger #34

Merged
merged 12 commits into from
May 29, 2020
4 changes: 2 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ cache:
- node_modules

node_js:
- "8"
- "10"
- "node"
- "12"
- "14"

script:
- npm run eslint
Expand Down
13 changes: 5 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
[![NPM version](https://badge.fury.io/js/hexo-log.svg)](https://www.npmjs.com/package/hexo-log)
[![Coverage Status](https://coveralls.io/repos/hexojs/hexo-log/badge.svg?branch=master)](https://coveralls.io/r/hexojs/hexo-log?branch=master)

Logger for Hexo, based on [bunyan] with better console output.
Logger for Hexo.

## Installation

Expand All @@ -15,22 +15,19 @@ $ npm install hexo-log --save
## Usage

``` js
var log = require('hexo-log')({
const log = require('hexo-log')({
debug: false,
silent: false
});
})

log.info('Hello world');
```

Option | Description | Default
--- | --- | ---
`debug` | Display debug message and save log to `debug.log` file. | `false`
`silent` | Don't display message in console. | `false`
`name` | Name | `hexo`
`debug` | Display debug message. | `false`
`silent` | Don't display any message in console. | `false`

## License

SukkaW marked this conversation as resolved.
Show resolved Hide resolved
MIT

[bunyan]: https://github.com/trentm/node-bunyan
128 changes: 73 additions & 55 deletions lib/log.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
'use strict';

const bunyan = require('hexo-bunyan');
const { Console } = require('console');
const chalk = require('chalk');
const Writable = require('stream').Writable;

const levelNames = {
const TRACE = 10;
const DEBUG = 20;
const INFO = 30;
const WARN = 40;
const ERROR = 50;
const FATAL = 60;

const LEVEL_NAMES = {
10: 'TRACE',
20: 'DEBUG',
30: 'INFO ',
Expand All @@ -13,7 +19,7 @@ const levelNames = {
60: 'FATAL'
};

const levelColors = {
const LEVEL_COLORS = {
10: 'gray',
20: 'gray',
30: 'green',
Expand All @@ -22,77 +28,89 @@ const levelColors = {
60: 'bgRed'
};

function ConsoleStream(env) {
Writable.call(this, {objectMode: true});
const console = new Console({
stdout: process.stdout,
stderr: process.stderr,
colorMode: false
});

this.debug = env.debug;
}
class Logger {
constructor(options = {}) {
const silent = options.silent || false;
this._debug = options.debug || false;

require('util').inherits(ConsoleStream, Writable);
this.level = INFO;

ConsoleStream.prototype._write = function(data, enc, callback) {
const level = data.level;
let msg = '';
if (silent) {
this.level = FATAL + 10;
}

// Time
if (this.debug) {
msg += chalk.gray(formatTime(data.time)) + ' ';
if (this._debug) {
this.level = TRACE;
}
}

// Level
msg += chalk[levelColors[level]](levelNames[level]) + ' ';
_writeLogOutput(level, consoleArgs) {
if (this._debug) {
const str = new Date().toISOString().substring(11, 23) + ' ';

// Message
msg += data.msg + '\n';
if (level === TRACE || level >= WARN) {
process.stderr.write(chalk[LEVEL_COLORS[DEBUG]](str));
} else {
process.stdout.write(chalk[LEVEL_COLORS[DEBUG]](str));
}
}

// Error
if (data.err) {
const err = data.err.stack || data.err.message;
if (err) msg += chalk.yellow(err) + '\n';
if (level >= this.level) {
const str = chalk[LEVEL_COLORS[level]](LEVEL_NAMES[level]) + ' ';
if (level === TRACE || level >= WARN) {
process.stderr.write(str);
} else {
process.stdout.write(str);
}

if (level === TRACE) {
console.trace(...consoleArgs);
} else if (level < INFO) {
console.debug(...consoleArgs);
} else if (level < WARN) {
console.info(...consoleArgs);
} else if (level < ERROR) {
console.warn(...consoleArgs);
} else {
console.error(...consoleArgs);
}
}
}

if (level >= 40) {
process.stderr.write(msg);
} else {
process.stdout.write(msg);
trace(...args) {
this._writeLogOutput(TRACE, args);
}

callback();
};

function formatTime(date) {
return date.toISOString().substring(11, 23);
}
debug(...args) {
this._writeLogOutput(DEBUG, args);
}

function createLogger(options) {
options = options || {};
info(...args) {
this._writeLogOutput(INFO, args);
}

const streams = [];
warn(...args) {
this._writeLogOutput(WARN, args);
}

if (!options.silent) {
streams.push({
type: 'raw',
level: options.debug ? 'trace' : 'info',
stream: new ConsoleStream(options)
});
error(...args) {
this._writeLogOutput(ERROR, args);
}

if (options.debug) {
streams.push({
level: 'trace',
path: 'debug.log'
});
fatal(...args) {
this._writeLogOutput(FATAL, args);
}
}

const logger = bunyan.createLogger({
name: options.name || 'hexo',
streams,
serializers: {
err: bunyan.stdSerializers.err
}
});
function createLogger(options) {
const logger = new Logger(options);

// Alias for logger levels
logger.d = logger.debug;
logger.i = logger.info;
logger.w = logger.warn;
Expand Down
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
],
"license": "MIT",
"dependencies": {
"hexo-bunyan": "^2.0.0",
"chalk": "^3.0.0"
},
"devDependencies": {
Expand All @@ -43,6 +42,6 @@
"sinon": "^8.0.1"
},
"engines": {
"node": ">=8.6.0"
"node": ">=10.13.0"
}
}
Loading