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

Port to TypeScript #71

Closed
wants to merge 15 commits into from
Closed

Port to TypeScript #71

wants to merge 15 commits into from

Conversation

tp
Copy link

@tp tp commented Jan 31, 2018

Hi @jonnyreeves,

since you'd would accept a TypeScript port, I have started one. At the moment it just compiles, but it's not final.

I try to keep the changes (for the user) as simple as possible, but it should probably be released as 2.0 if merged (at least the TypeScript using users will get some errors due to fixes – for JS users I strive for full compatibility).

For now I have 2 questions:

  1. You advertise including the script via https://cdn.rawgit.com/jonnyreeves/js-logger/master/src/logger.min.js
    I would like to use the src folder exclusively for the TypeScript sources, and put the transpiled files in lib (as is common convention). That would obviously break the above file. I hope no-one is using that in production anyway, but if you want to keep that stable we would need to re-arrange the source.

  2. Context type
    In

    level: ILogLevel;
    it is stated that the context's log level is stored in a level property.

But in the current implementation I can find references to filterLevel (

this.setLevel(defaultContext.filterLevel);
) as well as level (
if (context.level === Logger.TIME) {
and
logHandler(msgArgs, merge({ level: level }, this.context));
).

Would you have any objections to just always use filterLevel (which seems to be the externally used one at the moment)?

@tp
Copy link
Author

tp commented Jan 31, 2018

For 2) I would propose the following: Removing filterLevel and just use level in context/IContext, as that is what's currently tested:

assert.strictEqual(this.calls[0].context.level, logger.TRACE);

@tp
Copy link
Author

tp commented Jan 31, 2018

Wondering what's different between my local test (just opening the test index in the browser) and Travis's. 🤔

@jonnyreeves
Copy link
Owner

Thank you @tp, you are an open source hero! I am on mobile, but from looking at your diff I believe you need to modify the gulpfile to run tsc so that the tests execute against the generated JavaScript.

Gulp is probably quite passé/unneeded now so feel free to migrate to npm scripts if that suits you better.

Thanks again

@tp
Copy link
Author

tp commented Feb 1, 2018

Tests now pass on Travis 🙃

