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 all commits
Commits
File filter...
Filter file types
Jump to…
Jump to file
Failed to load files.

Always

Just for now

@@ -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,8 +15,9 @@
*/
package com.github.tony19.loggly;

import com.google.gson.internal.LinkedTreeMap;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.http.Body;
import retrofit2.http.Header;
import retrofit2.http.POST;
@@ -41,14 +42,15 @@
Call<LogglyResponse> log(@Path("token") String token, @Header("X-LOGGLY-TAG") String tags, @Body String message);

/**
* Posts a single log event to Loggly's REST endpoint
* @param token Loggly customer token
* @param tags CSV of tags
* 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
* @param callback callback to be invoked on completion of the post
* @return result of the post as a {@link com.github.tony19.loggly.LogglyResponse}
*/
@POST("inputs/{token}")
Call<Void> log(@Path("token") String token, @Header("X-LOGGLY-TAG") String tags, @Body String message, Callback<LogglyResponse> callback);
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
@@ -62,15 +64,4 @@
@POST("bulk/{token}")
Call<LogglyResponse> logBulk(@Path("token") String token, @Header("X-LOGGLY-TAG") String tags, @Body String messages);

/**
* Posts several log events at once to Loggly's bulk REST endpoint
* @param token Loggly customer token
* @param tags CSV of tags
* @param messages log event messages, each delimited by new-line
* The text is parsed for a log event in each line.
* e.g., "Hello\nWorld" would create two log events.
* @param callback callback to be invoked on completion of the post
*/
@POST("bulk/{token}")
Call<Void> logBulk(@Path("token") String token, @Header("X-LOGGLY-TAG") String tags, @Body String messages, Callback<LogglyResponse> callback);
}
@@ -15,14 +15,19 @@
*/
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;

import retrofit2.Call;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

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

/**
* Loggly client
*
@@ -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
@@ -116,20 +139,40 @@ public boolean log(String message) {
public void log(String message, final Callback callback) {
if (message == null) return;

loggly.log(token,
tags,
message,
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());
}
});
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 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());
}
});
}

/**
@@ -171,23 +214,23 @@ public boolean logBulk(Collection<String> messages) {
public void logBulk(Collection<String> messages, final Callback callback) {
if (messages == null) return;



String parcel = joinStrings(messages);
if (parcel.isEmpty()) return;

loggly.logBulk(token,
tags,
parcel,
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());
}
});
Call call = loggly.logBulk(token, tags, parcel);
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());
}
});
}

/**
ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.