0

I'm trying to log data, collected by my web application to Loggly. However after submitting JSON (or even plain text) containing strings in russian or hebrew I receive question marks at output.

Example string that contains word in russian:

curl -H "content-type:text/plain" -d "Привет, мир!" https://logs.loggly.com/inputs/YOUR-INPUT-GOES-HERE

Is there support for non-english characters in Loggly? If yes, how do I utilize it? I'm pretty sure I'm sending data as UTF-8.

Artyom Sokolov
  • 2,385
  • 3
  • 23
  • 34

3 Answers3

2

Apparently and surprisingly, it seems that only ASCII (0-127) chars are supported:

We only index ASCII and UTF-8 which falls into ASCII (If they don't fall into these two categories the data will be displayed as ?'s). My apologies for the inconvenience.

Source: http://community.loggly.com/customer/portal/questions/6549048-cyrrilic-encoding?b_id=50

cprcrack
  • 17,118
  • 7
  • 88
  • 91
1

Have not used loggly myself yet, so this is just a wild guess: The charset in the content-type header needs to be specified:

Content-Type: text/plain; charset=utf-8

Otherwise it probably defaults to ascii.

b0ti
  • 2,319
  • 1
  • 18
  • 18
0

check out this solution to the problem.

import { Injectable } from '@angular/core';

@Injectable()
export class Loggly {
  private LTracker: any;

  constructor() {

    this.LTracker = this.LTrack(window, document) || [];
    this.LTracker.push({
      'logglyKey': 'YOUR_KEY_HERE',
      'sendConsoleErrors' : true,
      'tag' : 'loggly-jslogger'
    });
  }

  push(props) {
    this.LTracker.push(props);
  }

  LTrack (window, document) {
    var LOGGLY_INPUT_PREFIX = 'http' + (('https:' === document.location.protocol ? 's' : '')) + '://',
      LOGGLY_COLLECTOR_DOMAIN = 'logs-01.loggly.com',
      LOGGLY_SESSION_KEY = 'logglytrackingsession',
      LOGGLY_SESSION_KEY_LENGTH = LOGGLY_SESSION_KEY.length + 1,
      LOGGLY_PROXY_DOMAIN = 'loggly';

    function uuid() {
      // lifted from here -> http://stackoverflow.com/questions/105034/how-to-create-a-guid-uuid-in-javascript/2117523#2117523
      return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
        var r = Math.random() * 16 | 0, v = c == 'x' ? r : (r & 0x3 | 0x8);
        return v.toString(16);
      });
    }

    function LogglyTracker() {
      this.key = false;
      this.sendConsoleErrors = false;
      this.tag = 'jslogger';
      this.useDomainProxy = false;
    }

    function setKey(tracker, key) {
      tracker.key = key;
      tracker.setSession();
      setInputUrl(tracker);
    }

    function setTag(tracker, tag) {
      tracker.tag = tag;
    }

    function setDomainProxy(tracker, useDomainProxy) {
      tracker.useDomainProxy = useDomainProxy;
      //refresh inputUrl value
      setInputUrl(tracker);
    }

    function setSendConsoleError(tracker, sendConsoleErrors) {
      tracker.sendConsoleErrors = sendConsoleErrors;

      if (tracker.sendConsoleErrors === true) {
        var _onerror = window.onerror;
        //send console error messages to Loggly
        window.onerror = function (msg, url, line, col, err){
          tracker.push({
            category: 'BrowserJsException',
            exception: {
              message: msg,
              url: url,
              lineno: line,
              colno: col,
              stack: err ? err.stack : 'n/a',
            }
          });

          if (_onerror && typeof _onerror === 'function') {
            _onerror.apply(window, arguments);
          }
        };
      }
    }

    function setInputUrl(tracker) {

      if (tracker.useDomainProxy == true) {
        tracker.inputUrl = LOGGLY_INPUT_PREFIX
          + window.location.host
          + '/'
          + LOGGLY_PROXY_DOMAIN
          + '/inputs/'
          + tracker.key
          + '/tag/'
          + tracker.tag;
      }
      else {
        tracker.inputUrl = LOGGLY_INPUT_PREFIX
          + (tracker.logglyCollectorDomain || LOGGLY_COLLECTOR_DOMAIN)
          + '/inputs/'
          + tracker.key
          + '/tag/'
          + tracker.tag;
      }
    }

    LogglyTracker.prototype = {
      setSession: function (session_id) {
        if (session_id) {
          this.session_id = session_id;
          this.setCookie(this.session_id);
        } else if (!this.session_id) {
          this.session_id = this.readCookie();
          if (!this.session_id) {
            this.session_id = uuid();
            this.setCookie(this.session_id);
          }
        }
      },
      push: function (data) {
        var type = typeof data;

        if (!data || !(type === 'object' || type === 'string')) {
          return;
        }

        var self = this;


        if (type === 'string') {
          data = {
            'text': data
          };
        } else {
          if (data.logglyCollectorDomain) {
            self.logglyCollectorDomain = data.logglyCollectorDomain;
            return;
          }

          if (data.sendConsoleErrors !== undefined) {
            setSendConsoleError(self, data.sendConsoleErrors);
          }

          if (data.tag) {
            setTag(self, data.tag);
          }

          if (data.useDomainProxy) {
            setDomainProxy(self, data.useDomainProxy);
          }

          if (data.logglyKey) {
            setKey(self, data.logglyKey);
            return;
          }

          if (data.session_id) {
            self.setSession(data.session_id);
            return;
          }
        }

        if (!self.key) {
          return;
        }

        self.track(data);


      },
      track: function (data) {
        // inject session id
        data.sessionId = this.session_id;

        try {
          //creating an asynchronous XMLHttpRequest
          var xmlHttp = new XMLHttpRequest();
          xmlHttp.open('POST', this.inputUrl, true); //true for asynchronous request
          xmlHttp.setRequestHeader('Content-Type', 'text/plain; charset=utf-8');
          xmlHttp.send(JSON.stringify(data));

        } catch (ex) {
          if (window && window.console && typeof window.console.log === 'function') {
            console.log("Failed to log to loggly because of this exception:\n" + ex);
            console.log("Failed log data:", data);
          }
        }
      },
      /**
       *  These cookie functions are not a global utilities.  It is for purpose of this tracker only
       */
      readCookie: function () {
        var cookie = document.cookie,
          i = cookie.indexOf(LOGGLY_SESSION_KEY);
        if (i < 0) {
          return false;
        } else {
          var end = cookie.indexOf(';', i + 1);
          end = end < 0 ? cookie.length : end;
          return cookie.slice(i + LOGGLY_SESSION_KEY_LENGTH, end);
        }
      },
      setCookie: function (value) {
        document.cookie = LOGGLY_SESSION_KEY + '=' + value;
      }
    };

    var existing = window._LTracker;

    var tracker = new LogglyTracker();

    if (existing && existing.length) {
      var i = 0,
        eLength = existing.length;
      for (i = 0; i < eLength; i++) {
        tracker.push(existing[i]);
      }
    }

     return tracker;
  };
}
Ros
  • 445
  • 4
  • 4