Skip to content

Commit

Permalink
Merge pull request #32 from sematext/allow_static_usage
Browse files Browse the repository at this point in the history
Allow static usage of Logsene object
  • Loading branch information
Rafał Kuć authored Dec 22, 2020
2 parents 248799a + 3416d3d commit 1d8d5ad
Show file tree
Hide file tree
Showing 8 changed files with 173 additions and 105 deletions.
53 changes: 49 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ Add the following inside the application manifest (inside `<application>`):
* **LogseneRequiresUnmeteredNetwork**: if logs should be shipped only on unmetered network connection
* **LogseneRequiresDeviceIdle**: if logs should be shipped only when device is idle
* **LogseneRequiresBatteryNotLow**: if logs should be shipped only when battery is not low
* **LogseneAutomaticLocationEnabled**: if logs should be automatically enriched with device location information


Example Application
Expand All @@ -99,6 +100,22 @@ To see how some basic use cases are actually implemented, checkout the bundled `

**Note** that it's highly recommended that you use one instance of Logsene at any time in your app.

Initializing Logsene
--------------------

Starting with version **3.0.0** of the Android library `Logsene` needs to be initialized in a **static** way in order to be used. This allows usage in classes that don't have `Context` available. To do that, you need to call the following in your application code. Keep in mind this is only needed **once**:

```java
Logsene.init(this);
```

To get the instance of the `Logsene` object that supports logging you need to call the `getInstance()` method, for example:

```java
Logsene logsene = Logsene.getInstance();
logsene.info("Hello World!");
```

Mobile Application Analytics
----------------------------

Expand All @@ -109,7 +126,7 @@ try {
JSONObject event = new JSONObject();
event.put("activity", this.getClass().getSimpleName());
event.put("action", "started");
Logsene logsene = new Logsene(this);
Logsene logsene = Logsene.getInstance();
logsene.event(event);
} catch (JSONException e) {
Log.e("myapp", "Unable to construct json", e);
Expand Down Expand Up @@ -214,7 +231,8 @@ Because of the automatic retrieval of location from the device the `ACCESS_COARS
If your application uses JUL (java.util.logging) loggers, you can use the provided custom Handler for Logsene. You will need to configure it through code, since we need a reference to the `Context` object. If you configure your loggers to use the `LogseneHandler`, all log messages will be sent to Sematext for centralized logging.

```java
Logsene logsene = new Logsene(context);
Logsene.init(this);
Logsene logsene = Logsene.getInstance();
Logger logger = Logger.getLogger("mylogger");
logger.addHandler(new LogseneHandler(logsene));
```
Expand All @@ -224,7 +242,8 @@ logger.addHandler(new LogseneHandler(logsene));
If you use JUL and the `LogseneHandler`, all logged exceptions will be sent to Sematext, no further configuration is needed. However, if you don't use JUL, the library provides a helper method to log exceptions:

```java
Logsene logsene = new Logsene(context);
Logsene.init(this);
Logsene logsene = Logsene.getInstance();
try {
trySomeOperation();
} catch (IOException e) {
Expand All @@ -243,7 +262,8 @@ public class TestApplication extends Application {
@Override
public void uncaughtException(Thread thread, Throwable ex) {
// Send uncaught exception to Logsene.
Logsene logsene = new Logsene(TestApplication.this);
Logsene.init(TestApplication.this);
Logsene logsene = Logsene.getInstance();
logsene.error(ex);

// Run the default android handler if one is set
Expand All @@ -261,3 +281,28 @@ public class TestApplication extends Application {
```

Don't forget to declare the custom application class in your manifest (with `android:name` on `application` element).

Migrating to version 3.x from 2.x
---------------------------------

Starting from version **3.0.0** Logsene Android SDK contains backwards incompatible changes related to how it is initilized. You no longer need to create the `Logsene` object everytime you would like to use it for logging. You no longer create the `Logsene` object itself like this:

```java
Logsene logsene = new Logsene(context, true);

```

Instead you call the `init(Context)` method once and retrieve the instance of `Logsene` object later:

```java
Logsene.init(context);
Logsene logsene = Logsene.getInstance();
```

You also need to include the `LogseneAutomaticLocationEnabled` property in your manifest file to enable automatic log enrichment with location data:

```xml
<meta-data
android:name="LogseneAutomaticLocationEnabled"
android:value="true" />
```
2 changes: 1 addition & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ android {
minSdkVersion 19
targetSdkVersion 28
versionCode 1
versionName "2.5.0"
versionName "3.0.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
Expand Down
6 changes: 3 additions & 3 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,10 @@
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>


<meta-data
android:name="LogseneAppToken"
android:value="95efcdaf-5ca4-4c48-9975-69cd601036f7" />
Expand All @@ -33,7 +31,9 @@
<meta-data
android:name="LogseneMaxTimeTrigger"
android:value="10000" />

<meta-data
android:name="LogseneAutomaticLocationEnabled"
android:value="true" />

</application>
</manifest>
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ public class MainActivity extends AppCompatActivity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

logsene = new Logsene(this, true);
Logsene.init(this);
logsene = logsene.getInstance();

Log.e("INFO", "Android version: " + Build.VERSION.RELEASE);

Expand Down
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ buildscript {

}
dependencies {
classpath 'com.android.tools.build:gradle:4.0.2'
classpath 'com.android.tools.build:gradle:4.1.1'
classpath 'com.github.dcendents:android-maven-gradle-plugin:2.1'

// NOTE: Do not place your application dependencies here; they belong
Expand Down
4 changes: 2 additions & 2 deletions gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#Wed Jun 03 17:02:22 CEST 2020
#Wed Dec 16 21:30:23 CET 2020
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-6.1.1-all.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-6.5-all.zip
2 changes: 1 addition & 1 deletion logseneandroid/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ android {
minSdkVersion 19
targetSdkVersion 28
versionCode 4
versionName "2.5.0"
versionName "3.0.0"

testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"

Expand Down
Loading

0 comments on commit 1d8d5ad

Please sign in to comment.