Created
June 23, 2018 11:39
-
-
Save Kobe/f356f005e8c705a32470c936a97d7463 to your computer and use it in GitHub Desktop.
logging via Ajax endpoint
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Location from './location.es6'; | |
import { isJQueryAvailable } from './lib.es6'; | |
export const LOG_LEVEL_DEBUG = 'DEBUG'; // eslint-disable-line no-unused-vars | |
export const LOG_LEVEL_INFO = 'INFO'; // eslint-disable-line no-unused-vars | |
export const LOG_LEVEL_WARN = 'WARN'; | |
export const LOG_LEVEL_ERROR = 'ERROR'; // eslint-disable-line no-unused-vars | |
export const LOG_LEVEL_OFF = 'OFF'; // eslint-disable-line no-unused-vars | |
const INTERNAL_LOGGING_URL = '/clientLogger'; | |
export class Logger { | |
/** | |
* @param {string} message | |
* @param {string} logger | |
* @param {string} level | |
* @param {string[]} history | |
* @return {Promise} | |
*/ | |
static log(message, logger = 'ClientLogger', level = LOG_LEVEL_WARN, history = []) { | |
let browserHistory = history; | |
if (history.length === 0) { | |
const currentLocation = Location.get(window); | |
browserHistory = [currentLocation]; | |
} | |
return new Promise((resolve) => { | |
if (isJQueryAvailable) { | |
$.ajax({ | |
method: 'POST', | |
url: INTERNAL_LOGGING_URL, | |
data: JSON.stringify({ | |
message, logger, level, browserHistory | |
}), | |
contentType: 'application/json' | |
}) | |
.always(() => { | |
resolve(); | |
}); | |
} else { | |
fetch(INTERNAL_LOGGING_URL, { | |
method: 'POST', | |
url: INTERNAL_LOGGING_URL, | |
body: JSON.stringify({ | |
message, logger, level, browserHistory | |
}), | |
headers: new Headers({ | |
'Content-Type': 'application/json' | |
}) | |
}) | |
.then(() => { | |
resolve(); | |
}); | |
} | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment