Logs UI errors to a service provided in the configuration

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;







up vote
1
down vote

favorite












As part of the project I am currently working on we want to log our UI component errors as well. I developed this package as a base.
It's a factory (basically, not sure if it's correctly a factory).
There is one function exposed configure that takes the parameters that each service (of the project) needs to supply and returns a logger; an object with four functions - debug, info, warn and error.



I am not sure if this is the right way to do it in javascript. Or in general. Is this how a factory should look? I have seen some that expose a function rather than an object that has a function on it but in my mind these two seem equivalent.



import uuid from 'uuid/v4';
import post from './post';

function requiredParam(param)
const requiredParamError = new Error(`Required parameter, "$param" is missing.`);
// preserve original stack trace
if (typeof Error.captureStackTrace === 'function')
Error.captureStackTrace(
requiredParamError,
requiredParam
);

throw requiredParamError;


const Logger =
configure: (
url = requiredParam('url'),
service = requiredParam('service'),
version = requiredParam('version'),
apigeeClientId = requiredParam('apigeeClientId')
) =>
const prepareForwardLogFunction = function prepareForwardLogFunction(logLevel)
return async function forwardLog(details, exception)
const payload =
...details,
service,
version,
loggername: 'UiLogger',
serviceType: 'reactjs',
level: logLevel,
eventType: logLevel === 'error' ? 'Exception' : 'Trace',
correlationId: uuid(),
timestamp: new Date(),
exception: exception !== undefined ? exception : null,
request: global.window.location.href
;

try
await post(url, payload, apigeeClientId);
catch (error)
console.warn('UI Logger failed:', url, payload, error ); // eslint-disable-line no-console

;
;

return
debug: prepareForwardLogFunction('debug'),
info: prepareForwardLogFunction('info'),
warn: prepareForwardLogFunction('warn'),
error: prepareForwardLogFunction('error')
;

;


export default Logger;






