Changing the way users can enable logging

This commit is contained in:
Pawel Kadluczka 2017-08-29 16:28:44 -07:00 committed by Pawel Kadluczka
parent b8d2d24b67
commit 7db480b23e
9 changed files with 89 additions and 32 deletions

View File

@ -16,7 +16,8 @@ describe("Connection", () => {
get(url: string): Promise<string> { get(url: string): Promise<string> {
return Promise.resolve(""); return Promise.resolve("");
} }
} },
logging: null
} as IHttpConnectionOptions; } as IHttpConnectionOptions;
let connection = new HttpConnection("http://tempuri.org", options); let connection = new HttpConnection("http://tempuri.org", options);
@ -51,7 +52,8 @@ describe("Connection", () => {
get(url: string): Promise<string> { get(url: string): Promise<string> {
return Promise.resolve(""); return Promise.resolve("");
} }
} },
logging: null
} as IHttpConnectionOptions; } as IHttpConnectionOptions;
let connection = new HttpConnection("http://tempuri.org", options); let connection = new HttpConnection("http://tempuri.org", options);
@ -74,7 +76,8 @@ describe("Connection", () => {
get(url: string): Promise<string> { get(url: string): Promise<string> {
return Promise.resolve(""); return Promise.resolve("");
} }
} },
logging: null
} as IHttpConnectionOptions; } as IHttpConnectionOptions;
let connection = new HttpConnection("http://tempuri.org", options); let connection = new HttpConnection("http://tempuri.org", options);
@ -109,7 +112,8 @@ describe("Connection", () => {
connection.stop(); connection.stop();
return Promise.resolve(""); return Promise.resolve("");
} }
} },
logging: null
} as IHttpConnectionOptions; } as IHttpConnectionOptions;
let connection = new HttpConnection("http://tempuri.org", options); let connection = new HttpConnection("http://tempuri.org", options);
@ -154,7 +158,8 @@ describe("Connection", () => {
return Promise.resolve(""); return Promise.resolve("");
} }
}, },
transport: fakeTransport transport: fakeTransport,
logging: null
} as IHttpConnectionOptions; } as IHttpConnectionOptions;
@ -183,7 +188,8 @@ describe("Connection", () => {
return Promise.resolve(""); return Promise.resolve("");
} }
}, },
transport: requestedTransport transport: requestedTransport,
logging: null
} as IHttpConnectionOptions; } as IHttpConnectionOptions;
let connection = new HttpConnection("http://tempuri.org", options); let connection = new HttpConnection("http://tempuri.org", options);
@ -208,7 +214,8 @@ describe("Connection", () => {
get(url: string): Promise<string> { get(url: string): Promise<string> {
return Promise.resolve(""); return Promise.resolve("");
} }
} },
logging: null
} as IHttpConnectionOptions; } as IHttpConnectionOptions;
let connection = new HttpConnection("http://tempuri.org", options); let connection = new HttpConnection("http://tempuri.org", options);
@ -249,7 +256,8 @@ describe("Connection", () => {
return Promise.resolve(""); return Promise.resolve("");
} }
}, },
transport: fakeTransport transport: fakeTransport,
logging: null
} as IHttpConnectionOptions; } as IHttpConnectionOptions;
let connection = new HttpConnection("https://tempuri.org", options); let connection = new HttpConnection("https://tempuri.org", options);

View File

@ -0,0 +1,24 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
import { LoggerFactory } from "../Microsoft.AspNetCore.SignalR.Client.TS/Loggers"
import { ILogger, LogLevel } from "../Microsoft.AspNetCore.SignalR.Client.TS/ILogger"
describe("LoggerFactory", () => {
it("creates ConsoleLogger when no logging specified", () => {
expect(LoggerFactory.createLogger().constructor.name).toBe("ConsoleLogger");
});
it("creates NullLogger when logging is set to null", () => {
expect(LoggerFactory.createLogger(null).constructor.name).toBe("NullLogger");
});
it("creates ConsoleLogger when log level specified", () => {
expect(LoggerFactory.createLogger(LogLevel.Information).constructor.name).toBe("ConsoleLogger");
});
it("does not create its own logger if the user provides one", () => {
let customLogger : ILogger = { log: LogLevel => {} };
expect(LoggerFactory.createLogger(customLogger)).toBe(customLogger);
});
});

View File

@ -4,7 +4,7 @@ import { ITransport, TransferMode, TransportType, WebSocketTransport, ServerSent
import { IHttpClient, HttpClient } from "./HttpClient" import { IHttpClient, HttpClient } from "./HttpClient"
import { IHttpConnectionOptions } from "./IHttpConnectionOptions" import { IHttpConnectionOptions } from "./IHttpConnectionOptions"
import { ILogger, LogLevel } from "./ILogger" import { ILogger, LogLevel } from "./ILogger"
import { NullLogger } from "./Loggers" import { LoggerFactory } from "./Loggers"
const enum ConnectionState { const enum ConnectionState {
Initial, Initial,
@ -32,8 +32,9 @@ export class HttpConnection implements IConnection {
constructor(url: string, options: IHttpConnectionOptions = {}) { constructor(url: string, options: IHttpConnectionOptions = {}) {
this.url = url; this.url = url;
options = options || {};
this.httpClient = options.httpClient || new HttpClient(); this.httpClient = options.httpClient || new HttpClient();
this.logger = options.logger || new NullLogger(); this.logger = LoggerFactory.createLogger(options.logging);
this.connectionState = ConnectionState.Initial; this.connectionState = ConnectionState.Initial;
this.options = options; this.options = options;
} }

View File

@ -7,13 +7,14 @@ import { JsonHubProtocol } from "./JsonHubProtocol";
import { TextMessageFormat } from "./Formatters" import { TextMessageFormat } from "./Formatters"
import { Base64EncodedHubProtocol } from "./Base64EncodedHubProtocol" import { Base64EncodedHubProtocol } from "./Base64EncodedHubProtocol"
import { ILogger, LogLevel } from "./ILogger" import { ILogger, LogLevel } from "./ILogger"
import { NullLogger } from "./Loggers" import { ConsoleLogger, NullLogger, LoggerFactory } from "./Loggers"
import { IHubConnectionOptions } from "./IHubConnectionOptions"
export { TransportType } from "./Transports" export { TransportType } from "./Transports"
export { HttpConnection } from "./HttpConnection" export { HttpConnection } from "./HttpConnection"
export { JsonHubProtocol } from "./JsonHubProtocol" export { JsonHubProtocol } from "./JsonHubProtocol"
export { LogLevel } from "./ILogger" export { LogLevel, ILogger } from "./ILogger"
export { ConsoleLogger } from "./Loggers" export { ConsoleLogger, NullLogger } from "./Loggers"
export class HubConnection { export class HubConnection {
private readonly connection: IConnection; private readonly connection: IConnection;
@ -24,10 +25,12 @@ export class HubConnection {
private id: number; private id: number;
private connectionClosedCallback: ConnectionClosed; private connectionClosedCallback: ConnectionClosed;
constructor(connection: IConnection, logger: ILogger = new NullLogger(), protocol: IHubProtocol = new JsonHubProtocol()) { constructor(connection: IConnection, options: IHubConnectionOptions = {}) {
this.connection = connection; this.connection = connection;
this.logger = logger || new NullLogger(); options = options || {};
this.protocol = protocol || new JsonHubProtocol(); this.logger = LoggerFactory.createLogger(options.logging);
this.protocol = options.protocol || new JsonHubProtocol();
this.connection.onDataReceived = data => { this.connection.onDataReceived = data => {
this.onDataReceived(data); this.onDataReceived(data);
}; };

View File

@ -1,9 +1,9 @@
import { IHttpClient } from "./HttpClient" import { IHttpClient } from "./HttpClient"
import { TransportType, ITransport } from "./Transports" import { TransportType, ITransport } from "./Transports"
import { ILogger } from "./ILogger"; import { ILogger, LogLevel } from "./ILogger";
export interface IHttpConnectionOptions { export interface IHttpConnectionOptions {
httpClient?: IHttpClient; httpClient?: IHttpClient;
transport?: TransportType | ITransport; transport?: TransportType | ITransport;
logger?: ILogger; logging?: ILogger | LogLevel;
} }

View File

@ -0,0 +1,10 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
import { IHubProtocol } from "./IHubProtocol"
import { ILogger, LogLevel } from "./ILogger"
export interface IHubConnectionOptions {
protocol?: IHubProtocol;
logging?: ILogger | LogLevel;
}

View File

@ -18,3 +18,21 @@ export class ConsoleLogger implements ILogger {
} }
} }
} }
export namespace LoggerFactory {
export function createLogger(logging?: ILogger | LogLevel) {
if (logging === undefined) {
return new ConsoleLogger(LogLevel.Information);
}
if (logging === null) {
return new NullLogger();
}
if ((<ILogger>logging).log) {
return <ILogger>logging;
}
return new ConsoleLogger(<LogLevel>logging);
}
}

View File

@ -31,7 +31,7 @@ describe('connection', function () {
var message = "Hello World!"; var message = "Hello World!";
var connection = new signalR.HttpConnection(ECHOENDPOINT_URL, { var connection = new signalR.HttpConnection(ECHOENDPOINT_URL, {
transport: transportType, transport: transportType,
logger: new signalR.ConsoleLogger(signalR.LogLevel.Information) logger: signalR.LogLevel.Information
}); });
var received = ""; var received = "";

View File

@ -8,8 +8,7 @@ describe('hubConnection', function () {
it('can invoke server method and receive result', function (done) { it('can invoke server method and receive result', function (done) {
var message = "你好,世界!"; var message = "你好,世界!";
var logger = new signalR.ConsoleLogger(signalR.LogLevel.Information); var hubConnection = new signalR.HubConnection(new signalR.HttpConnection(TESTHUBENDPOINT_URL, { transport: transportType }), { protocol: protocol});
var hubConnection = new signalR.HubConnection(new signalR.HttpConnection(TESTHUBENDPOINT_URL, { transport: transportType, logger: logger }), logger, protocol);
hubConnection.onClosed = function (error) { hubConnection.onClosed = function (error) {
expect(error).toBe(undefined); expect(error).toBe(undefined);
done(); done();
@ -30,8 +29,7 @@ describe('hubConnection', function () {
}); });
it('can stream server method and receive result', function (done) { it('can stream server method and receive result', function (done) {
var logger = new signalR.ConsoleLogger(signalR.LogLevel.Information); var hubConnection = new signalR.HubConnection(new signalR.HttpConnection(TESTHUBENDPOINT_URL, { transport: transportType }), { protocol: protocol});
var hubConnection = new signalR.HubConnection(new signalR.HttpConnection(TESTHUBENDPOINT_URL, { transport: transportType, logger: logger }), logger, protocol);
hubConnection.onClosed = function (error) { hubConnection.onClosed = function (error) {
expect(error).toBe(undefined); expect(error).toBe(undefined);
@ -61,9 +59,7 @@ describe('hubConnection', function () {
it('rethrows an exception from the server when invoking', function (done) { it('rethrows an exception from the server when invoking', function (done) {
var errorMessage = "An error occurred."; var errorMessage = "An error occurred.";
var hubConnection = new signalR.HubConnection(new signalR.HttpConnection(TESTHUBENDPOINT_URL, { transport: transportType }), { protocol: protocol});
var logger = new signalR.ConsoleLogger(signalR.LogLevel.Information);
var hubConnection = new signalR.HubConnection(new signalR.HttpConnection(TESTHUBENDPOINT_URL, { transport: transportType, logger: logger }), logger, protocol);
hubConnection.start().then(function () { hubConnection.start().then(function () {
hubConnection.invoke('ThrowException', errorMessage).then(function () { hubConnection.invoke('ThrowException', errorMessage).then(function () {
@ -85,8 +81,7 @@ describe('hubConnection', function () {
it('rethrows an exception from the server when streaming', function (done) { it('rethrows an exception from the server when streaming', function (done) {
var errorMessage = "An error occurred."; var errorMessage = "An error occurred.";
var logger = new signalR.ConsoleLogger(signalR.LogLevel.Information); var hubConnection = new signalR.HubConnection(new signalR.HttpConnection(TESTHUBENDPOINT_URL, { transport: transportType }), { protocol: protocol});
var hubConnection = new signalR.HubConnection(new signalR.HttpConnection(TESTHUBENDPOINT_URL, { transport: transportType, logger: logger }), logger, protocol);
hubConnection.start().then(function () { hubConnection.start().then(function () {
hubConnection.stream('ThrowException', errorMessage).subscribe({ hubConnection.stream('ThrowException', errorMessage).subscribe({
@ -108,8 +103,7 @@ describe('hubConnection', function () {
}); });
it('can receive server calls', function (done) { it('can receive server calls', function (done) {
var logger = new signalR.ConsoleLogger(signalR.LogLevel.Information); var hubConnection = new signalR.HubConnection(new signalR.HttpConnection(TESTHUBENDPOINT_URL, { transport: transportType }), { protocol: protocol});
var hubConnection = new signalR.HubConnection(new signalR.HttpConnection(TESTHUBENDPOINT_URL, { transport: transportType, logger: logger }), logger, protocol);
var message = "你好 SignalR"; var message = "你好 SignalR";
@ -137,8 +131,7 @@ describe('hubConnection', function () {
ServerSentEvents: "Error occurred" ServerSentEvents: "Error occurred"
}; };
var logger = new signalR.ConsoleLogger(signalR.LogLevel.Information); var hubConnection = new signalR.HubConnection(new signalR.HttpConnection('http://' + document.location.host + '/uncreatable', { transport: transportType }), { protocol: protocol});
var hubConnection = new signalR.HubConnection(new signalR.HttpConnection('http://' + document.location.host + '/uncreatable', { transport: transportType, logger: logger }), logger, protocol);
hubConnection.onClosed = function (error) { hubConnection.onClosed = function (error) {
expect(error.message).toMatch(errorRegex[signalR.TransportType[transportType]]); expect(error.message).toMatch(errorRegex[signalR.TransportType[transportType]]);