Creating a logger in the top level of my react app and then using it in other components as I need it

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












//App.jsx
import React, Component from 'react';
import initialiseLogger from './logger';

class App extends Component
constructor(props)
super(props);
initialiseLogger(props.environment);

render()
return (
<Component />
);



export default App;


This it he top level of my React app. I initialise my logger here.
And below is the logger. I am using our ui logger by my project. It exposes one function called configure that creates a logger based on some params. Each time it gets called it will create a new logger.



//logger.js
import LoggerFactory from '@company/ui-logger';
import appConfigurations from '../shared/appConfigurations';

const loggerContainer = ;

const initialiseLogger = (env) =>
const url = appConfigurations.getLogUrl(env);
const service = appConfigurations.getLogUrl(env);
const version = appConfigurations.getComponentVersion(env);
const apigeeClientId = appConfigurations.getComponentName(env);
Object.freeze(
Object.assign(
loggerContainer,
logger: LoggerFactory.configure( url, service, version, apigeeClientId )
)
);
return loggerContainer.logger;
;
const getLogger = () =>
if (!loggerContainer.logger)
// eslint-disable-next-line no-console
console.error('A logger has not been initialised');

return loggerContainer.logger;
;

export initialiseLogger, getLogger ;


As you can see initialiseLogger gets some app config and creates a logger which then gets assigned inside a const object that then becomes immutable.
I also expose a getLogger method that is going to be used in the other components to get the logger and then log what they want.



Example:



import React, Component from 'react';
import PropTypes from 'prop-types';
import getLogger from '../logger';

class ErrorBox extends Component
constructor()
super();
this.state =
isVisible: false,
apiErrors: ,
uiErrors:
;
this.logger = getLogger();


componentDidMount()
...


componentWillUnmount()
...


updateErrorList = () =>
...


componentDidCatch(error)
this.setState(
isVisible: true,
uiErrors: [error]
);

this.logger.error(
message: error.message ,
error.stack
);



render()
...



export default ErrorBox;


I am not feeling 100% sure about this code.
Should I go for a singleton pattern and just expose the getLogger function.



Something like



import LoggerFactory from '@company/ui-logger';
import appConfigurations from '../shared/appConfigurations';

const loggerContainer = ;

const initialiseLogger = (env) =>
const url = appConfigurations.getLogUrl(env);
const service = appConfigurations.getLogUrl(env);
const version = appConfigurations.getComponentVersion(env);
const apigeeClientId = appConfigurations.getComponentName(env);
Object.freeze(
Object.assign(
loggerContainer,
logger: LoggerFactory.configure( url, service, version, apigeeClientId )
)
);
return loggerContainer.logger;
;
const getLogger = () =>
if (!loggerContainer.logger)
initialiseLogger('env');

return loggerContainer.logger;
;

export default getLogger;


Or even



import LoggerFactory from '@company/ui-logger';
import appConfigurations from '../shared/appConfigurations';

const Logger = () =>
let instance;

const initialiseLogger = (env) =>
const url = appConfigurations.getLogUrl(env);
const service = appConfigurations.getLogUrl(env);
const version = appConfigurations.getComponentVersion(env);
const apigeeClientId = appConfigurations.getComponentName(env);
return LoggerFactory.configure( url, service, version, apigeeClientId );
;

return
getLogger()
if (!instance)
instance = initialiseLogger('env');

return instance;

;
;

export default Logger();


But i am not sure about my design of this singleton. Would the instance be visble to getLogger when I export it as a module to another compoent?







