Skip to content

Fix #8974 by supporting strings in configureLogging for SignalR JS client #9252

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

Merged
merged 8 commits into from
Apr 17, 2019

Conversation

analogrelay
Copy link
Contributor

Fixes #8974

This allows users to pass in a string for the log level when calling configureLogging. This makes it much easier to configure logging in environments where you don't have access to the enum definition (such as in Blazor when you're just doing this in a script block in your page).

Note: I also grouped all the configureLogging tests into their own describe because there were now quite a few :).

Test output for HubConnectionBuilder.test.ts:

 PASS  signalr/tests/HubConnectionBuilder.test.ts (6.62s)
  HubConnectionBuilder
    √ withUrl throws if url is null (3ms)
    √ withHubProtocol throws if protocol is null
    √ withUrl throws if url is undefined
    √ withHubProtocol throws if protocol is undefined (1ms)
    √ builds HubConnection with HttpConnection using provided URL (12ms)
    √ can configure transport type
    √ can configure hub protocol (2ms)
    √ reconnectPolicy undefined by default (1ms)
    √ withAutomaticReconnect throws if reconnectPolicy is already set
    √ withAutomaticReconnect uses default retryDelays when called with no arguments (1ms)
    √ withAutomaticReconnect uses custom retryDelays when provided
    √ withAutomaticReconnect uses a custom IReconnectPolicy when provided
    configureLogging
      √ throws if logger is null (1ms)
      √ throws if logger is undefined
      √ accepts LogLevel.None
      √ accepts LogLevel.Critical
      √ accepts LogLevel.Error
      √ accepts LogLevel.Warning (1ms)
      √ accepts LogLevel.Information
      √ accepts LogLevel.Debug
      √ accepts LogLevel.Trace (1ms)
      √ accepts "critical" as an alias for LogLevel.Critical
      √ accepts "debug" as an alias for LogLevel.Debug (1ms)
      √ accepts "error" as an alias for LogLevel.Error
      √ accepts "info" as an alias for LogLevel.Information
      √ accepts "information" as an alias for LogLevel.Information (1ms)
      √ accepts "none" as an alias for LogLevel.None
      √ accepts "trace" as an alias for LogLevel.Trace
      √ accepts "warn" as an alias for LogLevel.Warning
      √ accepts "warning" as an alias for LogLevel.Warning (1ms)
      √ allows logger to be replaced (1ms)
      √ configures logger for both HttpConnection and HubConnection (2ms)
      √ does not replace HttpConnectionOptions logger if provided (1ms)

cc @rynowak

@analogrelay analogrelay added this to the 3.0.0-preview5 milestone Apr 10, 2019
@BrennanConroy BrennanConroy added the area-signalr Includes: SignalR clients and servers label Apr 10, 2019
@analogrelay
Copy link
Contributor Author

🆙 📅

@analogrelay
Copy link
Contributor Author

Update to fix build.

@@ -13,6 +13,31 @@ import { JsonHubProtocol } from "./JsonHubProtocol";
import { NullLogger } from "./Loggers";
import { Arg, ConsoleLogger } from "./Utils";

