From 7e383b159f750b5bece6e6eafe423448af205096 Mon Sep 17 00:00:00 2001 From: kutuluk Date: Fri, 2 Jun 2017 01:00:44 +0400 Subject: [PATCH] init realese --- .eslintrc.json | 14 + .gitignore | 2 + LICENSE | 2 +- dist/loglevel-remote.js | 207 +++ dist/loglevel-remote.min.js | 1 + package-lock.json | 2584 +++++++++++++++++++++++++++++++++++ package.json | 48 + src/remote.js | 175 +++ 8 files changed, 3032 insertions(+), 1 deletion(-) create mode 100644 .eslintrc.json create mode 100644 .gitignore create mode 100644 dist/loglevel-remote.js create mode 100644 dist/loglevel-remote.min.js create mode 100644 package-lock.json create mode 100644 package.json create mode 100644 src/remote.js diff --git a/.eslintrc.json b/.eslintrc.json new file mode 100644 index 0000000..540f539 --- /dev/null +++ b/.eslintrc.json @@ -0,0 +1,14 @@ +{ + "extends": "airbnb-base", + "plugins": [ + "import" + ], + "env": { + "browser": true, + "es6": true + }, + "rules": { + "no-param-reassign": "off", + "default-case": "off" + } +} \ No newline at end of file diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..417c6ce --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +node_modules/ +.vscode/ \ No newline at end of file diff --git a/LICENSE b/LICENSE index 8864d4a..0d9f072 100644 --- a/LICENSE +++ b/LICENSE @@ -1,6 +1,6 @@ MIT License -Copyright (c) 2017 +Copyright (c) 2017 Eugene Pavlov Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal diff --git a/dist/loglevel-remote.js b/dist/loglevel-remote.js new file mode 100644 index 0000000..46d3cf7 --- /dev/null +++ b/dist/loglevel-remote.js @@ -0,0 +1,207 @@ +(function (global, factory) { + if (typeof define === "function" && define.amd) { + define(['module', 'exports'], factory); + } else if (typeof exports !== "undefined") { + factory(module, exports); + } else { + var mod = { + exports: {} + }; + factory(mod, mod.exports); + global.remote = mod.exports; + } +})(this, function (module, exports) { + 'use strict'; + + Object.defineProperty(exports, "__esModule", { + value: true + }); + + var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { + return typeof obj; + } : function (obj) { + return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; + }; + + var isAssigned = false; + var CIRCULAR_ERROR_MESSAGE = void 0; + + // https://github.com/nodejs/node/blob/master/lib/util.js + function tryStringify(arg) { + try { + return JSON.stringify(arg); + } catch (err) { + // Populate the circular error message lazily + if (!CIRCULAR_ERROR_MESSAGE) { + try { + var a = {}; + a.a = a; + JSON.stringify(a); + } catch (e) { + CIRCULAR_ERROR_MESSAGE = e.message; + } + } + if (err.name === 'TypeError' && err.message === CIRCULAR_ERROR_MESSAGE) return '[Circular]'; + throw err; + } + } + + function getClass(obj) { + return {}.toString.call(obj).slice(8, -1); + } + + var format = function format(argss) { + var args = [].concat(argss); + var result = ''; + + if (args.length > 1 && typeof args[0] === 'string') { + var template = args.shift(); + result = template.replace(/(%?)(%([sdo]))/g, function (match, escaped, ptn, flag) { + if (!escaped) { + var arg = args.shift(); + var a = ''; + switch (flag) { + case ('s', 'd'): + a = '' + arg; + break; + case 'o': + a = tryStringify(arg); + break; + } + return a; + } + return match; + }); + } + + // arguments remain after formatting + /* + if (args.length) { + result += ` ${args.join(' ')}`; + } + */ + + args.forEach(function (arg) { + if (result.length) result += ' '; + switch (typeof arg === 'undefined' ? 'undefined' : _typeof(arg)) { + case 'object': + { + result += getClass(arg); + result += tryStringify(arg); + break; + } + + default: + result += arg; + break; + } + }); + + // update escaped %% values + result = result.replace(/%{2,2}/g, '%'); + + return result; + }; + + var stackTrace = function stackTrace() { + try { + throw new Error(''); + } catch (e) { + return e.stack; + } + }; + + var hasStack = !!stackTrace(); + + var remote = function remote(logger, options) { + if (!logger || !logger.getLogger) { + throw new TypeError('Argument is not a root loglevel object'); + } + + if (isAssigned) { + throw new TypeError('You can assign a plugin only one time'); + } + + isAssigned = true; + + options = options || {}; + options.url = options.url || window.location.origin + '/logger'; + options.call = options.call || true; + options.timeout = options.timeout || 5000; + options.clear = options.clear || 1; + options.trace = options.trace || ['trace', 'warn', 'error']; + + var trace = {}; + for (var i = 0; i < options.trace.length; i += 1) { + var key = options.trace[i]; + trace[key] = true; + } + + var queue = []; + var isSending = false; + + var send = function send() { + if (!queue.length || isSending) { + return; + } + + isSending = true; + + var msg = queue.shift(); + + var xhr = new XMLHttpRequest(); + xhr.open('POST', options.url + '?r=' + Math.random(), true); + xhr.timeout = options.timeout; + xhr.setRequestHeader('Content-Type', 'text/plain'); + + xhr.onreadystatechange = function () { + if (xhr.readyState !== 4) { + return; + } + + if (xhr.status !== 200) { + queue.unshift(msg); + } + + isSending = false; + setTimeout(send, 0); + }; + + if (!msg.trace) { + xhr.send(format(msg.message)); + return; + } + + var lines = msg.trace.split('\n'); + lines.splice(0, options.clear + 2); + msg.message.push('\n' + lines.join('\n')); + + xhr.send(format(msg.message)); + msg.message.pop(); + }; + + var originalFactory = logger.methodFactory; + logger.methodFactory = function methodFactory(methodName, logLevel, loggerName) { + var rawMethod = originalFactory(methodName, logLevel, loggerName); + + return function () { + for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) { + args[_key] = arguments[_key]; + } + + var stack = hasStack && methodName in trace ? stackTrace() : undefined; + + queue.push({ level: methodName, message: args, trace: stack }); + send(); + + if (options.call) rawMethod.apply(undefined, args); + }; + }; + + logger.setLevel(logger.getLevel()); + return logger; + }; + + exports.default = remote; + module.exports = exports['default']; +}); diff --git a/dist/loglevel-remote.min.js b/dist/loglevel-remote.min.js new file mode 100644 index 0000000..333a39e --- /dev/null +++ b/dist/loglevel-remote.min.js @@ -0,0 +1 @@ +!function(e,t){if("function"==typeof define&&define.amd)define(["module","exports"],t);else if("undefined"!=typeof exports)t(module,exports);else{var r={exports:{}};t(r,r.exports),e.remote=r.exports}}(this,function(e,t){"use strict";function r(e){try{return JSON.stringify(e)}catch(e){if(!i)try{var t={};t.a=t,JSON.stringify(t)}catch(e){i=e.message}if("TypeError"===e.name&&e.message===i)return"[Circular]";throw e}}function o(e){return{}.toString.call(e).slice(8,-1)}Object.defineProperty(t,"__esModule",{value:!0});var n="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(e){return typeof e}:function(e){return e&&"function"==typeof Symbol&&e.constructor===Symbol&&e!==Symbol.prototype?"symbol":typeof e},a=!1,i=void 0,c=function(e){var t=[].concat(e),a="";if(t.length>1&&"string"==typeof t[0]){var i=t.shift();a=i.replace(/(%?)(%([sdo]))/g,function(e,o,n,a){if(!o){var i=t.shift(),c="";switch(a){case"d":c=""+i;break;case"o":c=r(i)}return c}return e})}return t.forEach(function(e){switch(a.length&&(a+=" "),void 0===e?"undefined":n(e)){case"object":a+=o(e),a+=r(e);break;default:a+=e}}),a=a.replace(/%{2,2}/g,"%")},s=function(){try{throw new Error("")}catch(e){return e.stack}},u=!!s(),remote=function(e,t){if(!e||!e.getLogger)throw new TypeError("Argument is not a root loglevel object");if(a)throw new TypeError("You can assign a plugin only one time");a=!0,(t=t||{}).url=t.url||window.location.origin+"/logger",t.call=t.call||!0,t.timeout=t.timeout||5e3,t.clear=t.clear||1,t.trace=t.trace||["trace","warn","error"];for(var r={},o=0;o 1 && typeof args[0] === 'string') { + const template = args.shift(); + result = template.replace(/(%?)(%([sdo]))/g, (match, escaped, ptn, flag) => { + if (!escaped) { + const arg = args.shift(); + let a = ''; + switch (flag) { + case ('s', 'd'): + a = `${arg}`; + break; + case 'o': + a = tryStringify(arg); + break; + } + return a; + } + return match; + }); + } + + // arguments remain after formatting + /* + if (args.length) { + result += ` ${args.join(' ')}`; + } + */ + + args.forEach((arg) => { + if (result.length) result += ' '; + switch (typeof arg) { + case 'object': { + result += getClass(arg); + result += tryStringify(arg); + break; + } + + default: + result += arg; + break; + } + }); + + // update escaped %% values + result = result.replace(/%{2,2}/g, '%'); + + return result; +}; + +const stackTrace = () => { + try { + throw new Error(''); + } catch (e) { + return e.stack; + } +}; + +const hasStack = !!stackTrace(); + +const remote = function remote(logger, options) { + if (!logger || !logger.getLogger) { + throw new TypeError('Argument is not a root loglevel object'); + } + + if (isAssigned) { + throw new TypeError('You can assign a plugin only one time'); + } + + isAssigned = true; + + options = options || {}; + options.url = options.url || `${window.location.origin}/logger`; + options.call = options.call || true; + options.timeout = options.timeout || 5000; + options.clear = options.clear || 1; + options.trace = options.trace || ['trace', 'warn', 'error']; + + const trace = {}; + for (let i = 0; i < options.trace.length; i += 1) { + const key = options.trace[i]; + trace[key] = true; + } + + const queue = []; + let isSending = false; + + const send = function send() { + if (!queue.length || isSending) { + return; + } + + isSending = true; + + const msg = queue.shift(); + + const xhr = new XMLHttpRequest(); + xhr.open('POST', `${options.url}?r=${Math.random()}`, true); + xhr.timeout = options.timeout; + xhr.setRequestHeader('Content-Type', 'text/plain'); + + xhr.onreadystatechange = () => { + if (xhr.readyState !== 4) { + return; + } + + if (xhr.status !== 200) { + queue.unshift(msg); + } + + isSending = false; + setTimeout(send, 0); + }; + + if (!msg.trace) { + xhr.send(format(msg.message)); + return; + } + + const lines = msg.trace.split('\n'); + lines.splice(0, options.clear + 2); + msg.message.push(`\n${lines.join('\n')}`); + + xhr.send(format(msg.message)); + msg.message.pop(); + }; + + const originalFactory = logger.methodFactory; + logger.methodFactory = function methodFactory(methodName, logLevel, loggerName) { + const rawMethod = originalFactory(methodName, logLevel, loggerName); + + return (...args) => { + const stack = hasStack && methodName in trace ? stackTrace() : undefined; + + queue.push({ level: methodName, message: args, trace: stack }); + send(); + + if (options.call) rawMethod(...args); + }; + }; + + logger.setLevel(logger.getLevel()); + return logger; +}; + +export default remote;