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

Added option to limit logging on wifi only #14

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,14 @@ Usage
Timber.i("hello world");
```


##### Limit on WiFi (optional)
If you want to reduce cellular data traffic you can limit logging only when connected to a WiFi network. If this s the case you need to add the `android.permission.ACCESS_NETWORK_STATE` in your AndroidManifest and Plant the `LogglyTree` with the limit.
```java
LogglyTree wifiLimitedLogglyTree = new LogglyTree(LOGGLY_TOKEN).limitWifi(this);
Timber.plant(wifiLimitedLogglyTree);
```

Download
--------

Expand Down
1 change: 1 addition & 0 deletions timber-loggly/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ dependencies {
testCompile 'com.fasterxml.jackson.core:jackson-databind:2.6.3'
testCompile 'com.fasterxml.jackson.core:jackson-annotations:2.6.3'

compile 'com.android.support:support-annotations:23.3.0'
compile 'com.jakewharton.timber:timber:4.1.2'
compile ('com.github.tony19:loggly-client:1.0.3') {
exclude group:'com.jakewharton.timber', module:'timber'
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
/**
* Copyright (C) 2015 Anthony K. Trinh
*
* <p>
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* <p>
* http://www.apache.org/licenses/LICENSE-2.0
* <p>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
Expand All @@ -15,8 +15,18 @@
*/
package com.github.tony19.timber.loggly;

import android.Manifest;
import android.annotation.SuppressLint;
import android.content.Context;
import android.content.pm.PackageManager;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.support.annotation.NonNull;
import android.support.annotation.RequiresPermission;

import com.github.tony19.loggly.ILogglyClient;
import com.github.tony19.loggly.LogglyClient;

import timber.log.Timber;

/**
Expand All @@ -31,6 +41,11 @@ public class LogglyTree extends Timber.Tree {
private ILogglyClient.Callback handler;
private IFormatter formatter;

private boolean isLimitWifi = false;

private Context context;
private ConnectivityManager connectivityManager;

/**
* Creates a <a href="https://github.com/JakeWharton/timber">Timber</a>
* tree for posting messages to <a href="http://loggly.com">Loggly</a>
Expand All @@ -55,6 +70,37 @@ public LogglyTree(String token) {
this.formatter = formatter;
}

/**
* Limits the {@link LogglyTree} to only send logs while connected to WiFi or suppress them otherwise.
* @param context Context
* @return a {@link LogglyTree} that only logs over WiFi
*/
@RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
public LogglyTree limitWifi(@NonNull Context context) {
isLimitWifi = true;

this.context = context;
this.connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);

return this;
}

/**
* Determines if the device is connected to a WiFi network.
* @return true if device is connected to WiFi or false otherwise
*/
@SuppressLint("MissingPermission")
private boolean isOnWifi() {
if(context.checkCallingOrSelfPermission(Manifest.permission.ACCESS_NETWORK_STATE)
== PackageManager.PERMISSION_GRANTED) {
NetworkInfo activeNetwork = connectivityManager.getActiveNetworkInfo();
if (activeNetwork != null && activeNetwork.isConnectedOrConnecting()) {
return activeNetwork.getType() == ConnectivityManager.TYPE_WIFI;
}
}
return false;
}

/**
* Writes a log message to its destination. Called for all level-specific methods by default.
*
Expand All @@ -65,7 +111,9 @@ public LogglyTree(String token) {
*/
@Override
protected void log(int priority, String tag, String message, Throwable t) {
loggly.log(formatter.format(priority, tag, message, t), handler);
if (!isLimitWifi || isOnWifi()) {
loggly.log(formatter.format(priority, tag, message, t), handler);
}
}

/**
Expand Down