Added types hexo-log

This commit is contained in:
segayuu
2017-09-14 13:49:07 +09:00
parent bec36516c2
commit 0438cc82c3
4 changed files with 275 additions and 0 deletions
+90
View File
@@ -0,0 +1,90 @@
import mocha = require('mocha');
import chai = require('chai');
const should = chai.should(); // eslint-disable-line
import rewire = require('rewire');
import sinon = require('sinon');
import logger = require('hexo-log');
const _c = logger();
type HexoLogger = typeof _c;
describe('hexo-log', () => {
const loggerModule = rewire<(options?: { name?: string; silent?: boolean; debug?: boolean; }) => HexoLogger>('./');
it('add alias for levels', () => {
const log = logger();
log.d.should.eql(log.debug);
log.i.should.eql(log.info);
log.w.should.eql(log.warn);
log.e.should.eql(log.error);
log.log.should.eql(log.info);
});
it('default name is hexo', () => {
const log = logger();
log.fields.name.should.eql('hexo');
});
it('options.name', () => {
const log = logger({ name: 'foo' });
log.fields.name.should.eql('foo');
});
it('level should be trace if options.debug is true', () => {
const log: any = logger({ debug: true });
log.streams[0].level.should.eql(10);
});
it('should add file stream if options.debug is true', () => {
const log: any = logger({ debug: true });
log.streams[1].path.should.eql('debug.log');
});
it('should remove console stream if options.silent is true', () => {
const log: any = logger({ silent: true });
log.streams.length.should.eql(0);
});
it('should display time if options.debug is true', () => {
const spy = sinon.spy();
const now = new Date();
loggerModule.__with__({
process: {
stdout: {
write: spy
}
}
})(() => {
sinon.useFakeTimers(now.valueOf());
const log = loggerModule({ debug: true });
log.info('test');
sinon.restore(undefined);
});
spy.args[0][0].should.contain(now.toISOString().substring(11, 23));
});
it('should print error to process.stderr stream', () => {
const spy = sinon.spy();
loggerModule.__with__({
process: {
stderr: {
write: spy
}
}
})(() => {
const log = loggerModule();
log.error('test');
});
spy.calledOnce.should.be.true;
});
});
+162
View File
@@ -0,0 +1,162 @@
// Type definitions for hexo-log 0.2
// Project: http://hexo.io/
// Definitions by: segayuu <https://github.com/segayuu>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
import bunyan = require('hexo-bunyan');
import streams = require('stream');
declare class HexoLogger extends bunyan {
/* tslint:disable:unified-signatures */
/**
* Returns a boolean: is the `debug` level enabled?
*
* This is equivalent to `log.isDebugEnabled()` or `log.isEnabledFor(DEBUG)` in log4j.
*/
d(): boolean;
/**
* Special case to log an `Error` instance to the record.
* This adds an `err` field with exception details
* (including the stack) and sets `msg` to the exception
* message or you can specify the `msg`.
*/
d(error: Error, ...params: any[]): void;
/**
* The first field can optionally be a "fields" object, which
* is merged into the log record.
*
* To pass in an Error *and* other fields, use the `err`
* field name for the Error instance.
*/
d(obj: object, ...params: any[]): void;
/**
* Uses `util.format` for msg formatting.
*/
d(format: any, ...params: any[]): void;
/**
* Returns a boolean: is the `info` level enabled?
*
* This is equivalent to `log.isInfoEnabled()` or `log.isEnabledFor(INFO)` in log4j.
*/
i(): boolean;
/**
* Special case to log an `Error` instance to the record.
* This adds an `err` field with exception details
* (including the stack) and sets `msg` to the exception
* message or you can specify the `msg`.
*/
i(error: Error, ...params: any[]): void;
/**
* The first field can optionally be a "fields" object, which
* is merged into the log record.
*
* To pass in an Error *and* other fields, use the `err`
* field name for the Error instance.
*/
i(obj: object, ...params: any[]): void;
/**
* Uses `util.format` for msg formatting.
*/
i(format: any, ...params: any[]): void;
/**
* Returns a boolean: is the `warn` level enabled?
*
* This is equivalent to `log.isWarnEnabled()` or `log.isEnabledFor(WARN)` in log4j.
*/
w(): boolean;
/**
* Special case to log an `Error` instance to the record.
* This adds an `err` field with exception details
* (including the stack) and sets `msg` to the exception
* message or you can specify the `msg`.
*/
w(error: Error, ...params: any[]): void;
/**
* The first field can optionally be a "fields" object, which
* is merged into the log record.
*
* To pass in an Error *and* other fields, use the `err`
* field name for the Error instance.
*/
w(obj: object, ...params: any[]): void;
/**
* Uses `util.format` for msg formatting.
*/
w(format: any, ...params: any[]): void;
/**
* Returns a boolean: is the `error` level enabled?
*
* This is equivalent to `log.isErrorEnabled()` or `log.isEnabledFor(ERROR)` in log4j.
*/
e(): boolean;
/**
* Special case to log an `Error` instance to the record.
* This adds an `err` field with exception details
* (including the stack) and sets `msg` to the exception
* message or you can specify the `msg`.
*/
e(error: Error, ...params: any[]): void;
/**
* The first field can optionally be a "fields" object, which
* is merged into the log record.
*
* To pass in an Error *and* other fields, use the `err`
* field name for the Error instance.
*/
e(obj: object, ...params: any[]): void;
/**
* Uses `util.format` for msg formatting.
*/
e(format: any, ...params: any[]): void;
/**
* Returns a boolean: is the `info` level enabled?
*
* This is equivalent to `log.isInfoEnabled()` or `log.isEnabledFor(INFO)` in log4j.
*/
log(): boolean;
/**
* Special case to log an `Error` instance to the record.
* This adds an `err` field with exception details
* (including the stack) and sets `msg` to the exception
* message or you can specify the `msg`.
*/
log(error: Error, ...params: any[]): void;
/**
* The first field can optionally be a "fields" object, which
* is merged into the log record.
*
* To pass in an Error *and* other fields, use the `err`
* field name for the Error instance.
*/
log(obj: object, ...params: any[]): void;
/**
* Uses `util.format` for msg formatting.
*/
log(format: any, ...params: any[]): void;
/* tslint:enable:unified-signatures */
}
declare function createLogger(options?: { name?: string; silent?: boolean; debug?: boolean; }): HexoLogger;
export = createLogger;
+22
View File
@@ -0,0 +1,22 @@
{
"compilerOptions": {
"module": "commonjs",
"lib": [
"es6"
],
"noImplicitAny": true,
"noImplicitThis": true,
"strictNullChecks": true,
"baseUrl": "../",
"typeRoots": [
"../"
],
"types": [],
"noEmit": true,
"forceConsistentCasingInFileNames": true
},
"files": [
"index.d.ts",
"hexo-log-tests.ts"
]
}
+1
View File
@@ -0,0 +1 @@
{ "extends": "dtslint/dt.json" }