constructor()

in packages/@fbcmobile-ui/Logging/UserActionLogger.js [128:199]


  constructor() {
    // beaver-logger expects a value for window.location.protocol but
    // React Native doesn't polyfill window.location.  We'll do it here.
    if (typeof window != 'undefined' && !window.location) {
      window.location = {protocol: 'http'};
    }

    this._asyncFieldsLoaded = false;

    // Listen for changes to network status.  If the network is available again
    // try to flush all the stored log events from disk.
    let isConnected = true;
    NetInfo.addEventListener(state => {
      if (state.isConnected != isConnected) {
        isConnected = state.isConnected;
        if (isConnected) {
          this._flushEventsFromDisk();
        }
      }
    });

    this._applicationName = DeviceInfo.getApplicationName();
    this._brand = DeviceInfo.getBrand();
    this._buildNumber = DeviceInfo.getBuildNumber();
    this._bundleId = DeviceInfo.getBundleId();
    this._deviceId = DeviceInfo.getDeviceId();
    this._model = DeviceInfo.getModel();
    this._readableVersion = DeviceInfo.getReadableVersion();
    this._systemVersion = DeviceInfo.getSystemVersion();
    this._version = DeviceInfo.getVersion();
    this._isTablet = DeviceInfo.isTablet() ? 'true' : 'false';

    this._beaverLogger = beaver.Logger({
      // Url to send logs to - is customized in transport function
      url: '',

      // Prefix to prepend to all events
      prefix: 'mobile_app',

      logLevel: beaver.LOG_LEVEL.INFO,

      // Interval to flush logs to server - 5 seconds
      flushInterval: 5 * 1000,

      transport: async ({_url, _method, json}): Promise<any> => {
        const eventData = json.events.map(e => {
          const {event, level, payload} = e;
          const {timestamp, ...rest} = payload;
          return {
            event: {name: event},
            level,
            ts: timestamp / 1000,
            ...rest,
          };
        });

        if (!this._baseUrl) {
          return;
        }
        this._fetchParams.body = JSON.stringify(eventData);

        fetch(this._baseUrl, this._fetchParams)
          .then(_response => {
            // noop - the service returns an empty response when logs are received
          })
          .catch((_error: Error) => {
            // Store failed log events to disk so they don't get lost.
            this._setLogEvent(JSON.stringify(eventData));
          });
      },
    });
  }