// tslint:disable:object-literal-sort-keys
const LogLevelNameMapping = {
Copy link
Member

@halter73 halter73 Apr 11, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this need to be exported now that it's included in the public configureLogging type signature?

It might be better to change configureLogging to take a keyof typeof LogLevel instead of keyof typeof LogLevelNameMapping. You can still use the LogLevelNameMapping and call toLowerCase on the argument.

As an upside, hat would be make things more consistent with the capitalization of our HubConnectionStates.

Copy link
Member

@halter73 halter73 Apr 11, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

keyof typeof LogLevel doesn't work since we want to allow "warn" and "info". I say don't export anything new, and change the configureLogging type signature to take a string.

We could link to the "Configure logging" section of docs from the configureLogging doc comment if the want to see what string are valid. If you want type safety, you can still use the Loglevel overload.

@analogrelay
Copy link
Contributor Author

Feedback applied!

@@ -146,27 +146,35 @@ export class SubjectSubscription<T> implements ISubscription<T> {
/** @private */
export class ConsoleLogger implements ILogger {
private readonly minimumLogLevel: LogLevel;
// Public for testing purposes.
Copy link
Member

@halter73 halter73 Apr 12, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This comment was indented with a tab.

YARN : error : F:/workspace/_work/1/s/src/SignalR/clients/ts/signalr/src/Utils.ts[149, 1]: space indentation expected [F:\workspace\_work\1\s\src\SignalR\clients\ts\signalr\signalr.npmproj]

double nit: Add newline above.

@@ -44,15 +69,17 @@ export class HubConnectionBuilder {

/** Configures custom logging for the {@link @aspnet/signalr.HubConnection}.
*
* @param {LogLevel | ILogger} logging An object implementing the {@link @aspnet/signalr.ILogger} interface or {@link @aspnet/signalr.LogLevel}.
* @param {LogLevel | string | ILogger} logging A {@link @aspnet/signalr.LogLevel}, a string representing a LogLevel, or an object implementing the {@link @aspnet/signalr.ILogger} interface. See {@link https://docs.microsoft.com/en-us/aspnet/core/signalr/configuration#configure-logging|the documentation for client logging configuration} for more details.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

YARN : error : F:/workspace/_work/1/s/src/SignalR/clients/ts/signalr/src/HubConnectionBuilder.ts[72, 1]: Exceeds maximum line length of 300 [F:\workspace\_work\1\s\src\SignalR\clients\ts\signalr\signalr.npmproj]

Copy link
Member

@halter73 halter73 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. Just some tslint cleanup left.

@analogrelay
Copy link
Contributor Author

analogrelay commented Apr 13, 2019

:'(. I'll look at that on Monday

if (typeof mapping !== "undefined") {
return mapping;
} else {
throw new Error(`Unknown log level: ${name}`);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: Do we want to preserve the casing on the user input here?

Copy link
Contributor Author

@analogrelay analogrelay Apr 15, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This does preserve that casing. I think that's the appropriate behavior here, since it's echoing what the user put in.

Copy link
Contributor

@mikaelm12 mikaelm12 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Test Failure. It looks like configureLogging is trying to define one of the string log level args as an ILogger which causes the test to fail.

  FAIL signalr/tests/HubConnectionBuilder.test.ts
    ΓùÅ Test suite failed to run
  
      signalr\tests\HubConnectionBuilder.test.ts (203,39): Argument of type '"trace" | "debug" | "info" | "information" | "warn" | "warning" | "error" | "critical" | "none"' is not assignable to parameter of type 'ILogger'.
        Type '"trace"' is not assignable to type 'ILogger'.
  
        at Object.runTsDiagnostics (common/node_modules/ts-jest/src/utils.ts:227:11)
        at process (common/node_modules/ts-jest/src/preprocessor.ts:47:5)
        at Object.process (common/node_modules/ts-jest/index.js:8:51)
        at ScriptTransformer.transformSource (common/node_modules/@jest/transform/build/ScriptTransformer.js:387:35)
        at ScriptTransformer._transformAndBuildScript (common/node_modules/@jest/transform/build/ScriptTransformer.js:457:40)
        at ScriptTransformer.transform (common/node_modules/@jest/transform/build/ScriptTransformer.js:513:25)

const mappedName = LogLevel[mapped];
it(`accepts "${str}" as an alias for LogLevel.${mappedName}`, async () => {
const builder = new HubConnectionBuilder()
.configureLogging(str);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's trying to assign str to an ILogger instead of a string here

@analogrelay
Copy link
Contributor Author

Blocked on @mikaelm12 's change request ;).

@BrennanConroy
Copy link
Member

Unblocked now :D

@analogrelay
Copy link
Contributor Author

hat tip

@analogrelay analogrelay merged commit 46db367 into master Apr 17, 2019
@BrennanConroy BrennanConroy deleted the anurse/8974-string-log-levels branch April 17, 2019 15:18
@mikaelm12
Copy link
Contributor

This is why I don't like using the request changes feature lol

@analogrelay
Copy link
Contributor Author

I only use it when I specifically feel the need to see the result of my feedback before signing off.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-signalr Includes: SignalR clients and servers
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Make it possible to set SignalR log level in javascript with strings
4 participants