share|improve this question



























    up vote
    1
    down vote

    favorite












    As part of the project I am currently working on we want to log our UI component errors as well. I developed this package as a base.
    It's a factory (basically, not sure if it's correctly a factory).
    There is one function exposed configure that takes the parameters that each service (of the project) needs to supply and returns a logger; an object with four functions - debug, info, warn and error.



    I am not sure if this is the right way to do it in javascript. Or in general. Is this how a factory should look? I have seen some that expose a function rather than an object that has a function on it but in my mind these two seem equivalent.



    import uuid from 'uuid/v4';
    import post from './post';

    function requiredParam(param)
    const requiredParamError = new Error(`Required parameter, "$param" is missing.`);
    // preserve original stack trace
    if (typeof Error.captureStackTrace === 'function')
    Error.captureStackTrace(
    requiredParamError,
    requiredParam
    );

    throw requiredParamError;


    const Logger =
    configure: (
    url = requiredParam('url'),
    service = requiredParam('service'),
    version = requiredParam('version'),
    apigeeClientId = requiredParam('apigeeClientId')
    ) =>
    const prepareForwardLogFunction = function prepareForwardLogFunction(logLevel)
    return async function forwardLog(details, exception)
    const payload =
    ...details,
    service,
    version,
    loggername: 'UiLogger',
    serviceType: 'reactjs',
    level: logLevel,
    eventType: logLevel === 'error' ? 'Exception' : 'Trace',
    correlationId: uuid(),
    timestamp: new Date(),
    exception: exception !== undefined ? exception : null,
    request: global.window.location.href
    ;

    try
    await post(url, payload, apigeeClientId);
    catch (error)
    console.warn('UI Logger failed:', url, payload, error ); // eslint-disable-line no-console

    ;
    ;

    return
    debug: prepareForwardLogFunction('debug'),
    info: prepareForwardLogFunction('info'),
    warn: prepareForwardLogFunction('warn'),
    error: prepareForwardLogFunction('error')
    ;

    ;


    export default Logger;






    share|improve this question























      up vote
      1
      down vote

      favorite









      up vote
      1
      down vote

      favorite











      As part of the project I am currently working on we want to log our UI component errors as well. I developed this package as a base.
      It's a factory (basically, not sure if it's correctly a factory).
      There is one function exposed configure that takes the parameters that each service (of the project) needs to supply and returns a logger; an object with four functions - debug, info, warn and error.



      I am not sure if this is the right way to do it in javascript. Or in general. Is this how a factory should look? I have seen some that expose a function rather than an object that has a function on it but in my mind these two seem equivalent.



      import uuid from 'uuid/v4';
      import post from './post';

      function requiredParam(param)
      const requiredParamError = new Error(`Required parameter, "$param" is missing.`);
      // preserve original stack trace
      if (typeof Error.captureStackTrace === 'function')
      Error.captureStackTrace(
      requiredParamError,
      requiredParam
      );

      throw requiredParamError;


      const Logger =
      configure: (
      url = requiredParam('url'),
      service = requiredParam('service'),
      version = requiredParam('version'),
      apigeeClientId = requiredParam('apigeeClientId')
      ) =>
      const prepareForwardLogFunction = function prepareForwardLogFunction(logLevel)
      return async function forwardLog(details, exception)
      const payload =
      ...details,
      service,
      version,
      loggername: 'UiLogger',
      serviceType: 'reactjs',
      level: logLevel,
      eventType: logLevel === 'error' ? 'Exception' : 'Trace',
      correlationId: uuid(),
      timestamp: new Date(),
      exception: exception !== undefined ? exception : null,
      request: global.window.location.href
      ;

      try
      await post(url, payload, apigeeClientId);
      catch (error)
      console.warn('UI Logger failed:', url, payload, error ); // eslint-disable-line no-console

      ;
      ;

      return
      debug: prepareForwardLogFunction('debug'),
      info: prepareForwardLogFunction('info'),
      warn: prepareForwardLogFunction('warn'),
      error: prepareForwardLogFunction('error')
      ;

      ;


      export default Logger;






      share|improve this question













      As part of the project I am currently working on we want to log our UI component errors as well. I developed this package as a base.
      It's a factory (basically, not sure if it's correctly a factory).
      There is one function exposed configure that takes the parameters that each service (of the project) needs to supply and returns a logger; an object with four functions - debug, info, warn and error.



      I am not sure if this is the right way to do it in javascript. Or in general. Is this how a factory should look? I have seen some that expose a function rather than an object that has a function on it but in my mind these two seem equivalent.



      import uuid from 'uuid/v4';
      import post from './post';

      function requiredParam(param)
      const requiredParamError = new Error(`Required parameter, "$param" is missing.`);
      // preserve original stack trace
      if (typeof Error.captureStackTrace === 'function')
      Error.captureStackTrace(
      requiredParamError,
      requiredParam
      );

      throw requiredParamError;


      const Logger =
      configure: (
      url = requiredParam('url'),
      service = requiredParam('service'),
      version = requiredParam('version'),
      apigeeClientId = requiredParam('apigeeClientId')
      ) =>
      const prepareForwardLogFunction = function prepareForwardLogFunction(logLevel)
      return async function forwardLog(details, exception)
      const payload =
      ...details,
      service,
      version,
      loggername: 'UiLogger',
      serviceType: 'reactjs',
      level: logLevel,
      eventType: logLevel === 'error' ? 'Exception' : 'Trace',
      correlationId: uuid(),
      timestamp: new Date(),
      exception: exception !== undefined ? exception : null,
      request: global.window.location.href
      ;

      try
      await post(url, payload, apigeeClientId);
      catch (error)
      console.warn('UI Logger failed:', url, payload, error ); // eslint-disable-line no-console

      ;
      ;

      return
      debug: prepareForwardLogFunction('debug'),
      info: prepareForwardLogFunction('info'),
      warn: prepareForwardLogFunction('warn'),
      error: prepareForwardLogFunction('error')
      ;

      ;


      export default Logger;








      share|improve this question












      share|improve this question




      share|improve this question








      edited Apr 29 at 3:31









      200_success

      123k14142399




      123k14142399









      asked Apr 28 at 21:12









      Andreas Andreou

      1521




      1521

























          active

          oldest

          votes











          Your Answer




          StackExchange.ifUsing("editor", function ()
          return StackExchange.using("mathjaxEditing", function ()
          StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix)
          StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
          );
          );
          , "mathjax-editing");

          StackExchange.ifUsing("editor", function ()
          StackExchange.using("externalEditor", function ()
          StackExchange.using("snippets", function ()
          StackExchange.snippets.init();
          );
          );
          , "code-snippets");

          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "196"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          convertImagesToLinks: false,
          noModals: false,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          bindNavPrevention: true,
          postfix: "",
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );








           

          draft saved


          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f193166%2flogs-ui-errors-to-a-service-provided-in-the-configuration%23new-answer', 'question_page');

          );

          Post as a guest



































          active

          oldest

          votes













          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes










           

          draft saved


          draft discarded


























           


          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f193166%2flogs-ui-errors-to-a-service-provided-in-the-configuration%23new-answer', 'question_page');

          );

          Post as a guest













































































          Popular posts from this blog

          Chat program with C++ and SFML

          Function to Return a JSON Like Objects Using VBA Collections and Arrays

          Will my employers contract hold up in court?