Skip to content

Commit

Permalink
增加log4j打印日志
Browse files Browse the repository at this point in the history
  • Loading branch information
jiahongfei committed Jun 28, 2017
1 parent bec3c4f commit 60499be
Show file tree
Hide file tree
Showing 8 changed files with 128 additions and 3 deletions.
25 changes: 25 additions & 0 deletions app/proguard-rules.pro
@@ -0,0 +1,25 @@
# Add project specific ProGuard rules here.
# By default, the flags in this file are appended to flags specified
# in /Users/jiahongfei/Documents/DeveloperSoftware/adt-bundle-mac-x86_64-20140702/sdk/tools/proguard/proguard-android.txt
# You can edit the include path and order by changing the proguardFiles
# directive in build.gradle.
#
# For more details, see
# http://developer.android.com/guide/developing/tools/proguard.html

# Add any project specific keep options here:

# If your project uses WebView with JS, uncomment the following
# and specify the fully qualified class name to the JavaScript interface
# class:
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
# public *;
#}

# Uncomment this to preserve the line number information for
# debugging stack traces.
#-keepattributes SourceFile,LineNumberTable

# If you keep the line number information, uncomment this to
# hide the original source file name.
#-renamesourcefileattribute SourceFile
1 change: 1 addition & 0 deletions settings.gradle
@@ -0,0 +1 @@
include ':app', ':todaystepcounterlib'
5 changes: 3 additions & 2 deletions todaystepcounterlib/build.gradle
Expand Up @@ -22,10 +22,11 @@ android {
}

dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile fileTree(include: ['*.jar'], dir: 'libs')
compile 'com.android.support:appcompat-v7:25.3.1'
compile files('libs/lite-orm-1.7.0.jar')
compile 'joda-time:joda-time:2.9.9'
compile 'com.alibaba:fastjson:1.2.33'

compile files('libs/android-logging-log4j-1.0.3.jar')
compile files('libs/log4j-1.2.17.jar')
}
Binary file not shown.
Binary file added todaystepcounterlib/libs/log4j-1.2.17.jar
Binary file not shown.
25 changes: 25 additions & 0 deletions todaystepcounterlib/proguard-rules.pro
@@ -0,0 +1,25 @@
# Add project specific ProGuard rules here.
# By default, the flags in this file are appended to flags specified
# in /Users/jiahongfei/Documents/DeveloperSoftware/adt-bundle-mac-x86_64-20140702/sdk/tools/proguard/proguard-android.txt
# You can edit the include path and order by changing the proguardFiles
# directive in build.gradle.
#
# For more details, see
# http://developer.android.com/guide/developing/tools/proguard.html

# Add any project specific keep options here:

# If your project uses WebView with JS, uncomment the following
# and specify the fully qualified class name to the JavaScript interface
# class:
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
# public *;
#}

# Uncomment this to preserve the line number information for
# debugging stack traces.
#-keepattributes SourceFile,LineNumberTable

# If you keep the line number information, uncomment this to
# hide the original source file name.
#-renamesourcefileattribute SourceFile
46 changes: 46 additions & 0 deletions todaystepcounterlib/src/main/java/com/today/step/lib/Log4j.java
@@ -0,0 +1,46 @@
package com.today.step.lib;

import org.apache.log4j.Logger;

import de.mindpipe.android.logging.log4j.LogConfigurator;

/**
* Created by jiahongfei on 2017/6/27.
*/

public class Log4j {

private static Logger logger = null;
private static boolean sIsDebug = false;

public Log4j(Class clazz, String logFileName){
LogConfigurator logConfigurator = new LogConfigurator();
logConfigurator.setFileName(logFileName);
logConfigurator.setMaxFileSize(1024 * 1024 * 500);
logConfigurator.setImmediateFlush(true);
logConfigurator.configure();
logger = Logger.getLogger(clazz);
}

public void d(String message) {
if (sIsDebug)
logger.debug( message);
}

public void i( String message) {
if (sIsDebug)
logger.info(message);
}

public void w( String message) {
if (sIsDebug)
logger.warn(message);
}

public static void e(String message) {
if (sIsDebug)
logger.error(message);
}


}
Expand Up @@ -13,6 +13,7 @@
import android.hardware.SensorManager;
import android.os.Build;
import android.os.CountDownTimer;
import android.os.Environment;
import android.os.Handler;
import android.os.IBinder;
import android.os.Looper;
Expand Down Expand Up @@ -85,13 +86,19 @@ public class VitalityStepService extends Service implements SensorEventListener
private boolean mBoot = false;
private static int mSaveStepCount = 0;
private static ArrayList<VitalityStepData> mVitalityStepDataList;
private Log4j mLog4j = null;

private UploadSportStepNetwork uploadSportStepNetwork;

