Skip to content

Commit 7ee8c5d

Browse files
authored
chore(almin-logger): export { AlminLogger } and remove SyncLogger (#269)
* chore(almin-logger): export { AlminLogger } from index BREAKING CHANGE: Remove SyncLogger If you use commonjs require, please use `require("almin-logger").default`. * docs(almin-logger): Reflect removal synclogger * test(almin-logger): add start/stop test * docs(almin-logger): Update README
1 parent f87af46 commit 7ee8c5d

File tree

10 files changed

+93
-243
lines changed

10 files changed

+93
-243
lines changed

packages/almin-logger/README.md

Lines changed: 6 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ Logger class for [Almin.js](https://github.com/almin/almin "Almin.js")
1010
- Multiple Execution warning log of UseCase
1111
- Changed log of Store
1212
- Nesting log support if the browser support`console.groupCollapsed`.
13-
- Async(by default) and Sync logger
13+
- Async logging
1414

1515
## Mark meaning
1616

@@ -27,7 +27,7 @@ Old IE need [console-polyfill](https://github.com/paulmillr/console-polyfill "co
2727
## Usage
2828

2929
```js
30-
import AlminLogger from "almin-logger";
30+
import { AlminLogger } from "almin-logger";
3131
// your store
3232
import AppStore from "./stores/AppStore";
3333
// context
@@ -53,52 +53,24 @@ See [Examples](./examples) for more details.
5353

5454
```js
5555
const DefaultOptions = {
56-
// output log asynchronously
57-
async: true,
5856
// use `console` object for logging
5957
console: console,
6058
};
6159
```
6260

63-
### Async mode
61+
### Async
6462

6563
Default: output log asynchronously
6664

67-
**Pros**
68-
6965
- no mixed UseCase/Dispatch log and the other log.
7066

71-
**Cons**
72-
73-
- Async log may confuse
74-
75-
### Sync mode
76-
77-
Set `async: false` to options.
78-
79-
```js
80-
// Create Logger
81-
const logger = new ContextLogger({
82-
async: false
83-
});
84-
// Start logger
85-
logger.startLogging(appContext);
86-
```
87-
88-
**Pros**
89-
90-
- mixed UseCase/Dispatch log and the other log.
91-
- time-series logging
92-
- A `groupCollapsed` contain the error that is occurred during executing A UseCase.
93-
94-
**Cons**
67+
### <del>Sync mode</del>
9568

96-
- mixed UseCase/Dispatch log and the other log.
97-
- It make confuse.
69+
Sync mode is removed since almin-logger 6.0.
9870

9971
## FAQ
10072

101-
### IE 11 always show log like "Dispatch".
73+
### IE 11 always show un-meaning name like "Dispatch".
10274

10375
IE 11 not have `Function.name`.
10476
almin-logger depended on `Function.name` or `Function.displayName`.

packages/almin-logger/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
"log"
4141
],
4242
"devDependencies": {
43+
"@types/mocha": "^2.2.41",
4344
"@types/node": "^8.0.16",
4445
"almin": "^0.13.10",
4546
"cross-env": "^5.0.1",

packages/almin-logger/src/AlminLogger.ts

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,21 @@
11
// LICENSE : MIT
22
"use strict";
33

4-
import SyncLogger from "./SyncLogger";
54
import AsyncLogger from "./AsyncLogger";
65
import { EventEmitter } from "events";
76
import { Context } from "almin";
87

9-
const DefaultOptions = {
10-
// output log asynchronously
11-
async: true,
8+
const AlminLoggerDefaultOptions = {
129
// use `console` object for logging
1310
console: console
1411
};
1512

1613
export interface AlminLoggerOptions {
1714
console?: any;
18-
async?: boolean;
1915
}
2016

21-
export default class AlminLogger extends EventEmitter {
22-
private isAsyncMode: boolean;
23-
private logger: AsyncLogger | SyncLogger;
17+
export class AlminLogger extends EventEmitter {
18+
private logger: AsyncLogger;
2419

2520
/**
2621
* Event constants values
@@ -35,17 +30,12 @@ export default class AlminLogger extends EventEmitter {
3530
constructor(options: AlminLoggerOptions = {}) {
3631
super();
3732
// default logger is `console`
38-
const console = options.console || DefaultOptions.console;
39-
const isAsyncMode = options.async !== undefined ? options.async : DefaultOptions.async;
33+
const console = options.console || AlminLoggerDefaultOptions.console;
4034
const loggerOptions = {
4135
console
4236
};
43-
/**
44-
* @type {boolean} if current is async mode, return true
45-
*/
46-
this.isAsyncMode = isAsyncMode;
4737
// default: Async logger
48-
this.logger = isAsyncMode ? new AsyncLogger(loggerOptions) : new SyncLogger(loggerOptions);
38+
this.logger = new AsyncLogger(loggerOptions);
4939
this.logger.on(AlminLogger.Events.output, () => {
5040
this.emit(AlminLogger.Events.output);
5141
});
@@ -63,8 +53,8 @@ export default class AlminLogger extends EventEmitter {
6353
this.logger.startLogging(context);
6454
}
6555

66-
stopLogging(_context: Context<any>) {
67-
this.logger.release();
56+
stopLogging() {
57+
this.logger.stopLogging();
6858
}
6959

7060
/**
@@ -82,3 +72,6 @@ export default class AlminLogger extends EventEmitter {
8272
this.logger.release();
8373
}
8474
}
75+
76+
// also export as default
77+
export default AlminLogger;

packages/almin-logger/src/AsyncLogger.ts

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -53,10 +53,10 @@ const tryGetState = (store: StoreLike) => {
5353
export default class AsyncLogger extends EventEmitter {
5454
private useCaseLogGroupMap: MapLike<UseCaseLike, LogGroup>;
5555
private logger: any;
56-
printLogger: any;
57-
_currentLogBuffer: LogGroup[];
58-
_releaseHandlers: Array<() => void>;
59-
_transactionMap: MapLike<string, LogGroup>;
56+
private printLogger: PrintLogger;
57+
private currentLogBuffer: LogGroup[];
58+
private _releaseHandlers: Array<() => void>;
59+
private _transactionMap: MapLike<string, LogGroup>;
6060

6161
/**
6262
* @param {Object} console
@@ -68,7 +68,7 @@ export default class AsyncLogger extends EventEmitter {
6868
* @type {MapLike[]}
6969
* @private
7070
*/
71-
this._currentLogBuffer = [];
71+
this.currentLogBuffer = [];
7272
/**
7373
* @type {MapLike}
7474
*/
@@ -109,9 +109,9 @@ export default class AsyncLogger extends EventEmitter {
109109
* @param {LogGroup} logGroup
110110
*/
111111
const outputLogging = (logGroup: LogGroup) => {
112-
const index = this._currentLogBuffer.indexOf(logGroup);
112+
const index = this.currentLogBuffer.indexOf(logGroup);
113113
if (index !== -1) {
114-
this._currentLogBuffer.splice(index, 1);
114+
this.currentLogBuffer.splice(index, 1);
115115
this.printLogger.printLogGroup(logGroup);
116116
this.emit(AlminLogger.Events.output, logGroup);
117117
}
@@ -127,7 +127,7 @@ export default class AsyncLogger extends EventEmitter {
127127
const logGroup = new LogGroup({ title: meta.transaction.name, isTransaction: true });
128128
this._transactionMap.set(meta.transaction.id, logGroup);
129129
// the logGroup is root
130-
this._currentLogBuffer.push(logGroup);
130+
this.currentLogBuffer.push(logGroup);
131131
};
132132

133133
/**
@@ -182,7 +182,7 @@ export default class AsyncLogger extends EventEmitter {
182182
transactionLogGroup.addGroup(logGroup);
183183
} else if (!parentUseCase) {
184184
// if logGroup is of root
185-
this._currentLogBuffer.push(logGroup);
185+
this.currentLogBuffer.push(logGroup);
186186
}
187187
};
188188
/**
@@ -366,6 +366,10 @@ export default class AsyncLogger extends EventEmitter {
366366
];
367367
}
368368

369+
stopLogging() {
370+
this.release();
371+
}
372+
369373
/**
370374
* add log to logger
371375
* @param {*} log
@@ -390,15 +394,17 @@ export default class AsyncLogger extends EventEmitter {
390394
* flush current log buffer
391395
*/
392396
flushBuffer() {
397+
this.currentLogBuffer.length = 0;
393398
this.useCaseLogGroupMap.clear();
399+
this._transactionMap.clear();
394400
}
395401

396402
/**
397403
* release event handlers
398404
*/
399405
release() {
406+
this.flushBuffer();
400407
this._releaseHandlers.forEach(releaseHandler => releaseHandler());
401408
this._releaseHandlers.length = 0;
402-
this.useCaseLogGroupMap.clear();
403409
}
404410
}

packages/almin-logger/src/SyncLogger.ts

Lines changed: 0 additions & 133 deletions
This file was deleted.

packages/almin-logger/test/AlminLogger-test.js

Lines changed: 0 additions & 10 deletions
This file was deleted.
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import * as assert from "assert";
2+
import { AlminLogger, default as DefaultLogger } from "./../src/AlminLogger";
3+
describe("export", () => {
4+
it("default should be AlminLogger", () => {
5+
assert.strictEqual(DefaultLogger, AlminLogger);
6+
});
7+
it("export { AlminLogger }", () => {
8+
assert.strictEqual(typeof AlminLogger, "function");
9+
});
10+
});

0 commit comments

Comments
 (0)