share|improve this question

























    up vote
    1
    down vote

    favorite












    //App.jsx
    import React, Component from 'react';
    import initialiseLogger from './logger';

    class App extends Component
    constructor(props)
    super(props);
    initialiseLogger(props.environment);

    render()
    return (
    <Component />
    );



    export default App;


    This it he top level of my React app. I initialise my logger here.
    And below is the logger. I am using our ui logger by my project. It exposes one function called configure that creates a logger based on some params. Each time it gets called it will create a new logger.



    //logger.js
    import LoggerFactory from '@company/ui-logger';
    import appConfigurations from '../shared/appConfigurations';

    const loggerContainer = ;

    const initialiseLogger = (env) =>
    const url = appConfigurations.getLogUrl(env);
    const service = appConfigurations.getLogUrl(env);
    const version = appConfigurations.getComponentVersion(env);
    const apigeeClientId = appConfigurations.getComponentName(env);
    Object.freeze(
    Object.assign(
    loggerContainer,
    logger: LoggerFactory.configure( url, service, version, apigeeClientId )
    )
    );
    return loggerContainer.logger;
    ;
    const getLogger = () =>
    if (!loggerContainer.logger)
    // eslint-disable-next-line no-console
    console.error('A logger has not been initialised');

    return loggerContainer.logger;
    ;

    export initialiseLogger, getLogger ;


    As you can see initialiseLogger gets some app config and creates a logger which then gets assigned inside a const object that then becomes immutable.
    I also expose a getLogger method that is going to be used in the other components to get the logger and then log what they want.



    Example:



    import React, Component from 'react';
    import PropTypes from 'prop-types';
    import getLogger from '../logger';

    class ErrorBox extends Component
    constructor()
    super();
    this.state =
    isVisible: false,
    apiErrors: ,
    uiErrors:
    ;
    this.logger = getLogger();


    componentDidMount()
    ...


    componentWillUnmount()
    ...


    updateErrorList = () =>
    ...


    componentDidCatch(error)
    this.setState(
    isVisible: true,
    uiErrors: [error]
    );

    this.logger.error(
    message: error.message ,
    error.stack
    );



    render()
    ...



    export default ErrorBox;


    I am not feeling 100% sure about this code.
    Should I go for a singleton pattern and just expose the getLogger function.



    Something like



    import LoggerFactory from '@company/ui-logger';
    import appConfigurations from '../shared/appConfigurations';

    const loggerContainer = ;

    const initialiseLogger = (env) =>
    const url = appConfigurations.getLogUrl(env);
    const service = appConfigurations.getLogUrl(env);
    const version = appConfigurations.getComponentVersion(env);
    const apigeeClientId = appConfigurations.getComponentName(env);
    Object.freeze(
    Object.assign(
    loggerContainer,
    logger: LoggerFactory.configure( url, service, version, apigeeClientId )
    )
    );
    return loggerContainer.logger;
    ;
    const getLogger = () =>
    if (!loggerContainer.logger)
    initialiseLogger('env');

    return loggerContainer.logger;
    ;

    export default getLogger;


    Or even



    import LoggerFactory from '@company/ui-logger';
    import appConfigurations from '../shared/appConfigurations';

    const Logger = () =>
    let instance;

    const initialiseLogger = (env) =>
    const url = appConfigurations.getLogUrl(env);
    const service = appConfigurations.getLogUrl(env);
    const version = appConfigurations.getComponentVersion(env);
    const apigeeClientId = appConfigurations.getComponentName(env);
    return LoggerFactory.configure( url, service, version, apigeeClientId );
    ;

    return
    getLogger()
    if (!instance)
    instance = initialiseLogger('env');

    return instance;

    ;
    ;

    export default Logger();


    But i am not sure about my design of this singleton. Would the instance be visble to getLogger when I export it as a module to another compoent?







    share|improve this question





















      up vote
      1
      down vote

      favorite









      up vote
      1
      down vote

      favorite











      //App.jsx
      import React, Component from 'react';
      import initialiseLogger from './logger';

      class App extends Component
      constructor(props)
      super(props);
      initialiseLogger(props.environment);

      render()
      return (
      <Component />
      );



      export default App;


      This it he top level of my React app. I initialise my logger here.
      And below is the logger. I am using our ui logger by my project. It exposes one function called configure that creates a logger based on some params. Each time it gets called it will create a new logger.



      //logger.js
      import LoggerFactory from '@company/ui-logger';
      import appConfigurations from '../shared/appConfigurations';

      const loggerContainer = ;

      const initialiseLogger = (env) =>
      const url = appConfigurations.getLogUrl(env);
      const service = appConfigurations.getLogUrl(env);
      const version = appConfigurations.getComponentVersion(env);
      const apigeeClientId = appConfigurations.getComponentName(env);
      Object.freeze(
      Object.assign(
      loggerContainer,
      logger: LoggerFactory.configure( url, service, version, apigeeClientId )
      )
      );
      return loggerContainer.logger;
      ;
      const getLogger = () =>
      if (!loggerContainer.logger)
      // eslint-disable-next-line no-console
      console.error('A logger has not been initialised');

      return loggerContainer.logger;
      ;

      export initialiseLogger, getLogger ;


      As you can see initialiseLogger gets some app config and creates a logger which then gets assigned inside a const object that then becomes immutable.
      I also expose a getLogger method that is going to be used in the other components to get the logger and then log what they want.



      Example:



      import React, Component from 'react';
      import PropTypes from 'prop-types';
      import getLogger from '../logger';

      class ErrorBox extends Component
      constructor()
      super();
      this.state =
      isVisible: false,
      apiErrors: ,
      uiErrors:
      ;
      this.logger = getLogger();


      componentDidMount()
      ...


      componentWillUnmount()
      ...


      updateErrorList = () =>
      ...


      componentDidCatch(error)
      this.setState(
      isVisible: true,
      uiErrors: [error]
      );

      this.logger.error(
      message: error.message ,
      error.stack
      );



      render()
      ...



      export default ErrorBox;


      I am not feeling 100% sure about this code.
      Should I go for a singleton pattern and just expose the getLogger function.



      Something like



      import LoggerFactory from '@company/ui-logger';
      import appConfigurations from '../shared/appConfigurations';

      const loggerContainer = ;

      const initialiseLogger = (env) =>
      const url = appConfigurations.getLogUrl(env);
      const service = appConfigurations.getLogUrl(env);
      const version = appConfigurations.getComponentVersion(env);
      const apigeeClientId = appConfigurations.getComponentName(env);
      Object.freeze(
      Object.assign(
      loggerContainer,
      logger: LoggerFactory.configure( url, service, version, apigeeClientId )
      )
      );
      return loggerContainer.logger;
      ;
      const getLogger = () =>
      if (!loggerContainer.logger)
      initialiseLogger('env');

      return loggerContainer.logger;
      ;

      export default getLogger;


      Or even



      import LoggerFactory from '@company/ui-logger';
      import appConfigurations from '../shared/appConfigurations';

      const Logger = () =>
      let instance;

      const initialiseLogger = (env) =>
      const url = appConfigurations.getLogUrl(env);
      const service = appConfigurations.getLogUrl(env);
      const version = appConfigurations.getComponentVersion(env);
      const apigeeClientId = appConfigurations.getComponentName(env);
      return LoggerFactory.configure( url, service, version, apigeeClientId );
      ;

      return
      getLogger()
      if (!instance)
      instance = initialiseLogger('env');

      return instance;

      ;
      ;

      export default Logger();


      But i am not sure about my design of this singleton. Would the instance be visble to getLogger when I export it as a module to another compoent?







      share|improve this question











      //App.jsx
      import React, Component from 'react';
      import initialiseLogger from './logger';

      class App extends Component
      constructor(props)
      super(props);
      initialiseLogger(props.environment);

      render()
      return (
      <Component />
      );



      export default App;


      This it he top level of my React app. I initialise my logger here.
      And below is the logger. I am using our ui logger by my project. It exposes one function called configure that creates a logger based on some params. Each time it gets called it will create a new logger.



      //logger.js
      import LoggerFactory from '@company/ui-logger';
      import appConfigurations from '../shared/appConfigurations';

      const loggerContainer = ;

      const initialiseLogger = (env) =>
      const url = appConfigurations.getLogUrl(env);
      const service = appConfigurations.getLogUrl(env);
      const version = appConfigurations.getComponentVersion(env);
      const apigeeClientId = appConfigurations.getComponentName(env);
      Object.freeze(
      Object.assign(
      loggerContainer,
      logger: LoggerFactory.configure( url, service, version, apigeeClientId )
      )
      );
      return loggerContainer.logger;
      ;
      const getLogger = () =>
      if (!loggerContainer.logger)
      // eslint-disable-next-line no-console
      console.error('A logger has not been initialised');

      return loggerContainer.logger;
      ;

      export initialiseLogger, getLogger ;


      As you can see initialiseLogger gets some app config and creates a logger which then gets assigned inside a const object that then becomes immutable.
      I also expose a getLogger method that is going to be used in the other components to get the logger and then log what they want.



      Example:



      import React, Component from 'react';
      import PropTypes from 'prop-types';
      import getLogger from '../logger';

      class ErrorBox extends Component
      constructor()
      super();
      this.state =
      isVisible: false,
      apiErrors: ,
      uiErrors:
      ;
      this.logger = getLogger();


      componentDidMount()
      ...


      componentWillUnmount()
      ...


      updateErrorList = () =>
      ...


      componentDidCatch(error)
      this.setState(
      isVisible: true,
      uiErrors: [error]
      );

      this.logger.error(
      message: error.message ,
      error.stack
      );



      render()
      ...



      export default ErrorBox;


      I am not feeling 100% sure about this code.
      Should I go for a singleton pattern and just expose the getLogger function.



      Something like



      import LoggerFactory from '@company/ui-logger';
      import appConfigurations from '../shared/appConfigurations';

      const loggerContainer = ;

      const initialiseLogger = (env) =>
      const url = appConfigurations.getLogUrl(env);
      const service = appConfigurations.getLogUrl(env);
      const version = appConfigurations.getComponentVersion(env);
      const apigeeClientId = appConfigurations.getComponentName(env);
      Object.freeze(
      Object.assign(
      loggerContainer,
      logger: LoggerFactory.configure( url, service, version, apigeeClientId )
      )
      );
      return loggerContainer.logger;
      ;
      const getLogger = () =>
      if (!loggerContainer.logger)
      initialiseLogger('env');

      return loggerContainer.logger;
      ;

      export default getLogger;


      Or even



      import LoggerFactory from '@company/ui-logger';
      import appConfigurations from '../shared/appConfigurations';

      const Logger = () =>
      let instance;

      const initialiseLogger = (env) =>
      const url = appConfigurations.getLogUrl(env);
      const service = appConfigurations.getLogUrl(env);
      const version = appConfigurations.getComponentVersion(env);
      const apigeeClientId = appConfigurations.getComponentName(env);
      return LoggerFactory.configure( url, service, version, apigeeClientId );
      ;

      return
      getLogger()
      if (!instance)
      instance = initialiseLogger('env');

      return instance;

      ;
      ;

      export default Logger();


      But i am not sure about my design of this singleton. Would the instance be visble to getLogger when I export it as a module to another compoent?









      share|improve this question










      share|improve this question




      share|improve this question









      asked Apr 28 at 21:42









      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%2f193168%2fcreating-a-logger-in-the-top-level-of-my-react-app-and-then-using-it-in-other-co%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%2f193168%2fcreating-a-logger-in-the-top-level-of-my-react-app-and-then-using-it-in-other-co%23new-answer', 'question_page');

          );

          Post as a guest













































































          Popular posts from this blog

          Greedy Best First Search implementation in Rust

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

          C++11 CLH Lock Implementation