@Override
public void onCreate() {
Logger.e(TAG, "onCreate:" + StepDcretor.CURRENT_SETP);
super.onCreate();

mLog4j = new Log4j(VitalityStepService.class,
Environment.getExternalStorageDirectory().getAbsolutePath() + "/VitalityStepServiceLibLog.txt");


initBroadcastReceiver();
sensorManager = (SensorManager) this
.getSystemService(SENSOR_SERVICE);
Expand Down Expand Up @@ -119,6 +126,13 @@ public int onStartCommand(Intent intent, int flags, int startId) {
mBoot = intent.getBooleanExtra(INTENT_NAME_BOOT, false);
}

if(mSeparate){
mLog4j.e("0点分隔广播");
}
if(mBoot){
mLog4j.e("开机自启动广播");
}

//初始化数据库
CURRENTDATE = getTodayDate();
StepDbUtils.createDb(this, DB_NAME);
Expand Down Expand Up @@ -351,9 +365,12 @@ private void resetSysMergeStep(float currSensorStep) {
float curStep = lastSensorStep - stepOffset;
AppSharedPreferencesHelper.getInstance(getApplication()).setVitalityStepOffset(-curStep);
Logger.e(TAG, "当天重启手机合并步数");
mLog4j.e("当天重启手机合并步数");

} else {
//系统重启隔天清零
Logger.e(TAG, "隔天清零");
mLog4j.e("隔天清零");
AppSharedPreferencesHelper.getInstance(getApplication()).setVitalityStepOffset(currSensorStep);
}
AppSharedPreferencesHelper.getInstance(getApplication()).setVitalityStepToday(getTodayDate());
Expand All @@ -367,6 +384,7 @@ public void onSensorChanged(SensorEvent event) {
if (mBoot) {
mBoot = false;
Logger.e(TAG, "boot 重启手机进行归并步数");
mLog4j.e("boot 重启手机进行归并步数");
resetSysMergeStep(event.values[0]);
//测试通过
} else {
Expand All @@ -375,13 +393,15 @@ public void onSensorChanged(SensorEvent event) {
if (!TextUtils.isEmpty(stepToday) &&
(event.values[0] < AppSharedPreferencesHelper.getInstance(getApplication()).getVitalityLastSensorStep())) {
Logger.e(TAG, "如果当前计步器的步数小于上次计步器的步数肯定是关机了");
mLog4j.e("如果当前计步器的步数小于上次计步器的步数肯定是关机了");

resetSysMergeStep(event.values[0]);
//测试通过

} else if (!TextUtils.isEmpty(stepToday) &&
(AppSharedPreferencesHelper.getInstance(getApplication()).getVitalityLastSystemRunningTime() > SystemClock.elapsedRealtime())) {
Logger.e(TAG, "上次系统运行时间如果大于当前系统运行时间有很大几率是关机了(这个只是个猜测值来提高精度的,实际上有可能当前系统运行时间超过上次运行时间)");
mLog4j.e("上次系统运行时间如果大于当前系统运行时间有很大几率是关机了(这个只是个猜测值来提高精度的,实际上有可能当前系统运行时间超过上次运行时间)");

resetSysMergeStep(event.values[0]);
//测试通过
Expand All @@ -395,6 +415,8 @@ public void onSensorChanged(SensorEvent event) {
AppSharedPreferencesHelper.getInstance(getApplication()).setVitalityStepToday(getTodayDate());
AppSharedPreferencesHelper.getInstance(getApplication()).setVitalityStepOffset(event.values[0]);
Logger.e(TAG, "mSeparate =true");
mLog4j.e("mSeparate =true");

//测试
}

Expand All @@ -413,6 +435,8 @@ public void onSensorChanged(SensorEvent event) {
int step = (int) (event.values[0] - AppSharedPreferencesHelper.getInstance(getApplication()).getVitalityStepOffset());
Logger.e(TAG, "跨越0点计算前一天时间 :" + DateUtils.dateFormat(preDay, "yyyy-MM-dd HH:mm:ss"));
Logger.e(TAG, "跨越0点计算前一天步数 :" + step);
mLog4j.e("跨越0点计算前一天时间 :" + DateUtils.dateFormat(preDay, "yyyy-MM-dd HH:mm:ss"));
mLog4j.e("跨越0点计算前一天步数 :" + step);
saveVitalityStepData(preDay / 1000, step);

AppSharedPreferencesHelper.getInstance(getApplication()).setVitalityStepToday(getTodayDate());
Expand All @@ -436,6 +460,8 @@ public void onSensorChanged(SensorEvent event) {
//做个容错如果步数计算小于0直接设置成0不能有负值
if (step < 0) {
Logger.e(TAG, "做个容错如果步数计算小于0直接设置成0不能有负值");
mLog4j.e("做个容错如果步数计算小于0直接设置成0不能有负值");

step = 0;
AppSharedPreferencesHelper.getInstance(getApplication()).setVitalityStepToday(getTodayDate());
AppSharedPreferencesHelper.getInstance(getApplication()).setVitalityStepOffset(event.values[0]);
Expand All @@ -445,7 +471,8 @@ public void onSensorChanged(SensorEvent event) {

Logger.e(TAG, "当前步数 : " + StepDcretor.CURRENT_SETP + "步");
Logger.e(TAG, "传感器步数 : " + event.values[0] + "步");

mLog4j.e("当前步数 : " + StepDcretor.CURRENT_SETP + "步");
mLog4j.e( "传感器步数 : " + event.values[0] + "步");

vitalityStepData();

Expand Down

0 comments on commit 60499be

Please sign in to comment.