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

Universal Logger Support? #14

Closed
TomiS opened this issue Feb 12, 2020 · 4 comments · Fixed by #32
Closed

Universal Logger Support? #14

TomiS opened this issue Feb 12, 2020 · 4 comments · Fixed by #32

Comments

@TomiS
Copy link

TomiS commented Feb 12, 2020

Hi. Really cool library. I was wondering if it would be possible to support detecting whether logger is called from browser or from Node and offer a unified API to be used in code that is ran in both server and client.

Then there could be an API like this:

Logger.error(__MODULE__, "My Error Message");

A quick googling gave me this:
https://stackoverflow.com/questions/4224606/how-to-check-whether-a-script-is-running-under-node-js
Although possibly just checking if window exists might be enough.

@alex35mil
Copy link
Member

Hey, I wouldn't mind having UniversalLogger.re that leverages current loggers but I'm not sure about details of environment detection and it'd be far low in my priorities list in the near future to make it happen. I would happily accept PR though!

@TomiS
Copy link
Author

TomiS commented Feb 13, 2020

@alexfedoseev Cool. I'll see if I'll find a way (and time) to make it happen.

@TomiS
Copy link
Author

TomiS commented Feb 26, 2020

Ok, not yet a PR, but I made one kind of working PoC inside my app. It looks like this:

// Let's define a variant because it's pretty
type platform =
  | Browser
  | Node;

// Utilizes 'ugly' raw javascript tester function
// that simply checks if window is available
let getPlatform = () => {
  let windowExists: unit => bool = [%raw
    {|
  function() {
    if (typeof window === 'undefined') { return false; }
    else return true;
  }
|}
  ];
  if (windowExists()) {
    Browser;
  } else {
    Node;
  };
};

// An universal wrapper module
module Log = {
  let error = (loc: string, msg: string) =>
    switch (getPlatform()) {
    | Browser => BrowserLogger.error(loc, msg)
    | Node => NodeLogger.error(loc, msg)
    };

  let info = (loc: string, msg: string) =>
    switch (getPlatform()) {
    | Browser => BrowserLogger.info(loc, msg)
    | Node => NodeLogger.info(loc, msg)
    };

  let warn = (loc: string, msg: string) =>
    switch (getPlatform()) {
    | Browser => BrowserLogger.warn(loc, msg)
    | Node => NodeLogger.warn(loc, msg)
    };
};

@alex35mil
Copy link
Member

Looking good. Couple of suggestions.

  1. It makes sense to memoize platform value.
  2. You can do this to get rid of raw:
[@bs.val] external window: 'a = "window";
window->Js.typeof == "undefined"

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 a pull request may close this issue.

2 participants