Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for json logs #10

Open
wants to merge 2 commits into
base: master
from
Open
Changes from 1 commit
Commits
File filter...
Filter file types
Jump to…
Jump to file
Failed to load files.

Always

Just for now

Prev

added support for json logs

  • Loading branch information
nchalkidou
nchalkidou committed May 13, 2019
commit f0c6fa1b6f06630223057c3d144be010f9638b9f
@@ -15,6 +15,11 @@
*/
package com.github.tony19.loggly;

import com.google.gson.JsonObject;
import com.google.gson.internal.LinkedTreeMap;

import org.json.JSONObject;

import java.util.Collection;

/**
@@ -55,6 +60,13 @@
*/
void log(String message, Callback callback);

/**
* Writes a single json log event asynchronously
* @param message message to be logged
* @param callback callback to be invoked on completion
*/
void log(LinkedTreeMap message, Callback callback);

/**
* Writes multiple log events at once
* @param messages log events to be written
@@ -15,6 +15,8 @@
*/
package com.github.tony19.loggly;

import com.google.gson.internal.LinkedTreeMap;

import retrofit2.Call;
import retrofit2.http.Body;
import retrofit2.http.Header;
@@ -39,6 +41,16 @@
@POST("inputs/{token}")
Call<LogglyResponse> log(@Path("token") String token, @Header("X-LOGGLY-TAG") String tags, @Body String message);

/**
* Posts a single json log event to Loggly's REST endpoint
*
* @param token Loggly customer token
* @param tags CSV of tags
* @param message log event to be posted
* @return result of the post as a {@link com.github.tony19.loggly.LogglyResponse}
*/
@POST("inputs/{token}")
Call<LogglyResponse> log(@Path("token") String token, @Header("X-LOGGLY-TAG") String tags, @Body LinkedTreeMap message);

/**
* Posts several log events at once to Loggly's bulk REST endpoint
@@ -15,6 +15,11 @@
*/
package com.github.tony19.loggly;

import com.google.gson.JsonObject;
import com.google.gson.internal.LinkedTreeMap;

import org.json.JSONObject;

import java.util.Arrays;
import java.util.Collection;

@@ -108,6 +113,24 @@ public boolean log(String message) {
return ok;
}

/**
* Posts a json log message to Loggly
* @param message message to be logged
* @return {@code true} if successful; {@code false} otherwise
*/
public boolean log(LinkedTreeMap message) {
if (message == null) return false;

boolean ok;
try {
ok = loggly.log(token, tags, message).isExecuted();
} catch (Exception e) {
e.printStackTrace();
ok = false;
}
return ok;
}

/**
* Posts a log message asynchronously to Loggly
* @param message message to be logged
@@ -130,6 +153,28 @@ public void onFailure(Call<LogglyResponse> call, Throwable throwable) {
});
}

/**
* Posts a json log message asynchronously to Loggly
* @param message message to be logged
* @param callback callback to be invoked on completion of the post
*/
public void log(LinkedTreeMap message, final Callback callback) {
if (message == null) return;

Call call = loggly.log(token, tags, message);
call.enqueue(new retrofit2.Callback<LogglyResponse>() {
@Override
public void onResponse(Call<LogglyResponse> call, Response<LogglyResponse> response) {
callback.success();
}

@Override
public void onFailure(Call<LogglyResponse> call, Throwable throwable) {
callback.failure(throwable.getMessage());
}
});
}

/**
* Posts several log messages in bulk to Loggly
* @param messages messages to be logged
ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.