move logging stuff to logging module

This commit is contained in:
Максим Бачинский 2020-05-27 20:58:05 +03:00
parent 45dddff10a
commit f3bd3b2f69
7 changed files with 81 additions and 73 deletions

View File

@ -19,14 +19,13 @@
package ru.touchin.templates;
import androidx.annotation.CallSuper;
import androidx.annotation.NonNull;
import java.io.IOException;
import java.io.Serializable;
import java.util.Collection;
import java.util.Iterator;
import androidx.annotation.CallSuper;
import androidx.annotation.NonNull;
import ru.touchin.roboswag.core.log.Lc;
import ru.touchin.roboswag.core.log.LcGroup;
@ -38,11 +37,6 @@ public abstract class ApiModel implements Serializable {
private static final long serialVersionUID = 1L;
/**
* Logging group to log API validation errors.
*/
public static final LcGroup API_VALIDATION_LC_GROUP = new LcGroup("API_VALIDATION");
/**
* Validates list of objects. Use it if objects in list extends {@link ApiModel}.
*
@ -76,14 +70,14 @@ public abstract class ApiModel implements Serializable {
throw exception;
case EXCEPTION_IF_ALL_INVALID:
iterator.remove();
API_VALIDATION_LC_GROUP.e(exception, "Item %s is invalid at " + Lc.getCodePoint(null, 1), position);
LcGroup.API_VALIDATION.e(exception, "Item %s is invalid at " + Lc.getCodePoint(null, 1), position);
if (!iterator.hasNext() && !haveValidItem) {
throw new ValidationException("Whole list is invalid at " + Lc.getCodePoint(null, 1));
}
break;
case REMOVE_INVALID_ITEMS:
iterator.remove();
API_VALIDATION_LC_GROUP.e(exception, "Item %s is invalid at " + Lc.getCodePoint(null, 1), position);
LcGroup.API_VALIDATION.e(exception, "Item %s is invalid at " + Lc.getCodePoint(null, 1), position);
break;
default:
Lc.assertion("Unexpected rule " + collectionValidationRule);

View File

@ -15,4 +15,8 @@ android {
dependencies {
implementation "androidx.annotation:annotation:$versions.androidx"
implementation("com.crashlytics.sdk.android:crashlytics:$versions.crashlytics@aar") {
transitive = true
}
}

View File

@ -19,12 +19,11 @@
package ru.touchin.roboswag.core.log;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import java.text.SimpleDateFormat;
import java.util.Locale;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import ru.touchin.roboswag.core.utils.ShouldNotHappenException;
import ru.touchin.roboswag.core.utils.ThreadLocalValue;
@ -45,6 +44,10 @@ public class LcGroup {
* Logging group to log UI lifecycle (onCreate, onStart, onResume etc.).
*/
public static final LcGroup UI_LIFECYCLE = new LcGroup("UI_LIFECYCLE");
/**
* Logging group to log UI lifecycle (onCreate, onStart, onResume etc.).
*/
public static final LcGroup API_VALIDATION = new LcGroup("API_VALIDATION");
private static final ThreadLocalValue<SimpleDateFormat> DATE_TIME_FORMATTER
= new ThreadLocalValue<>(() -> new SimpleDateFormat("HH:mm:ss.SSS", Locale.getDefault()));

View File

@ -0,0 +1,66 @@
package ru.touchin.roboswag.core.utils;
import android.util.Log;
import com.crashlytics.android.Crashlytics;
import java.util.ArrayList;
import java.util.List;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import ru.touchin.roboswag.core.log.Lc;
import ru.touchin.roboswag.core.log.LcGroup;
import ru.touchin.roboswag.core.log.LcLevel;
import ru.touchin.roboswag.core.log.LogProcessor;
public class CrashlyticsLogProcessor extends LogProcessor {
@NonNull
private final Crashlytics crashlytics;
public CrashlyticsLogProcessor(@NonNull final Crashlytics crashlytics) {
super(LcLevel.INFO);
this.crashlytics = crashlytics;
}
@Override
public void processLogMessage(@NonNull final LcGroup group,
@NonNull final LcLevel level,
@NonNull final String tag,
@NonNull final String message,
@Nullable final Throwable throwable) {
if (group == LcGroup.UI_LIFECYCLE) {
crashlytics.core.log(level.getPriority(), tag, message);
} else if (!level.lessThan(LcLevel.ASSERT)
|| (group == LcGroup.API_VALIDATION && level == LcLevel.ERROR)) {
Log.e(tag, message);
if (throwable != null) {
crashlytics.core.log(level.getPriority(), tag, message);
crashlytics.core.logException(throwable);
} else {
final ShouldNotHappenException exceptionToLog = new ShouldNotHappenException(tag + ':' + message);
reduceStackTrace(exceptionToLog);
crashlytics.core.logException(exceptionToLog);
}
}
}
private void reduceStackTrace(@NonNull final Throwable throwable) {
final StackTraceElement[] stackTrace = throwable.getStackTrace();
final List<StackTraceElement> reducedStackTraceList = new ArrayList<>();
for (int i = stackTrace.length - 1; i >= 0; i--) {
final StackTraceElement stackTraceElement = stackTrace[i];
if (stackTraceElement.getClassName().contains(getClass().getSimpleName())
|| stackTraceElement.getClassName().contains(LcGroup.class.getName())
|| stackTraceElement.getClassName().contains(Lc.class.getName())) {
break;
}
reducedStackTraceList.add(0, stackTraceElement);
}
StackTraceElement[] reducedStackTrace = new StackTraceElement[reducedStackTraceList.size()];
reducedStackTrace = reducedStackTraceList.toArray(reducedStackTrace);
throwable.setStackTrace(reducedStackTrace);
}
}

View File

@ -23,7 +23,6 @@ android {
dependencies {
api project(":utils")
api project(":logging")
api project(":api-logansquare")
api 'androidx.multidex:multidex:2.0.1'

View File

@ -28,20 +28,14 @@ import com.crashlytics.android.Crashlytics;
import net.danlew.android.joda.JodaTimeAndroid;
import java.util.ArrayList;
import java.util.List;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.multidex.MultiDex;
import io.fabric.sdk.android.Fabric;
import ru.touchin.roboswag.core.log.ConsoleLogProcessor;
import ru.touchin.roboswag.core.log.Lc;
import ru.touchin.roboswag.core.log.LcGroup;
import ru.touchin.roboswag.core.log.LcLevel;
import ru.touchin.roboswag.core.log.LogProcessor;
import ru.touchin.roboswag.core.utils.ShouldNotHappenException;
import ru.touchin.templates.ApiModel;
import ru.touchin.roboswag.core.utils.CrashlyticsLogProcessor;
/**
* Created by Gavriil Sitnikov on 10/03/16.
@ -93,55 +87,4 @@ public abstract class TouchinApp extends Application {
.build());
}
private static class CrashlyticsLogProcessor extends LogProcessor {
@NonNull
private final Crashlytics crashlytics;
public CrashlyticsLogProcessor(@NonNull final Crashlytics crashlytics) {
super(LcLevel.INFO);
this.crashlytics = crashlytics;
}
@Override
public void processLogMessage(@NonNull final LcGroup group,
@NonNull final LcLevel level,
@NonNull final String tag,
@NonNull final String message,
@Nullable final Throwable throwable) {
if (group == LcGroup.UI_LIFECYCLE) {
crashlytics.core.log(level.getPriority(), tag, message);
} else if (!level.lessThan(LcLevel.ASSERT)
|| (group == ApiModel.API_VALIDATION_LC_GROUP && level == LcLevel.ERROR)) {
Log.e(tag, message);
if (throwable != null) {
crashlytics.core.log(level.getPriority(), tag, message);
crashlytics.core.logException(throwable);
} else {
final ShouldNotHappenException exceptionToLog = new ShouldNotHappenException(tag + ':' + message);
reduceStackTrace(exceptionToLog);
crashlytics.core.logException(exceptionToLog);
}
}
}
private void reduceStackTrace(@NonNull final Throwable throwable) {
final StackTraceElement[] stackTrace = throwable.getStackTrace();
final List<StackTraceElement> reducedStackTraceList = new ArrayList<>();
for (int i = stackTrace.length - 1; i >= 0; i--) {
final StackTraceElement stackTraceElement = stackTrace[i];
if (stackTraceElement.getClassName().contains(getClass().getSimpleName())
|| stackTraceElement.getClassName().contains(LcGroup.class.getName())
|| stackTraceElement.getClassName().contains(Lc.class.getName())) {
break;
}
reducedStackTraceList.add(0, stackTraceElement);
}
StackTraceElement[] reducedStackTrace = new StackTraceElement[reducedStackTraceList.size()];
reducedStackTrace = reducedStackTraceList.toArray(reducedStackTrace);
throwable.setStackTrace(reducedStackTrace);
}
}
}

View File

@ -23,7 +23,6 @@ android {
dependencies {
api project(":utils")
api project(":logging")
api project(":api-logansquare")
api project(":navigation-base")
api 'androidx.multidex:multidex:2.0.1'