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

feat: replace newNodeLogContext with NodeConsoleLogger #14

Merged
merged 2 commits into from
Jan 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@rocicorp/logger",
"description": "Logging utilities",
"version": "2.2.0",
"version": "3.0.0",
"repository": "github:rocicorp/logger",
"license": "Apache-2.0",
"engines": {
Expand Down
40 changes: 20 additions & 20 deletions src/logger.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {
FormatLogger,
LogContext,
LogSink,
newNodeLogContext,
NodeConsoleLogger,
TeeLogSink,
type LogLevel,
} from './logger.js';
Expand Down Expand Up @@ -100,14 +100,14 @@ test('FormatLogger', () => {
}
});

test('nodeLogContext', () => {
test('NodeConsoleLogger', () => {
const mockDebug = mockConsoleMethod('debug');
const mockInfo = mockConsoleMethod('info');
const mockError = mockConsoleMethod('error');

{
sinon.reset();
const l = newNodeLogContext('debug');
const l = new NodeConsoleLogger('debug');
expect(l.debug).to.be.instanceOf(Function);
expect(l.info).to.be.instanceOf(Function);
expect(l.error).to.be.instanceOf(Function);
Expand Down Expand Up @@ -188,42 +188,42 @@ class TestLogSinkWithFlush extends TestLogSink {
}
}

test("TeeLogSink", () => {
test('TeeLogSink', () => {
const l1 = new TestLogSink();
const l2 = new TestLogSink();
const tl = new TeeLogSink([l1, l2]);

expect(l1.messages).to.deep.equal([]);
expect(l2.messages).to.deep.equal([]);

tl.log("info", 1, 2);
expect(l1.messages).to.deep.equal([["info", 1, 2]]);
expect(l2.messages).to.deep.equal([["info", 1, 2]]);
tl.log('info', 1, 2);
expect(l1.messages).to.deep.equal([['info', 1, 2]]);
expect(l2.messages).to.deep.equal([['info', 1, 2]]);

tl.log("debug", 3);
tl.log('debug', 3);
expect(l1.messages).to.deep.equal([
["info", 1, 2],
["debug", 3],
['info', 1, 2],
['debug', 3],
]);
expect(l2.messages).to.deep.equal([
["info", 1, 2],
["debug", 3],
['info', 1, 2],
['debug', 3],
]);

tl.log("error", 4, 5, 6);
tl.log('error', 4, 5, 6);
expect(l1.messages).to.deep.equal([
["info", 1, 2],
["debug", 3],
["error", 4, 5, 6],
['info', 1, 2],
['debug', 3],
['error', 4, 5, 6],
]);
expect(l2.messages).to.deep.equal([
["info", 1, 2],
["debug", 3],
["error", 4, 5, 6],
['info', 1, 2],
['debug', 3],
['error', 4, 5, 6],
]);
});

test("tee logger flush", async () => {
test('tee logger flush', async () => {
const l1 = new TestLogSinkWithFlush();
const l2 = new TestLogSink();
const l3 = new TestLogSinkWithFlush();
Expand Down
22 changes: 12 additions & 10 deletions src/logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ export class TeeLogSink implements LogSink {
}

async flush(): Promise<void> {
await Promise.all(this._sinks.map((logger) => logger.flush?.()));
await Promise.all(this._sinks.map(logger => logger.flush?.()));
}
}

Expand Down Expand Up @@ -104,16 +104,18 @@ export class FormatLogger implements LogSink {
}

/**
* Instantiates a new console LogContext logger appropriate for the node
* environment.
* Create a logger that will log to the console with a prefix,
* appropriate for the node environment or terminal output.
*/
export function newNodeLogContext(level: LogLevel): LogContext {
const fl = new FormatLogger(
(lvl: LogLevel, ...args: unknown[]): unknown[] => {
return [logLevelPrefix[lvl], ...args];
},
);
return new LogContext(level, fl);
export class NodeConsoleLogger extends OptionalLoggerImpl {
constructor(level: LogLevel) {
const fl = new FormatLogger(
(lvl: LogLevel, ...args: unknown[]): unknown[] => {
return [logLevelPrefix[lvl], ...args];
},
);
super(fl, level);
}
}

/**
Expand Down