Still need to test/add the capability to run the library on Node.js, as I think that was possible before. (I have never written such an export like
https://github.com/jonnyreeves/js-logger/blob/34f8dc5e5ea2208479181b8d8871e1422c117ba1/src/logger.js#L260:L276

But I think that should be possible.

The only other remaining issue is the gulp git plugin, which crashes on require for me locally as well as on Travis. But as you suggested we can probably move away from it altogether. yarn publish does a fine job of tagging in Git.

Copy link
Owner

@jonnyreeves jonnyreeves left a comment

Choose a reason for hiding this comment

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

Couple of nits, otherwise awesome work.

// Inner class which performs the bulk of the work; ContextualLogger instances can be configured independently
// of each other.
export class ContextualLogger implements ILogger {
public readonly context: IContext;
Copy link
Owner

Choose a reason for hiding this comment

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

Any reason for making the context public?

Copy link
Author

Choose a reason for hiding this comment

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

// Changes the current logging level for the logging instance.
public setLevel(newLevel: ILogLevel) {
// Ensure the supplied Level object looks valid.
if (newLevel && "value" in newLevel) {
Copy link
Owner

Choose a reason for hiding this comment

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

Not sure we need this runtime check now that we are using types.

Copy link
Author

Choose a reason for hiding this comment

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

Yep, that would be obsolete by now :) Neither null / undefined nor a non-matching interface could be passed here from strict TypeScript code.

src/logger.ts Outdated
export const Logger: JSLoggerExportType = { ...boundGlobalLoggerFunctions, ...JSLoggerDefaults };

// For those that are at home that are keeping score.
// Logger.VERSION = "1.4.1";
Copy link
Owner

Choose a reason for hiding this comment

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

We should continue to export the version here (or remove this an make a breaking change in 2.0)

Copy link
Author

Choose a reason for hiding this comment

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

Yeah, I think we can definitely keep the version. Just need to add it to the type.

src/datatypes.ts Outdated
}

declare var Logger: ILogger;
export = Logger;
export type JSLoggerExportType = IJSLoggerDefaultsType & ILogger;
Copy link
Owner

Choose a reason for hiding this comment

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

Not a fan of this class name, why not just JSLogger?

Copy link
Author

Choose a reason for hiding this comment

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

👍 library name makes sense here, I agree

enabledFor(level: ILogLevel): boolean;
}

export interface IJSLoggerDefaultsType {
Copy link
Owner

Choose a reason for hiding this comment

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

I think this would be a good oppertunity to make the LogLevel's a publicly exported enum, we can do this in a subsequent refactor if you want to reduce the amount of change in this PR.

Copy link
Author

Choose a reason for hiding this comment

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

Yeah, using TypeScript enums would be awesome :)

I'll see if we can utilize them in a way that would still keep them on the IJSLoggerDefaultsType, such that the tests pass without any modifications.

Copy link
Author

Choose a reason for hiding this comment

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

@jonnyreeves I think that would work. Would you be fine with a string based enum to preserve the human readability when it puts up during debugging? Put follow-up PR might be best, I hope no-one is using the internals of the log level interface.

tsconfig.json Outdated
"strict": true,
"removeComments": false,
"preserveConstEnums": true,
// "outFile": "./lib/logger.js",
Copy link
Owner

Choose a reason for hiding this comment

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

remove this line.

@jonnyreeves
Copy link
Owner

The only other remaining issue is the gulp git plugin, which crashes on require for me locally as well as on Travis. But as you suggested we can probably move away from it altogether. yarn publish does a fine job of tagging in Git.

I've not used yarn, but I have no strong feelings on this one - happy to do whatever works :)

@tp
Copy link
Author

tp commented Feb 2, 2018

@jonnyreeves Thanks for your input! I'll clean this up and finalize the PR.

@tp
Copy link
Author

tp commented Feb 2, 2018

@jonnyreeves I have implemented changes based on your comments and also added a new TypeScript test project to ensure that the library can actually be used in TypeScript as we intent it to :)

@tp
Copy link
Author

tp commented Feb 2, 2018

The only remaining issue I see would be direct links to the minified library version as suggested in the README: <script src="https://cdn.rawgit.com/jonnyreeves/js-logger/master/src/logger.min.js"></script>.

Can we break these? Not sure what Github's policy is on those, but if it's generally accepted we should probably keep them for the foreseeable future.

In that case, do we keep the 1.4.1 file there for maximum compatibility? Maybe adding a note in the header, that a new version is available etc.

@jonnyreeves
Copy link
Owner

Can we break these?

Ehhh, I'm pretty torn on this; I'm not a fan of "breaking the web", but JS dev has come a long way since 2011 when I first authored this library and blindly acquiring JavaScript snippets in this fashion is a long, long way from "best practice" in 2018.

However, in the slim chance that someone is actually (ab)using this approach we shouldn't make their website/app stop working so let's do as you propose: publish 1.4.1 file in that location with a comment in the header explaining why it's there (and remove all mentions from the README)

@tp
Copy link
Author

tp commented Feb 9, 2018

@jonnyreeves Re-added the minified library file to the src folder.

@jonnyreeves
Copy link
Owner

Merged into https://github.com/jonnyreeves/js-logger/tree/2.0 - let's continue development in this branch.

@tp
Copy link
Author

tp commented Feb 12, 2018

Hi @jonnyreeves,

great that you want to do all the clean up in the branch! Good work on #75!

Is there anything left to do that I could help you with?

https://github.com/jonnyreeves/js-logger/milestone/1 currently shows no pending work.

The only remaining thing I have thought about is running the tests on BrowserStack/some other Selenium Grid, but I don't know what the scope of that would be.

Best,
Timm

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants