-
Notifications
You must be signed in to change notification settings - Fork 9
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
Comments
Hey, I wouldn't mind having |
@alexfedoseev Cool. I'll see if I'll find a way (and time) to make it happen. |
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)
};
}; |
Looking good. Couple of suggestions.
[@bs.val] external window: 'a = "window";
window->Js.typeof == "undefined" |
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:
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.
The text was updated successfully, but these errors were encountered: